Projekt

Obecné

Profil

« Předchozí | Další » 

Revize a9acbe07

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

Created endpoint for changing user information

Zobrazit rozdíly:

Backend/Backend/Controllers/UserController.cs
73 73
            var res = annotationService.GetUserAnnotations(userId);
74 74
            return Ok(res);
75 75
        }
76

  
77
        [HttpPut("/user/{userId}")]
78
        [Authorize(Models.Enums.ERole.ADMINISTRATOR)]
79
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(AnnotationListResponse))]
80
        [ProducesResponseType((int)HttpStatusCode.Forbidden)]
81
        public ActionResult<AnnotationListResponse> ChangeUserInfo(Guid userId, [FromBody] ChangeUserInfoRequest request)
82
        {
83
            try
84
            {
85
                if (request.Password != null)
86
                {
87
                    if (userService.ChangePassword(userId, request.Password) == null)
88
                    {
89
                        throw new BadRequestException("User not found or password is empty");
90
                    }
91
                }
92

  
93
                if (userService.UpdateUser(userId, request.Username, request.Name, request.Surname, request.Role) == null)
94
                {
95
                    throw new BadRequestException("User not found");
96
                }
97
            }
98
            catch (InvalidOperationException e)
99
            {
100
                throw new BadRequestException(e.Message);
101
            }
102

  
103
            return Ok();
104
        }
76 105
    }
77 106
}
Backend/Core/Services/UserService/IUserService.cs
15 15
        public User? GetUserByUsername(string username);
16 16
        public User? GetUserById(Guid id);
17 17
        public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null);
18
        public User? UpdateUser(Guid userId, string? username = null, string? name = null, string? surname = null, ERole? role = null);
18 19
        public User ChangePassword(User user, string newPassword);
20
        public User? ChangePassword(Guid userId, string newPassword);
19 21
        public User? CheckUsernamePassword(string username, string password);
20 22
        public UserList GetUsers();
21 23
    }
Backend/Core/Services/UserService/UserServiceEF.cs
40 40
            return user;
41 41
        }
42 42

  
43
        public User? ChangePassword(Guid userId, string newPassword)
44
        {
45
            User? user = null;
46
            user = _databaseContext.Users.First(u => u.Id == userId);
47

  
48
            return ChangePassword(user, newPassword);
49
        }
50

  
43 51
        public User? CheckUsernamePassword(string username, string password)
44 52
        {
45 53
            try
......
146 154
            return user;
147 155
        }
148 156

  
157
        public User? UpdateUser(Guid userId, string? username = null, string? name = null, string? surname = null, ERole? role = null)
158
        {
159
            try
160
            {
161
                User user = _databaseContext.Users.First(u => u.Id == userId);
162
                return UpdateUser(user, username, name, surname, role);
163
            }
164
            catch (Exception ex)
165
            {
166
                return null;
167
            }
168
        }
169

  
170

  
149 171
        public UserList GetUsers() => new UserList()
150 172
        {
151 173
            Users = _databaseContext.Users.Select(u => _mapper.Map<UserInfo>(u)).ToList()
Backend/Models/Users/ChangeUserInfoRequest.cs
1
using Models.Enums;
2
using System;
3
using System.Collections.Generic;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7

  
8
namespace Models.Users
9
{
10
    public class ChangeUserInfoRequest
11
    {
12
        public string? Password { get; set; } = null;
13
        public string? Username { get; set; } = null;
14
        public string? Name { get; set; } = null;
15
        public string? Surname { get; set; } = null;
16
        public ERole? Role { get; set; } = null;
17
    }
18
}

Také k dispozici: Unified diff