44 lines
1.7 KiB
C#
44 lines
1.7 KiB
C#
using MediatR;
|
|
using Modules.Media.Api.Commands;
|
|
using Modules.User.Application.Gateways;
|
|
|
|
namespace Modules.User.Application.Commands;
|
|
|
|
public class SetUserAvatarCommand : IRequest<Unit>
|
|
{
|
|
public string Extension { get; set; } = default!;
|
|
public string ContentType { get; set; } = default!;
|
|
public Stream Data { get; set; } = default!;
|
|
}
|
|
|
|
public class SetUserAvatarCommandHandler(UserContext userContext, IUserGateway userGateway, IMediator mediator) : IRequestHandler<SetUserAvatarCommand, Unit>
|
|
{
|
|
public async Task<Unit> Handle(SetUserAvatarCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var userInfo = await userContext.GetUserInfo(cancellationToken);
|
|
if (userInfo?.AccountId == null) throw new Exception("User not found");
|
|
var user = await userGateway.Get(userInfo.AccountId.Value) ?? throw new Exception("User was not found");
|
|
|
|
var newAvatarId = await mediator.Send(new AddObjectCommand
|
|
{
|
|
ObjectName = !string.IsNullOrWhiteSpace(user.AvatarId) ? user.AvatarId : null,
|
|
BusketName = "my-bookmark.user",
|
|
ObjectPrefix = "avatar",
|
|
ObjectExtension = request.Extension,
|
|
ContentType = request.ContentType,
|
|
Data = request.Data,
|
|
}, cancellationToken);
|
|
user.SetAvatar(newAvatarId);
|
|
await userGateway.UpdateUser(user.Id, new Models.User
|
|
{
|
|
NickName = user.NickName,
|
|
FirstName = user.FirstName,
|
|
Patronymic = user.Patronymic,
|
|
LastName = user.LastName,
|
|
BirthDate = user.BirthDate,
|
|
LanguageId = user.LanguageId,
|
|
}, user.AvatarId);
|
|
|
|
return Unit.Value;
|
|
}
|
|
} |