diff --git a/backend/Src/PIED_LMS.Application/UserCases/Commands/Identity/UpdateProfileHandler.cs b/backend/Src/PIED_LMS.Application/UserCases/Commands/Identity/UpdateProfileHandler.cs index f3510209..00fa0f02 100644 --- a/backend/Src/PIED_LMS.Application/UserCases/Commands/Identity/UpdateProfileHandler.cs +++ b/backend/Src/PIED_LMS.Application/UserCases/Commands/Identity/UpdateProfileHandler.cs @@ -7,14 +7,14 @@ namespace PIED_LMS.Application.UserCases.Commands.Identity; public class UpdateProfileHandler( UserManager userManager, IFileStorageService fileStorageService, - ILogger logger) : IRequestHandler> + ILogger logger) : IRequestHandler> { - public async Task> Handle(UpdateProfileCommand request, CancellationToken cancellationToken) + public async Task> Handle(UpdateProfileCommand request, CancellationToken cancellationToken) { try { var user = await userManager.FindByIdAsync(request.UserId.ToString()); - if (user is null) return new ServiceResponse(false, "User not found"); + if (user is null) return new ServiceResponse(false, "User not found"); if (!string.IsNullOrEmpty(request.FirstName)) user.FirstName = request.FirstName; @@ -60,15 +60,42 @@ public async Task> Handle(UpdateProfileCommand request, var errors = result.Errors.Select(e => e.Description).ToList(); logger.LogWarning("Failed to update profile for user {UserId}: {Errors}", user.Id, string.Join(", ", errors)); - return new ServiceResponse(false, "Failed to update profile"); + return new ServiceResponse(false, "Failed to update profile"); } + + var roles = await userManager.GetRolesAsync(user); + string? profilePicUrl = null; + if (!string.IsNullOrWhiteSpace(user.ProfilePictureUrl)) + { + try + { + profilePicUrl = await fileStorageService.GetFileUrlAsync(user.ProfilePictureUrl); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Failed to resolve profile picture URL for user {UserId}. Key: {Key}", user.Id, + user.ProfilePictureUrl); + } + } + + var userDto = new UserDto( + user.Id, + user.Email ?? string.Empty, + user.FirstName, + user.LastName, + user.IsActive, + user.CreatedAt, + roles.ToList(), + user.Bio, + profilePicUrl + ); - return new ServiceResponse(true, "Profile updated successfully"); + return new ServiceResponse(true, "Profile updated successfully", userDto); } catch (Exception ex) { logger.LogError(ex, "Error occurred while updating profile for user {UserId}", request.UserId); - return new ServiceResponse(false, "An error occurred while updating the profile"); + return new ServiceResponse(false, "An error occurred while updating the profile"); } } } diff --git a/backend/Src/PIED_LMS.Contract/Services/Identity/Command.cs b/backend/Src/PIED_LMS.Contract/Services/Identity/Command.cs index 21d7be43..2a46c3e9 100644 --- a/backend/Src/PIED_LMS.Contract/Services/Identity/Command.cs +++ b/backend/Src/PIED_LMS.Contract/Services/Identity/Command.cs @@ -90,7 +90,7 @@ public record UpdateProfileCommand( string? LastName, string? Bio, IFormFile? ProfilePicture -) : IRequest>; +) : IRequest>; public sealed record UpdateProfileRequest { diff --git a/backend/Src/PIED_LMS.Presentation/APIs/AuthenticationEndpoints.cs b/backend/Src/PIED_LMS.Presentation/APIs/AuthenticationEndpoints.cs index ff2c30b1..8e13537f 100644 --- a/backend/Src/PIED_LMS.Presentation/APIs/AuthenticationEndpoints.cs +++ b/backend/Src/PIED_LMS.Presentation/APIs/AuthenticationEndpoints.cs @@ -51,7 +51,7 @@ public void AddRoutes(IEndpointRouteBuilder app) .WithName("UpdateProfile") .RequireAuthorization() .DisableAntiforgery() - .WithServiceResponseOpenApi(ServiceResponseStatusProfile.OkOrBadRequest); + .WithServiceResponseOpenApi(ServiceResponseStatusProfile.OkOrBadRequest); group.MapGet("/me", GetMe) .WithName("GetMe") diff --git a/backend/Src/PIED_LMS.Presentation/Extensions/EndpointExtensions.cs b/backend/Src/PIED_LMS.Presentation/Extensions/EndpointExtensions.cs index 8cc73a52..79d9f3e8 100644 --- a/backend/Src/PIED_LMS.Presentation/Extensions/EndpointExtensions.cs +++ b/backend/Src/PIED_LMS.Presentation/Extensions/EndpointExtensions.cs @@ -8,9 +8,6 @@ public static IResult ToActionResult(this ServiceResponse result, HttpCont { if (result.Success) { - if (result.Data is null && typeof(T).IsClass) - return Results.NotFound(result); - return Results.Ok(result); } @@ -25,7 +22,7 @@ public static IResult ToActionResult(this ServiceResponse result, HttpCont return Results.Json(result, statusCode: StatusCodes.Status401Unauthorized); if (result.ErrorCode == "FORBIDDEN" || result.ErrorCode == "ACCESS_DENIED" || - (result.Message.Contains("authorized") || result.Message.Contains("permission"))) + (result.Message is not null && (result.Message.Contains("authorized") || result.Message.Contains("permission")))) return Results.Json(result, statusCode: StatusCodes.Status403Forbidden); return result.ErrorCode switch