Skip to content

Commit

Permalink
[Users management][Duy] Update users' self profile (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
duykasama authored Feb 16, 2024
1 parent 4d9391f commit 9798d18
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Domus.Api/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public async Task<IActionResult> GetUserSelfProfile(string token)
async () => await _userService.GetSelfProfile(token).ConfigureAwait(false)
).ConfigureAwait(false);
}

[AllowAnonymous]
[HttpPut("self-profile/{token}")]
public async Task<IActionResult> UpdateSelfProfile(UpdateSelfProfileRequest request, string token)
{
return await ExecuteServiceLogic(
async () => await _userService.UpdateSelfProfile(request, token).ConfigureAwait(false)
).ConfigureAwait(false);
}
}
25 changes: 25 additions & 0 deletions Domus.Service/Implementations/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,29 @@ private async Task EnsureRoleExistsAsync(string role)
await _roleManager.CreateAsync(new IdentityRole(role));
}
}

public async Task<ServiceActionResult> UpdateSelfProfile(UpdateSelfProfileRequest request, string token)
{
if (!_jwtService.IsValidToken(token))
throw new InvalidTokenException();

var userId = _jwtService.GetTokenClaim(token, TokenClaimConstants.SUBJECT)?.ToString() ?? throw new UserNotFoundException();
var user = await _userManager.Users.Where(u => u.Id == userId)
.FirstOrDefaultAsync() ?? throw new UserNotFoundException();

if (!string.IsNullOrEmpty(request.Email) && await _userRepository.ExistsAsync(u => u.Email == request.Email && u.Id != userId))
throw new UserAlreadyExistsException("The email is already in use");
if (!string.IsNullOrEmpty(request.UserName) && await _userRepository.ExistsAsync(u => u.UserName == request.UserName && u.Id != userId))
throw new UserAlreadyExistsException("The username is already in use");

user.Email = string.IsNullOrEmpty(request.Email) ? user.Email : request.Email;
user.UserName = string.IsNullOrEmpty(request.UserName) ? user.UserName : request.UserName;
user.PhoneNumber = string.IsNullOrEmpty(request.PhoneNumber) ? user.PhoneNumber : request.PhoneNumber;
user.ProfileImage = string.IsNullOrEmpty(request.ProfileImage) ? user.ProfileImage : request.ProfileImage;

await _userManager.UpdateAsync(user);
await _unitOfWork.CommitAsync();

return new ServiceActionResult(true) { Detail = "User profile updated successfully" };
}
}
1 change: 1 addition & 0 deletions Domus.Service/Interfaces/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public interface IUserService : IAutoRegisterable
Task<ServiceActionResult> GetAllUsers();
Task<ServiceActionResult> DeleteUser(string userId);
Task<ServiceActionResult> GetSelfProfile(string token);
Task<ServiceActionResult> UpdateSelfProfile(UpdateSelfProfileRequest request, string token);
}
12 changes: 12 additions & 0 deletions Domus.Service/Models/Requests/Users/UpdateSelfProfileRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Domus.Service.Models.Requests.Users;

public class UpdateSelfProfileRequest
{
public string? Email { get; set; }

public string? UserName { get; set; }

public string? PhoneNumber { get; set; }

public string? ProfileImage { get; set; }
}

0 comments on commit 9798d18

Please sign in to comment.