mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-14 17:54:42 +00:00
43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using Domain.Entities;
|
|
using Features.ImageUploads.Commands.UploadPhoto;
|
|
using Features.Users.Repository;
|
|
using MediatR;
|
|
|
|
namespace Features.Users.Commands.Profile.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, 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.GetProfileIdAsync(
|
|
request.UserAccountId,
|
|
cancellationToken
|
|
);
|
|
|
|
UserAvatar avatar = new()
|
|
{
|
|
UserAvatarId = Guid.NewGuid(),
|
|
UserProfileId = profileId,
|
|
PhotoId = photoId,
|
|
};
|
|
|
|
await userProfileRepository.SaveAvatarAsync(avatar, cancellationToken);
|
|
|
|
return photoId;
|
|
}
|
|
}
|