Projekt

Obecné

Profil

Stáhnout (5.11 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Core.Services.DocumentService;
2
using Microsoft.AspNetCore.Mvc;
3
using RestAPI.Utils;
4
using System.Net;
5
using Serilog;
6
using ILogger = Serilog.ILogger;
7
using Models.Documents;
8
using RestAPI.Exceptions;
9
using RestAPI.Authentication;
10

    
11
namespace RestAPI.Controllers;
12

    
13

    
14

    
15
public class DocumentController : Common.CommonControllerBase
16
{
17
    private readonly IDocumentService documentService;
18
    private readonly ILogger logger;
19

    
20

    
21
    public DocumentController(IDocumentService documentService, ILogger logger)
22
    {
23
        this.documentService = documentService;
24
        this.logger = logger;
25
    }
26

    
27
    [HttpGet("/documents")]
28
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
29
    [ProducesResponseType((int)HttpStatusCode.OK)]
30
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
31
    public ActionResult<DocumentListResponse> GetDocuments([FromServices] ClientInfo clientInfo, [FromQuery] int pageIndex, [FromQuery] int pageSize)
32
    {
33
        if (clientInfo.LoggedUser == null)
34
        {
35
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
36
            return Problem();
37
        }
38

    
39
        return documentService.GetDocuments(pageIndex, pageSize);
40
    }
41

    
42
    [HttpPost("/documents")]
43
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
44
    [ProducesResponseType((int)HttpStatusCode.OK)]
45
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
46
    public ActionResult PostDocuments([FromServices] ClientInfo clientInfo, [FromBody] DocumentAddRequest documentAddRequest)
47
    {
48
        if (clientInfo.LoggedUser == null)
49
        {
50
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
51
            return Problem();
52
        }
53

    
54
        try
55
        {
56
            documentService.AddDocuments(documentAddRequest, clientInfo.LoggedUser.Id);
57
        }
58
        catch (InvalidOperationException)
59
        {
60
            logger.Warning($"Error in DocumentService.AddDocuments - could not retrieve user with GUID {clientInfo.LoggedUser.Id} from database");
61
            throw new InternalErrorException();
62
        }
63
        catch (Exception)
64
        {
65
            logger.Warning($"Error in DocumentService.AddDocuments - could not process ZIP file");
66
            throw new BadRequestException("Invalid ZIP file");
67
        }
68

    
69
        return Ok();
70
    }
71

    
72

    
73

    
74

    
75
    [HttpPost("/documents/requiredAnnotations")]
76
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
77
    [ProducesResponseType((int)HttpStatusCode.OK)]
78
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
79
    public ActionResult SetRequiredAnnotationsForDocuments([FromServices] ClientInfo clientInfo, [FromBody] SetRequiredAnnotationsRequest request)
80
    {
81
        if (clientInfo.LoggedUser == null)
82
        {
83
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
84
            return Problem();
85
        }
86

    
87
        try
88
        {
89
            documentService.SetRequiredAnnotationsForDocuments(request);
90
        }
91
        catch (InvalidOperationException e)
92
        {
93
            throw new BadRequestException(e.Message);
94
        }
95
        catch (Exception e)
96
        {
97
            throw new InternalErrorException(e.Message);
98
        }
99

    
100
        return Ok();
101
    }
102

    
103
    [HttpPost("/documents/requiredAnnotations/global")]
104
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
105
    [ProducesResponseType((int)HttpStatusCode.OK)]
106
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
107
    public ActionResult SetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo, [FromBody] SetRequiredAnnotationsGlobalRequest request)
108
    {
109
        if (clientInfo.LoggedUser == null)
110
        {
111
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
112
            return Problem();
113
        }
114

    
115
        try
116
        {
117
            documentService.SetRequiredAnnotationsGlobal(request.RequiredAnnotations);
118
        }
119
        catch (InvalidOperationException e)
120
        {
121
            throw new BadRequestException(e.Message);
122
        }
123
        catch (Exception e)
124
        {
125
            throw new InternalErrorException(e.Message);
126
        }
127

    
128
        return Ok();
129
    }
130

    
131
    [HttpGet("/documents/requiredAnnotations/global")]
132
    [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
133
    [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(GetRequiredAnnotationsGlobalResponse))]
134
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
135
    public ActionResult<GetRequiredAnnotationsGlobalResponse> GetRequiredAnnotationsGlobal([FromServices] ClientInfo clientInfo)
136
    {
137
        if (clientInfo.LoggedUser == null)
138
        {
139
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
140
            return Problem();
141
        }
142

    
143
        try
144
        {
145
            return Ok(new GetRequiredAnnotationsGlobalResponse()
146
            {
147
                RequiredAnnotationsGlobal = documentService.GetRequiredAnnotationsGlobal()
148
            });
149

    
150
        }
151
        catch (InvalidOperationException e)
152
        {
153
            throw new BadRequestException(e.Message);
154
        }
155
        catch (Exception e)
156
        {
157
            throw new InternalErrorException(e.Message);
158
        }
159
    }
160
}
161

    
(3-3/5)