Projekt

Obecné

Profil

« Předchozí | Další » 

Revize bb5a4390

Přidáno uživatelem Vojtěch Bartička před asi 2 roky(ů)

DocumentController created

Zobrazit rozdíly:

Backend/Backend/Controllers/DocumentController.cs
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

  
10
namespace RestAPI.Controllers;
11

  
12

  
13

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

  
19

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

  
26
    [HttpGet("/documents")]
27
    [ProducesResponseType((int)HttpStatusCode.OK)]
28
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
29
    [ProducesResponseType((int)HttpStatusCode.InternalServerError)]
30
    public ActionResult<DocumentListResponse> GetDocuments([FromServices] ClientInfo clientInfo, [FromBody] DocumentListRequest documentListRequest)
31
    {
32
        if (clientInfo.LoggedUser == null)
33
        {
34
            logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /documents");
35
            return Problem();
36
        }
37

  
38
        if (clientInfo.LoggedUser.Role != Core.Enums.ERole.ADMINISTRATOR)
39
        {
40
            return Forbid("User is not administrator");
41
        }
42

  
43
        return documentService.GetDocuments(documentListRequest);
44
    }
45

  
46
    [HttpPost("/documents")]
47
    [ProducesResponseType((int)HttpStatusCode.OK)]
48
    [ProducesResponseType((int)HttpStatusCode.Forbidden)]
49
    [ProducesResponseType((int)HttpStatusCode.InternalServerError)]
50
    [ProducesResponseType((int)HttpStatusCode.UnsupportedMediaType)]
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
        if (clientInfo.LoggedUser.Role != Core.Enums.ERole.ADMINISTRATOR)
60
        {
61
            return Forbid("User is not administrator");
62
        }
63

  
64
        try
65
        {
66
            documentService.AddDocuments(documentAddRequest, clientInfo.LoggedUser.Id);
67
        }
68
        catch (InvalidOperationException ex)
69
        {
70
            logger.Warning($"Error in DocumentService.AddDocuments - could not retrieve user with GUID {clientInfo.LoggedUser.Id} from database");
71
            throw new InternalErrorException();
72
        }
73
        catch (Exception ex)
74
        {
75
            logger.Warning($"Error in DocumentService.AddDocuments - could not process ZIP file");
76
            throw new BadRequestException("Invalid ZIP file");
77
        }
78

  
79
        return Ok();
80
    }
81

  
82
}
83

  

Také k dispozici: Unified diff