/// <summary> /// Create a new user account. Some crude validation will take place to ensure it is valid. /// </summary> /// <param name="userName">The username of the account.</param> /// <param name="password">The password of the account.</param> /// <returns>A response containing success/failure of the operation and an error message if one occurs.</returns> public async Task <UserResponse> Create(string userName, string password) { int nextUserId; User newUser; lock (_newUserLock) { if (!_usersValidator.IsPasswordValid(password, out string errorMessage)) { return(UserResponse.CreateFailedResult(errorMessage)); } if (!_usersValidator.IsUsernameUnique(password, _context, out errorMessage)) { return(UserResponse.CreateFailedResult(errorMessage)); } _currentMaxUserId++; nextUserId = _currentMaxUserId; } newUser = new User(nextUserId, userName, password); _context.Add(newUser.UserId, newUser); return(UserResponse.CreateSuccessfulResult(newUser)); }
/// <summary> /// Delete a user account. /// </summary> /// <param name="userId">The ID of the user to delete.</param> /// <returns>A response containing success/failure of the operation and an error message if one occurs.</returns> public async Task <UserResponse> Delete(int userId) { if (!_context.TryGetValue(userId, out User existingUser)) { return(UserResponse.CreateFailedResult($"Could not find user with ID = {userId} to delete.")); } _context.Remove(userId); return(UserResponse.CreateSuccessfulResult(User.Empty)); }
/// <summary> /// Update the details of a registered user. The properties that can be modified are the username /// and the password. /// </summary> /// <param name="user">The user details of the account to be modified.</param> /// <returns>A response containing success/failure of the operation and an error message if one occurs.</returns> public async Task <UserResponse> Update(User user) { if (!_context.TryGetValue(user.UserId, out User existingUser)) { return(UserResponse.CreateFailedResult($"Could not find user with ID = {user.UserId} to update.")); } if (!_usersValidator.IsUsernameUnique(user.Password, _context, out string errorMessage)) { return(UserResponse.CreateFailedResult(errorMessage)); } _context[user.UserId] = user; return(UserResponse.CreateSuccessfulResult(user)); }