Projekt

Obecné

Profil

Stáhnout (9.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 bb5a4390 Vojtěch Bartička
using Core.Services.DocumentService;
2
using Microsoft.AspNetCore.Mvc;
3
using RestAPI.Utils;
4
using System.Net;
5
using ILogger = Serilog.ILogger;
6
using Models.Documents;
7
using RestAPI.Exceptions;
8 3c161f5b Vojtěch Bartička
using RestAPI.Authentication;
9 440997e0 Vojtěch Bartička
using Models.Annotations;
10
using Core.Services.AnnotationService;
11 baf6fc22 Vojtěch Bartička
using System.Net.Http.Headers;
12
using Microsoft.Net.Http.Headers;
13
using System.Net.Mime;
14 bb5a4390 Vojtěch Bartička
15
namespace RestAPI.Controllers;
16
17
18
19
public class DocumentController : Common.CommonControllerBase
20
{
21
    private readonly IDocumentService documentService;
22
    private readonly ILogger logger;
23 440997e0 Vojtěch Bartička
    private readonly IAnnotationService annotationService;
24 bb5a4390 Vojtěch Bartička
25 440997e0 Vojtěch Bartička
    public DocumentController(IDocumentService documentService, IAnnotationService annotationService, ILogger logger)
26 bb5a4390 Vojtěch Bartička
    {
27
        this.documentService = documentService;
28
        this.logger = logger;
29 440997e0 Vojtěch Bartička
        this.annotationService = annotationService;
30 bb5a4390 Vojtěch Bartička
    }
31
32
    [HttpGet("/documents")]
33 23165326 Vojtěch Bartička
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
34 bb5a4390 Vojtěch Bartička
    [ProducesResponseType((int)HttpStatusCode.OK)]
35
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
36 004c4a4e Vojtěch Bartička
    public ActionResult<DocumentListResponse> GetDocuments([FromServices] ClientInfo clientInfo)
37 bb5a4390 Vojtěch Bartička
    {
38
        if (clientInfo.LoggedUser == null)
39
        {
40
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
41
            return Problem();
42
        }
43
44 004c4a4e Vojtěch Bartička
        return documentService.GetDocuments();
45 bb5a4390 Vojtěch Bartička
    }
46
47
    [HttpPost("/documents")]
48 23165326 Vojtěch Bartička
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
49 bb5a4390 Vojtěch Bartička
    [ProducesResponseType((int)HttpStatusCode.OK)]
50
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
51
    public ActionResult PostDocuments([FromServices] ClientInfo clientInfo, [FromBody] DocumentAddRequest documentAddRequest)
52
    {
53
        if (clientInfo.LoggedUser == null)
54
        {
55
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
56
            return Problem();
57
        }
58
59
        try
60
        {
61
            documentService.AddDocuments(documentAddRequest, clientInfo.LoggedUser.Id);
62
        }
63 11f447b9 Vojtěch Bartička
        catch (InvalidOperationException)
64 bb5a4390 Vojtěch Bartička
        {
65
            logger.Warning($"Error in DocumentService.AddDocuments - could not retrieve user with GUID {clientInfo.LoggedUser.Id} from database");
66
            throw new InternalErrorException();
67
        }
68 11f447b9 Vojtěch Bartička
        catch (Exception)
69 bb5a4390 Vojtěch Bartička
        {
70
            logger.Warning($"Error in DocumentService.AddDocuments - could not process ZIP file");
71
            throw new BadRequestException("Invalid ZIP file");
72
        }
73
74
        return Ok();
75
    }
76
77 5adba4c4 Vojtěch Bartička
78
79
80
    [HttpPost("/documents/requiredAnnotations")]
81
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
82
    [ProducesResponseType((int)HttpStatusCode.OK)]
83
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
84
    public ActionResult SetRequiredAnnotationsForDocuments([FromServices] ClientInfo clientInfo, [FromBody] SetRequiredAnnotationsRequest request)
85
    {
86
        if (clientInfo.LoggedUser == null)
87
        {
88
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
89
            return Problem();
90
        }
91
92
        try
93
        {
94
            documentService.SetRequiredAnnotationsForDocuments(request);
95
        }
96
        catch (InvalidOperationException e)
97
        {
98
            throw new BadRequestException(e.Message);
99
        }
100
        catch (Exception e)
101
        {
102
            throw new InternalErrorException(e.Message);
103
        }
104
105
        return Ok();
106
    }
107 0a6d22b7 Vojtěch Bartička
108
    [HttpPost("/documents/requiredAnnotations/global")]
109
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
110
    [ProducesResponseType((int)HttpStatusCode.OK)]
111
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
112
    public ActionResult SetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo, [FromBody] SetRequiredAnnotationsGlobalRequest request)
113
    {
114
        if (clientInfo.LoggedUser == null)
115
        {
116
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
117
            return Problem();
118
        }
119
120
        try
121
        {
122
            documentService.SetRequiredAnnotationsGlobal(request.RequiredAnnotations);
123
        }
124
        catch (InvalidOperationException e)
125
        {
126
            throw new BadRequestException(e.Message);
127
        }
128
        catch (Exception e)
129
        {
130
            throw new InternalErrorException(e.Message);
131
        }
132
133
        return Ok();
134
    }
135 ceb95b98 Vojtěch Bartička
136
    [HttpGet("/documents/requiredAnnotations/global")]
137
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
138
    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GetRequiredAnnotationsGlobalResponse))]
139
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
140
    public ActionResult<GetRequiredAnnotationsGlobalResponse> GetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo)
141
    {
142
        if (clientInfo.LoggedUser == null)
143
        {
144
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
145
            return Problem();
146
        }
147
148
        try
149
        {
150
            return Ok(new GetRequiredAnnotationsGlobalResponse()
151
            {
152
                RequiredAnnotationsGlobal = documentService.GetRequiredAnnotationsGlobal()
153
            });
154
155
        }
156
        catch (InvalidOperationException e)
157
        {
158
            throw new BadRequestException(e.Message);
159
        }
160
        catch (Exception e)
161
        {
162
            throw new InternalErrorException(e.Message);
163
        }
164
    }
165 d8873409 Vojtěch Bartička
166 42bb0025 Vojtěch Bartička
    [HttpGet("/document/{documentId}")]
167
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
168
    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DocumentPreviewResponse))]
169
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
170
    public ActionResult GetDocumentPreview([FromServices] ClientInfo clientInfo, Guid documentId)
171
    {
172
        if (clientInfo.LoggedUser == null)
173
        {
174
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
175
            return Problem();
176
        }
177
178
        try
179
        {
180
            return Ok(documentService.GetDocumentPreview(documentId));
181
        }
182
        catch (InvalidOperationException e)
183
        {
184
            throw new BadRequestException(e.Message);
185 c8159dc5 Vojtěch Bartička
        }
186
    }
187
188
    [HttpDelete("/documents")]
189
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
190
    [ProducesResponseType((int)HttpStatusCode.OK)]
191
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
192
    public ActionResult DeleteDocument([FromServices] ClientInfo clientInfo, [FromBody] DeleteDocumentsRequest request)
193
    {
194
        if (clientInfo.LoggedUser == null)
195
        {
196
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
197
            return Problem();
198
        }
199
200
        try
201
        {
202
            documentService.DeleteDocuments(request);
203
            return Ok();
204
        }
205
        catch (InvalidOperationException e)
206
        {
207
            throw new BadRequestException(e.Message);
208 42bb0025 Vojtěch Bartička
        }
209
    }
210
211 440997e0 Vojtěch Bartička
    [HttpPost("/document/{documentId}/final")]
212
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
213
    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(CreateFinalAnnotationResponse))]
214
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
215
    public ActionResult<CreateFinalAnnotationResponse> CreateFinalAnnotation([FromServices] ClientInfo clientInfo, Guid documentId)
216
    {
217
        if (clientInfo.LoggedUser == null)
218
        {
219
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller");
220
            return Problem();
221
        }
222
223
        try
224
        {
225
            var guid = annotationService.CreateFinalAnnotation(documentId, clientInfo.LoggedUser.Id);
226
227
            return Ok(new CreateFinalAnnotationResponse()
228
            {
229
                FinalAnnotationId = guid
230
            });
231
        }
232
        catch (InvalidOperationException e)
233
        {
234
            throw new BadRequestException(e.Message);
235
        }
236
    }
237 4540d7b6 Vojtěch Bartička
238
    [HttpDelete("/documents/{documentId}/annotators/{annotatorId}")]
239
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
240
    [ProducesResponseType((int)HttpStatusCode.OK)]
241
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
242
    public ActionResult RemoveAnnotatorFromDocument([FromServices] ClientInfo clientInfo, Guid documentId, Guid annotatorId)
243
    {
244
        if (clientInfo.LoggedUser == null)
245
        {
246
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
247
            return Problem();
248
        }
249
250
        try
251
        {
252
            documentService.RemoveAnnotatorFromDocument(documentId, annotatorId);
253
            return Ok();
254
        }
255
        catch (InvalidOperationException e)
256
        {
257
            throw new BadRequestException("Could not find specified annotation");
258
        }
259
260
    }
261 830652de Vojtěch Bartička
262
    [HttpPost("/documents/export")]
263
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
264 dd5390e2 Vojtěch Bartička
    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(String))]
265 830652de Vojtěch Bartička
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
266 dd5390e2 Vojtěch Bartička
    public ActionResult<string> ExportDocuments([FromServices] ClientInfo clientInfo, [FromBody] ExportRequest request)
267 830652de Vojtěch Bartička
    {
268
        if (clientInfo.LoggedUser == null)
269
        {
270
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller");
271
            return Problem();
272
        }
273
274
        try
275
        {
276
            var outputStream = annotationService.Export(request);
277 dd5390e2 Vojtěch Bartička
            var bytes = outputStream.ToArray();
278
            var outputString = Convert.ToBase64String(bytes);
279 830652de Vojtěch Bartička
280 dd5390e2 Vojtěch Bartička
            return Ok(outputString);
281 830652de Vojtěch Bartička
        }
282
        catch (InvalidOperationException e)
283
        {
284
            throw new BadRequestException(e.Message);
285
        }
286
    }
287 bb5a4390 Vojtěch Bartička
}