| Name | Message | Date |
|---|---|---|
| 📄 AdminAuthService.cs | 11 days ago | |
| 📄 DateService.cs | 11 days ago | |
| 📄 ExportService.cs | 10 days ago | |
| 📄 NotificationBackgroundService.cs | 10 days ago | |
| 📄 NotificationService.cs | 10 days ago | |
| 📄 NotificationServiceExtensions.cs | 10 days ago |
📄
MatDenDagen/Services/ExportService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
using System; using System.IO; using System.IO.Compression; using System.Text; using System.Threading; using System.Threading.Tasks; using MatDenDagen.Infrastructure.Storage.BlobStorage; using MatDenDagen.Infrastructure.Storage.Database; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace MatDenDagen.Services; public sealed class ExportService( ILogger<ExportService> logger, QuestionnaireContext questionnaireContext, BlobStorageService blobStorage ) { public async Task ExportAllSubmissions(Stream outputStream, CancellationToken cancellationToken) { await using var archive = await ZipArchive.CreateAsync( outputStream, ZipArchiveMode.Create, leaveOpen: true, Encoding.UTF8, cancellationToken ); var questions = await questionnaireContext.Questions.ToDictionaryAsync( q => q.Id, q => q.Text, cancellationToken ); var submissions = await questionnaireContext .Submissions.Include(s => s.Answers) .Include(s => s.Uploads) .ToArrayAsync(cancellationToken); { var entry = archive.CreateEntry("export.md"); await using var entryStream = await entry.OpenAsync(cancellationToken); await using var writer = new StreamWriter(entryStream); await writer.WriteLineAsync( $""" # Export Svar från {submissions.Length} deltagare """.AsMemory(), cancellationToken ); } foreach (var submission in submissions) { var participant = await questionnaireContext.Participants.SingleAsync( p => p.Id == submission.Participant, cancellationToken ); { var entry = archive.CreateEntry(Path.Join(participant.Name, "svar.md")); await using var entryStream = await entry.OpenAsync(cancellationToken); await using var writer = new StreamWriter(entryStream); await writer.WriteLineAsync( $""" # {participant.Name} """.AsMemory(), cancellationToken ); foreach (var answer in submission.Answers) { if (!questions.TryGetValue(answer.QuestionId, out var question)) { logger.LogError("An answer to {QuestionId} exist, but no question.", answer.QuestionId); question = answer.QuestionId.ToString(); } await writer.WriteLineAsync( $""" ## {question} {answer.Text} """.AsMemory(), cancellationToken ); } } foreach (var upload in submission.Uploads) { var entry = archive.CreateEntry(Path.Join(participant.Name, upload.Name)); await using var entryStream = await entry.OpenAsync(cancellationToken); var blobStream = blobStorage.GetBlob(upload.Id); if (blobStream is null) { logger.LogError("Upload {UploadId} is missing from blob storage.", upload.Id); await using var writer = new StreamWriter(entryStream); await writer.WriteLineAsync("Filen saknas.".AsMemory(), cancellationToken); } else { await using (blobStream) { await blobStream.CopyToAsync(entryStream, cancellationToken); } } } } } } public static class ExportServiceCollectionExtensions { public static IServiceCollection AddExportService(this IServiceCollection services) => services.AddTransient<ExportService>(); }