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
|
|
12
|
namespace RestAPI.Controllers;
|
13
|
|
14
|
|
15
|
|
16
|
public class DocumentController : Common.CommonControllerBase
|
17
|
{
|
18
|
private readonly IDocumentService documentService;
|
19
|
private readonly ILogger logger;
|
20
|
private readonly IAnnotationService annotationService;
|
21
|
|
22
|
public DocumentController(IDocumentService documentService, IAnnotationService annotationService, ILogger logger)
|
23
|
{
|
24
|
this.documentService = documentService;
|
25
|
this.logger = logger;
|
26
|
this.annotationService = annotationService;
|
27
|
}
|
28
|
|
29
|
[HttpGet("/documents")]
|
30
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
31
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
32
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
33
|
public ActionResult<DocumentListResponse> GetDocuments([FromServices] ClientInfo clientInfo, [FromQuery] int pageIndex, [FromQuery] int pageSize)
|
34
|
{
|
35
|
if (clientInfo.LoggedUser == null)
|
36
|
{
|
37
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
38
|
return Problem();
|
39
|
}
|
40
|
|
41
|
return documentService.GetDocuments(pageIndex, pageSize);
|
42
|
}
|
43
|
|
44
|
[HttpPost("/documents")]
|
45
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
46
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
47
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
48
|
public ActionResult PostDocuments([FromServices] ClientInfo clientInfo, [FromBody] DocumentAddRequest documentAddRequest)
|
49
|
{
|
50
|
if (clientInfo.LoggedUser == null)
|
51
|
{
|
52
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
53
|
return Problem();
|
54
|
}
|
55
|
|
56
|
try
|
57
|
{
|
58
|
documentService.AddDocuments(documentAddRequest, clientInfo.LoggedUser.Id);
|
59
|
}
|
60
|
catch (InvalidOperationException)
|
61
|
{
|
62
|
logger.Warning($"Error in DocumentService.AddDocuments - could not retrieve user with GUID {clientInfo.LoggedUser.Id} from database");
|
63
|
throw new InternalErrorException();
|
64
|
}
|
65
|
catch (Exception)
|
66
|
{
|
67
|
logger.Warning($"Error in DocumentService.AddDocuments - could not process ZIP file");
|
68
|
throw new BadRequestException("Invalid ZIP file");
|
69
|
}
|
70
|
|
71
|
return Ok();
|
72
|
}
|
73
|
|
74
|
|
75
|
|
76
|
|
77
|
[HttpPost("/documents/requiredAnnotations")]
|
78
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
79
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
80
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
81
|
public ActionResult SetRequiredAnnotationsForDocuments([FromServices] ClientInfo clientInfo, [FromBody] SetRequiredAnnotationsRequest request)
|
82
|
{
|
83
|
if (clientInfo.LoggedUser == null)
|
84
|
{
|
85
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
86
|
return Problem();
|
87
|
}
|
88
|
|
89
|
try
|
90
|
{
|
91
|
documentService.SetRequiredAnnotationsForDocuments(request);
|
92
|
}
|
93
|
catch (InvalidOperationException e)
|
94
|
{
|
95
|
throw new BadRequestException(e.Message);
|
96
|
}
|
97
|
catch (Exception e)
|
98
|
{
|
99
|
throw new InternalErrorException(e.Message);
|
100
|
}
|
101
|
|
102
|
return Ok();
|
103
|
}
|
104
|
|
105
|
[HttpPost("/documents/requiredAnnotations/global")]
|
106
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
107
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
108
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
109
|
public ActionResult SetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo, [FromBody] SetRequiredAnnotationsGlobalRequest request)
|
110
|
{
|
111
|
if (clientInfo.LoggedUser == null)
|
112
|
{
|
113
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
114
|
return Problem();
|
115
|
}
|
116
|
|
117
|
try
|
118
|
{
|
119
|
documentService.SetRequiredAnnotationsGlobal(request.RequiredAnnotations);
|
120
|
}
|
121
|
catch (InvalidOperationException e)
|
122
|
{
|
123
|
throw new BadRequestException(e.Message);
|
124
|
}
|
125
|
catch (Exception e)
|
126
|
{
|
127
|
throw new InternalErrorException(e.Message);
|
128
|
}
|
129
|
|
130
|
return Ok();
|
131
|
}
|
132
|
|
133
|
[HttpGet("/documents/requiredAnnotations/global")]
|
134
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
135
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GetRequiredAnnotationsGlobalResponse))]
|
136
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
137
|
public ActionResult<GetRequiredAnnotationsGlobalResponse> GetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo)
|
138
|
{
|
139
|
if (clientInfo.LoggedUser == null)
|
140
|
{
|
141
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
142
|
return Problem();
|
143
|
}
|
144
|
|
145
|
try
|
146
|
{
|
147
|
return Ok(new GetRequiredAnnotationsGlobalResponse()
|
148
|
{
|
149
|
RequiredAnnotationsGlobal = documentService.GetRequiredAnnotationsGlobal()
|
150
|
});
|
151
|
|
152
|
}
|
153
|
catch (InvalidOperationException e)
|
154
|
{
|
155
|
throw new BadRequestException(e.Message);
|
156
|
}
|
157
|
catch (Exception e)
|
158
|
{
|
159
|
throw new InternalErrorException(e.Message);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
[HttpGet("/document/{documentId}")]
|
164
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
165
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(DocumentPreviewResponse))]
|
166
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
167
|
public ActionResult GetDocumentPreview([FromServices] ClientInfo clientInfo, Guid documentId)
|
168
|
{
|
169
|
if (clientInfo.LoggedUser == null)
|
170
|
{
|
171
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
|
172
|
return Problem();
|
173
|
}
|
174
|
|
175
|
try
|
176
|
{
|
177
|
return Ok(documentService.GetDocumentPreview(documentId));
|
178
|
}
|
179
|
catch (InvalidOperationException e)
|
180
|
{
|
181
|
throw new BadRequestException(e.Message);
|
182
|
}
|
183
|
}
|
184
|
|
185
|
[HttpPost("/document/{documentId}/final")]
|
186
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
187
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(CreateFinalAnnotationResponse))]
|
188
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
189
|
public ActionResult<CreateFinalAnnotationResponse> CreateFinalAnnotation([FromServices] ClientInfo clientInfo, Guid documentId)
|
190
|
{
|
191
|
if (clientInfo.LoggedUser == null)
|
192
|
{
|
193
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller");
|
194
|
return Problem();
|
195
|
}
|
196
|
|
197
|
try
|
198
|
{
|
199
|
var guid = annotationService.CreateFinalAnnotation(documentId, clientInfo.LoggedUser.Id);
|
200
|
|
201
|
return Ok(new CreateFinalAnnotationResponse()
|
202
|
{
|
203
|
FinalAnnotationId = guid
|
204
|
});
|
205
|
}
|
206
|
catch (InvalidOperationException e)
|
207
|
{
|
208
|
throw new BadRequestException(e.Message);
|
209
|
}
|
210
|
}
|
211
|
|
212
|
[HttpPost("/documents/export")]
|
213
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
214
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
215
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
216
|
public ActionResult<ExportResponse> ExportDocuments([FromServices] ClientInfo clientInfo, [FromBody] ExportRequest request)
|
217
|
{
|
218
|
if (clientInfo.LoggedUser == null)
|
219
|
{
|
220
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller");
|
221
|
return Problem();
|
222
|
}
|
223
|
|
224
|
try
|
225
|
{
|
226
|
var outputStream = annotationService.Export(request);
|
227
|
|
228
|
var cd = new System.Net.Mime.ContentDisposition()
|
229
|
{
|
230
|
FileName = "export.zip",
|
231
|
Inline = false,
|
232
|
};
|
233
|
|
234
|
Response.Headers.Add("Content-Disposition", cd.ToString());
|
235
|
return File(outputStream, "application/zip");
|
236
|
}
|
237
|
catch (InvalidOperationException e)
|
238
|
{
|
239
|
throw new BadRequestException(e.Message);
|
240
|
}
|
241
|
}
|
242
|
}
|
243
|
|