Projekt

Obecné

Profil

Stáhnout (2.21 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Core.Services;
2
using Core.Services.AnnotationService;
3
using Microsoft.AspNetCore.Mvc;
4
using Models.Annotations;
5
using Models.Users;
6
using RestAPI.Authentication;
7
using RestAPI.Controllers.Common;
8
using RestAPI.Exceptions;
9
using RestAPI.Utils;
10
using System.Net;
11

    
12
namespace RestAPI.Controllers
13
{
14
    public class UserController : CommonControllerBase
15
    {
16
        private readonly Serilog.ILogger logger;
17
        private readonly IUserService userService;
18
        private readonly IAnnotationService annotationService;
19

    
20
        public UserController(Serilog.ILogger logger, IUserService userService, IAnnotationService annotationService)
21
        {
22
            this.logger = logger;
23
            this.userService = userService;
24
            this.annotationService = annotationService;
25
        }
26

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

    
39
            try
40
            {
41
                var userList = userService.GetUsers();
42
                return Ok(userList);
43
            }
44
            catch (Exception e)
45
            {
46
                throw new InternalErrorException(e.Message);
47
            }
48

    
49

    
50
        }
51

    
52
        [HttpGet("/user/annotations")]
53
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(AnnotationListResponse))]
54
        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
55
        public ActionResult<AnnotationListResponse> GetUserAnnotations([FromServices] ClientInfo clientInfo)
56
        {
57
            if (clientInfo.LoggedUser == null)
58
            {
59
                logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /annotations");
60
                return Problem();
61
            }
62

    
63
            var res = annotationService.GetUserAnnotations(clientInfo.LoggedUser.Id);
64
            return Ok(res);
65
        }
66
    }
67
}
(5-5/5)