Projekt

Obecné

Profil

Stáhnout (2.21 KB) Statistiky
| Větev: | Tag: | Revize:
1 a70f3c5f Vojtěch Bartička
using Core.Services;
2 6bdb3d95 Vojtěch Bartička
using Core.Services.AnnotationService;
3 a70f3c5f Vojtěch Bartička
using Microsoft.AspNetCore.Mvc;
4 2d7e2ad6 Vojtěch Bartička
using Models.Annotations;
5 a70f3c5f Vojtěch Bartička
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 6bdb3d95 Vojtěch Bartička
        private readonly IAnnotationService annotationService;
19 a70f3c5f Vojtěch Bartička
20 6bdb3d95 Vojtěch Bartička
        public UserController(Serilog.ILogger logger, IUserService userService, IAnnotationService annotationService)
21 a70f3c5f Vojtěch Bartička
        {
22
            this.logger = logger;
23
            this.userService = userService;
24 6bdb3d95 Vojtěch Bartička
            this.annotationService = annotationService;
25 a70f3c5f Vojtěch Bartička
        }
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 329ac528 Vojtěch Bartička
                logger.Warning("ClientInfo has null LoggerUser in [Authorized] controller /users");
36 a70f3c5f Vojtěch Bartička
                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 6bdb3d95 Vojtěch Bartička
50
        }
51
52
        [HttpGet("/user/annotations")]
53 2d7e2ad6 Vojtěch Bartička
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(AnnotationListResponse))]
54 6bdb3d95 Vojtěch Bartička
        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
55 2d7e2ad6 Vojtěch Bartička
        public ActionResult<AnnotationListResponse> GetUserAnnotations([FromServices] ClientInfo clientInfo)
56 6bdb3d95 Vojtěch Bartička
        {
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 a70f3c5f Vojtěch Bartička
        }
66
    }
67
}