1
|
using Microsoft.AspNetCore.Mvc;
|
2
|
using Models.Annotations;
|
3
|
using RestAPI.Authentication;
|
4
|
using RestAPI.Controllers.Common;
|
5
|
using RestAPI.Utils;
|
6
|
using System.Net;
|
7
|
using Serilog;
|
8
|
using Core.Services.AnnotationService;
|
9
|
using RestAPI.Exceptions;
|
10
|
|
11
|
namespace RestAPI.Controllers
|
12
|
{
|
13
|
public class AnnotationController : CommonControllerBase
|
14
|
{
|
15
|
private readonly Serilog.ILogger logger;
|
16
|
private readonly IAnnotationService annotationService;
|
17
|
|
18
|
public AnnotationController(Serilog.ILogger logger, IAnnotationService annotationService)
|
19
|
{
|
20
|
this.logger = logger;
|
21
|
this.annotationService = annotationService;
|
22
|
}
|
23
|
|
24
|
[HttpPost("/annotations")]
|
25
|
[Authorize(Models.Enums.ERole.ADMINISTRATOR)]
|
26
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
27
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
28
|
public ActionResult AddDocumentAnnotations([FromServices] ClientInfo clientInfo, [FromBody] AnnotationsAddRequest request)
|
29
|
{
|
30
|
if (clientInfo.LoggedUser == null)
|
31
|
{
|
32
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
|
33
|
return Problem();
|
34
|
}
|
35
|
|
36
|
try
|
37
|
{
|
38
|
annotationService.CreateDocumentAnnotations(request, clientInfo.LoggedUser.Id);
|
39
|
}
|
40
|
catch (InvalidOperationException e)
|
41
|
{
|
42
|
throw new BadRequestException(e.Message);
|
43
|
}
|
44
|
|
45
|
return Ok();
|
46
|
}
|
47
|
|
48
|
[HttpGet("/annotation/{annotationId}")]
|
49
|
[ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(AnnotationInfo))]
|
50
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
51
|
public ActionResult<AnnotationInfo> GetAnnotation([FromServices] ClientInfo clientInfo, Guid annotationId)
|
52
|
{
|
53
|
if (clientInfo.LoggedUser == null)
|
54
|
{
|
55
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
|
56
|
return Problem();
|
57
|
}
|
58
|
|
59
|
// Take care of - non-admin user requesting not-assigned annotation
|
60
|
// non-existent annotation
|
61
|
try
|
62
|
{
|
63
|
var res = annotationService.GetAnnotation(annotationId, clientInfo.LoggedUser.Id, clientInfo.LoggedUser.Role);
|
64
|
return Ok(res);
|
65
|
}
|
66
|
catch (InvalidOperationException e)
|
67
|
{
|
68
|
throw new BadRequestException("Could not find specified annotation");
|
69
|
}
|
70
|
catch (UnauthorizedAccessException)
|
71
|
{
|
72
|
return Forbid();
|
73
|
}
|
74
|
|
75
|
}
|
76
|
|
77
|
[HttpPost("/annotation/{annotationId}")]
|
78
|
[ProducesResponseType((int)HttpStatusCode.OK)]
|
79
|
[ProducesResponseType((int)HttpStatusCode.Forbidden)]
|
80
|
public ActionResult PostAnnotation([FromServices] ClientInfo clientInfo, Guid annotationId, [FromBody] AnnotationInstanceAddRequest request)
|
81
|
{
|
82
|
if (clientInfo.LoggedUser == null)
|
83
|
{
|
84
|
logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
|
85
|
return Problem();
|
86
|
}
|
87
|
|
88
|
// Take care of - non-admin user requesting not-assigned annotation
|
89
|
// non-existent annotation
|
90
|
try
|
91
|
{
|
92
|
annotationService.AddAnnotationInstance(annotationId, clientInfo.LoggedUser.Id, clientInfo.LoggedUser.Role, request);
|
93
|
return Ok();
|
94
|
}
|
95
|
catch (InvalidOperationException e)
|
96
|
{
|
97
|
throw new BadRequestException("Could not find specified annotation");
|
98
|
}
|
99
|
catch (UnauthorizedAccessException)
|
100
|
{
|
101
|
return Forbid();
|
102
|
}
|
103
|
|
104
|
}
|
105
|
}
|
106
|
}
|