Skip to content

Commit 902a1df

Browse files
feat(users): add profile update endpoint with partial updates
Implement PATCH /profile endpoint to allow users to update their profile information with partial updates. The endpoint uses a new UpdateUserProfileCommand and handler to process updates only for provided fields while maintaining data integrity and authentication checks.
1 parent 397519d commit 902a1df

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

Controllers/UsersController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,10 @@ public async Task<IActionResult> GetProfile()
3333
var profile = await _mediator.Send(new GetUserProfileQuery());
3434
return OK(profile);
3535
}
36+
[HttpPatch("profile")]
37+
public async Task<IActionResult> UpdateProfile([FromBody] UpdateUserProfileCommand command)
38+
{
39+
var result = await _mediator.Send(command);
40+
return OK(result);
41+
}
3642
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using PaymentCoreServiceApi.Common.Mediator;
2+
using PaymentCoreServiceApi.Core.Entities.UserGenerated;
3+
4+
namespace PaymentCoreServiceApi.Features.Users.Commands;
5+
6+
public record UpdateUserProfileCommand : IRequestApiResponse<User>
7+
{
8+
public string? NickName { get; set; }
9+
public string? Avatar { get; set; }
10+
public int? Gender { get; set; }
11+
public DateTime? BirthDate { get; set; }
12+
public int? Age { get; set; }
13+
public string? Email { get; set; }
14+
public string? PhoneNumber { get; set; }
15+
public string? Address { get; set; }
16+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using AutoMapper;
2+
using Microsoft.EntityFrameworkCore;
3+
using PaymentCoreServiceApi.Common;
4+
using PaymentCoreServiceApi.Common.Mediator;
5+
using PaymentCoreServiceApi.Core.Entities.UserGenerated;
6+
using PaymentCoreServiceApi.Infrastructure.DbContexts;
7+
using PaymentCoreServiceApi.Services;
8+
9+
namespace PaymentCoreServiceApi.Features.Users.Commands;
10+
11+
public class UpdateUserProfileCommandHandler : IRequestApiResponseHandler<UpdateUserProfileCommand, User>
12+
{
13+
private readonly AppDbContext _context;
14+
private readonly IExecutionContext _currentUser;
15+
16+
public UpdateUserProfileCommandHandler(AppDbContext context, IExecutionContext currentUser)
17+
{
18+
_context = context;
19+
_currentUser = currentUser;
20+
}
21+
22+
public async Task<ApiResponse<User>> Handle(UpdateUserProfileCommand request, CancellationToken cancellationToken)
23+
{
24+
if (!_currentUser.IsAuthenticated)
25+
{
26+
return ApiResponse<User>.Unauthorized("User is not authenticated");
27+
}
28+
29+
var user = await _context.Users
30+
.FirstOrDefaultAsync(u => u.Id == _currentUser.Id, cancellationToken);
31+
32+
if (user == null)
33+
{
34+
return ApiResponse<User>.NotFound($"User with ID {_currentUser.Id} not found");
35+
}
36+
37+
// Update only provided fields
38+
if (!string.IsNullOrEmpty(request.NickName))
39+
user.NickName = request.NickName;
40+
41+
if (!string.IsNullOrEmpty(request.Avatar))
42+
user.Avatar = request.Avatar;
43+
44+
if (request.Gender.HasValue)
45+
user.Gender = request.Gender;
46+
47+
if (request.BirthDate.HasValue)
48+
user.BirthDate = request.BirthDate;
49+
50+
if (request.Age.HasValue)
51+
user.Age = request.Age.Value;
52+
53+
if (!string.IsNullOrEmpty(request.Email))
54+
user.Email = request.Email;
55+
56+
if (!string.IsNullOrEmpty(request.PhoneNumber))
57+
user.PhoneNumber = request.PhoneNumber;
58+
59+
if (!string.IsNullOrEmpty(request.Address))
60+
user.Address = request.Address;
61+
62+
user.UpdatedAt = DateTime.UtcNow;
63+
user.UpdatedBy = _currentUser.Id;
64+
65+
try
66+
{
67+
await _context.SaveChangesAsync(cancellationToken);
68+
return ApiResponse<User>.Success(user, "Profile updated successfully");
69+
}
70+
catch (Exception ex)
71+
{
72+
return ApiResponse<User>.InternalServerError($"Failed to update profile: {ex.Message}");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)