1
|
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
|
using RestAPI.Authentication;
|
9
|
using Models.Annotations;
|
10
|
using Core.Services.AnnotationService;
|
11
|
using System.Net.Http.Headers;
|
12
|
using Microsoft.Net.Http.Headers;
|
13
|
using System.Net.Mime;
|
14
|
|
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
|
private readonly IAnnotationService annotationService;
|
24
|
|
25
|
public DocumentController(IDocumentService documentService, IAnnotationService annotationService, ILogger logger)
|
26
|
{
|
27
|
this.documentService = documentService;
|
28
|
this.logger = logger;
|
29
|
this.annotationService = annotationService;
|
30
|
}
|
31
|
|
32
|
[HttpGet("/documents")]
|
33
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
34
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
35
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
36
|
public ActionResult<DocumentListResponse> GetDocuments([FromServices] ClientInfo clientInfo)
|
37
|
{
|
38
|
if (clientInfo.LoggedUser == null)
|
39
|
{
|
40
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
41
|
return Problem();
|
42
|
}
|
43
|
|
44
|
return documentService.GetDocuments();
|
45
|
}
|
46
|
|
47
|
[HttpPost("/documents")]
|
48
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
49
|
[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
|
catch (InvalidOperationException)
|
64
|
{
|
65
|
logger.Warning($"Error in DocumentService.AddDocuments - could not retrieve user with GUID {clientInfo.LoggedUser.Id} from database");
|
66
|
throw new InternalErrorException();
|
67
|
}
|
68
|
catch (Exception)
|
69
|
{
|
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
|
|
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
|
|
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
|
|
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
|
|
166
|
[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
|
}
|
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
|
}
|
209
|
}
|
210
|
|
211
|
[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
|
|
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
|
|
262
|
[HttpPost("/documents/export")]
|
263
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
264
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(String))]
|
265
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
266
|
public ActionResult<string> ExportDocuments([FromServices] ClientInfo clientInfo, [FromBody] ExportRequest request)
|
267
|
{
|
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
|
var bytes = outputStream.ToArray();
|
278
|
var outputString = Convert.ToBase64String(bytes);
|
279
|
|
280
|
return Ok(outputString);
|
281
|
}
|
282
|
catch (InvalidOperationException e)
|
283
|
{
|
284
|
throw new BadRequestException(e.Message);
|
285
|
}
|
286
|
}
|
287
|
}
|
288
|
|