Add user profile and avatar upload feature

This commit is contained in:
Aaron Po
2026-08-30 04:13:47 -04:00
parent bd78fe6533
commit d1d089aa73
15 changed files with 325 additions and 47 deletions

View File

@@ -0,0 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Http;
namespace Features.Auth.Commands.UploadAvatar;
/// <summary>Uploads a new avatar photo for a user account, replacing any existing avatar.</summary>
public record UploadAvatarCommand(Guid UserAccountId, IFormFile File) : IRequest<Guid>;

View File

@@ -0,0 +1,45 @@
using Domain.Entities;
using Features.Auth.Repository;
using Features.ImageUploads.Commands.UploadPhoto;
using MediatR;
namespace Features.Auth.Commands.UploadAvatar;
/// <summary>
/// Handles <see cref="UploadAvatarCommand" />: uploads the photo via <see cref="UploadPhotoCommand" />,
/// then saves the <see cref="UserAvatar" /> record, replacing any existing avatar for the user.
/// </summary>
public class UploadAvatarHandler(
IMediator mediator,
IUserAvatarRepository userAvatarRepository,
IUserProfileRepository userProfileRepository
) : IRequestHandler<UploadAvatarCommand, Guid>
{
public async Task<Guid> Handle(UploadAvatarCommand request, CancellationToken cancellationToken)
{
Guid photoId = await mediator.Send(
new UploadPhotoCommand(
request.UserAccountId,
$"avatars/{request.UserAccountId}",
request.File
),
cancellationToken
);
Guid profileId = await userProfileRepository.GetOrCreateProfileIdAsync(
request.UserAccountId,
cancellationToken
);
UserAvatar avatar = new()
{
UserAvatarId = Guid.NewGuid(),
UserProfileId = profileId,
PhotoId = photoId,
};
await userAvatarRepository.SaveAsync(avatar, cancellationToken);
return photoId;
}
}