1
0
mirror of synced 2024-12-18 17:25:54 +01:00
GC-local-server-rewrite/Application/Api/SetFavoriteMusicCommand.cs
2023-02-17 00:38:01 +08:00

39 lines
1.7 KiB
C#

using Microsoft.Extensions.Logging;
namespace Application.Api;
public record SetFavoriteMusicCommand(MusicDetailDto Data) : IRequestWrapper<bool>;
public class SetFavoriteMusicCommandHandler : RequestHandlerBase<SetFavoriteMusicCommand, bool>
{
private readonly ILogger<SetFavoriteMusicCommandHandler> logger;
public SetFavoriteMusicCommandHandler(ICardDependencyAggregate aggregate, ILogger<SetFavoriteMusicCommandHandler> logger) : base(aggregate)
{
this.logger = logger;
}
public override async Task<ServiceResult<bool>> Handle(SetFavoriteMusicCommand request,
CancellationToken cancellationToken)
{
var musicDetail = await CardDbContext.CardDetails.FirstOrDefaultAsync(detail =>
detail.CardId == request.Data.CardId &&
detail.Pcol1 == 10 &&
detail.Pcol2 == request.Data.MusicId &&
detail.Pcol3 == 0,
cancellationToken);
if (musicDetail is null)
{
logger.LogWarning("Attempt to set favorite for non existing music, card id: {CardId}, music id: {MusicId}",
request.Data.CardId, request.Data.MusicId);
return ServiceResult.Failed<bool>(ServiceError.CustomMessage("Music record not found"));
}
musicDetail.Fcol1 = request.Data.IsFavorite ? 1 : 0;
CardDbContext.CardDetails.Update(musicDetail);
var count = await CardDbContext.SaveChangesAsync(cancellationToken);
return count == 1 ? new ServiceResult<bool>(true) : ServiceResult.Failed<bool>(ServiceError.DatabaseSaveFailed);
}
}