Projekt

Obecné

Profil

Stáhnout (2.29 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, [FromBody] DocumentListRequest documentListRequest)
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(documentListRequest);
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

    
(2-2/2)