|  |  | 1 |  | using System.ComponentModel.DataAnnotations; | 
|  |  | 2 |  | using WebApi.DAO; | 
|  |  | 3 |  | using WebApi.Models; | 
|  |  | 4 |  |  | 
|  |  | 5 |  | namespace WebApi.Services | 
|  |  | 6 |  | { | 
|  |  | 7 |  |    public class UserService : IUserService | 
|  |  | 8 |  | { | 
|  |  | 9 |  |  | 
|  |  | 10 |  |     private readonly IUserDAO _userDAO; | 
|  |  | 11 |  |  | 
|  | 5 | 12 |  |     public UserService(IUserDAO userDAO) | 
|  | 5 | 13 |  |     { | 
|  | 5 | 14 |  |         _userDAO = userDAO; | 
|  | 5 | 15 |  |     } | 
|  |  | 16 |  |  | 
|  |  | 17 |  |     public async Task<User> ValidateUserAsync(string username, string password) | 
|  | 2 | 18 |  |     { | 
|  |  | 19 |  |         try | 
|  | 2 | 20 |  |         { | 
|  | 2 | 21 |  |             User existingUser = await _userDAO.ValidateUserAsync(username, password); | 
|  | 1 | 22 |  |             if (existingUser == null) | 
|  | 0 | 23 |  |             { | 
|  | 0 | 24 |  |                 throw new UnauthorizedAccessException("Username or password is incorrect."); | 
|  |  | 25 |  |             } | 
|  | 1 | 26 |  |             return existingUser; | 
|  |  | 27 |  |         } | 
|  | 1 | 28 |  |         catch (UnauthorizedAccessException) | 
|  | 1 | 29 |  |         { | 
|  |  | 30 |  |             // Re-throw specific exceptions directly without wrapping | 
|  | 1 | 31 |  |             throw; | 
|  |  | 32 |  |         } | 
|  | 0 | 33 |  |         catch (Exception ex) | 
|  | 0 | 34 |  |         { | 
|  |  | 35 |  |             // For unexpected errors, you might still want to wrap to add context or log them appropriately | 
|  | 0 | 36 |  |             throw new Exception("An error occurred during user validation.", ex); | 
|  |  | 37 |  |         } | 
|  | 1 | 38 |  |     } | 
|  |  | 39 |  |  | 
|  |  | 40 |  |     public async Task<User> GetUserAsync(string username) | 
|  | 0 | 41 |  |     { | 
|  | 0 | 42 |  |         return await _userDAO.GetUserAsync(username); | 
|  | 0 | 43 |  |     } | 
|  |  | 44 |  |  | 
|  |  | 45 |  |     public async Task RegisterUserAsync(UserCreationDTO userCreationDTO) | 
|  | 2 | 46 |  |     { | 
|  | 2 | 47 |  |         if (string.IsNullOrEmpty(userCreationDTO.Username) || string.IsNullOrEmpty(userCreationDTO.Password) || string.I | 
|  | 0 | 48 |  |         { | 
|  | 0 | 49 |  |             throw new ValidationException("Something went wrong."); // Vague response to make it harder for hackers | 
|  |  | 50 |  |         } | 
|  |  | 51 |  |  | 
|  | 2 | 52 |  |         if (userCreationDTO.Password.Length < 8) | 
|  | 1 | 53 |  |         { | 
|  | 1 | 54 |  |             throw new ValidationException("Password must be at least 8 characters long."); | 
|  |  | 55 |  |         } | 
|  |  | 56 |  |  | 
|  |  | 57 |  |         // Check for the uniqueness of the username and register the user | 
|  |  | 58 |  |         try | 
|  | 1 | 59 |  |         { | 
|  | 1 | 60 |  |             var user = new User | 
|  | 1 | 61 |  |             { | 
|  | 1 | 62 |  |                 Username = userCreationDTO.Username, | 
|  | 1 | 63 |  |                 Password = userCreationDTO.Password, | 
|  | 1 | 64 |  |                 Email = userCreationDTO.Email, | 
|  | 1 | 65 |  |                 Role = "User", | 
|  | 1 | 66 |  |                 Age = userCreationDTO.Age | 
|  | 1 | 67 |  |             }; | 
|  |  | 68 |  |  | 
|  | 1 | 69 |  |             await _userDAO.RegisterUserAsync(user); | 
|  | 1 | 70 |  |         } | 
|  | 0 | 71 |  |         catch (Exception ex) | 
|  | 0 | 72 |  |         { | 
|  | 0 | 73 |  |             throw new Exception("Something went wrong."); | 
|  |  | 74 |  |         } | 
|  |  | 75 |  |  | 
|  | 1 | 76 |  |     } | 
|  |  | 77 |  |  | 
|  |  | 78 |  |     public async Task<List<List<User>>> GetAllUsersAsync() | 
|  | 1 | 79 |  |     { | 
|  |  | 80 |  |         try | 
|  | 1 | 81 |  |         { | 
|  | 1 | 82 |  |             List<User> adminList = new List<User>(); | 
|  | 1 | 83 |  |             List<User> superUserList = new List<User>(); | 
|  | 1 | 84 |  |             List<User> userList = new List<User>(); | 
|  | 1 | 85 |  |             List<List<User>> allUsersList = new List<List<User>>(); | 
|  |  | 86 |  |  | 
|  | 9 | 87 |  |             foreach (User user in await _userDAO.GetAllUsersAsync()) | 
|  | 3 | 88 |  |             { | 
|  | 3 | 89 |  |                 if (user.Role == "Admin") | 
|  | 1 | 90 |  |                 { | 
|  | 1 | 91 |  |                     adminList.Add(user); | 
|  | 3 | 92 |  |                 } else if (user.Role == "SuperUser") | 
|  | 1 | 93 |  |                 { | 
|  | 1 | 94 |  |                     superUserList.Add(user); | 
|  | 1 | 95 |  |                 } | 
|  |  | 96 |  |                 else | 
|  | 1 | 97 |  |                 { | 
|  | 1 | 98 |  |                     userList.Add(user); | 
|  | 1 | 99 |  |                 } | 
|  | 3 | 100 |  |             } | 
|  |  | 101 |  |  | 
|  | 1 | 102 |  |             allUsersList.Add(adminList); | 
|  | 1 | 103 |  |             allUsersList.Add(superUserList); | 
|  | 1 | 104 |  |             allUsersList.Add(userList); | 
|  |  | 105 |  |  | 
|  | 1 | 106 |  |             return allUsersList; | 
|  |  | 107 |  |         } | 
|  | 0 | 108 |  |         catch (Exception ex) | 
|  | 0 | 109 |  |         { | 
|  | 0 | 110 |  |             throw new Exception($"Failed to retrieve all users sorted by role: {ex.Message}", ex); | 
|  |  | 111 |  |         } | 
|  | 1 | 112 |  |     } | 
|  |  | 113 |  |  | 
|  |  | 114 |  |     public async Task<User> UpdateUserAsync(User user) | 
|  | 0 | 115 |  |     { | 
|  | 0 | 116 |  |         return await _userDAO.UpdateUserAsync(user); | 
|  | 0 | 117 |  |     } | 
|  |  | 118 |  |  | 
|  |  | 119 |  | } | 
|  |  | 120 |  | } |