2023-02-23 10:41:15 +01:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2023-03-03 12:03:13 +01:00
|
|
|
|
using ChoETL;
|
|
|
|
|
using Domain.Enums;
|
2023-02-23 10:41:15 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2023-02-21 19:55:32 +01:00
|
|
|
|
|
|
|
|
|
namespace Application.Api;
|
|
|
|
|
|
|
|
|
|
public record UnlockAllMusicCommand(long CardId) : IRequestWrapper<bool>;
|
|
|
|
|
|
|
|
|
|
public class UnlockAllMusicCommandHandler : RequestHandlerBase<UnlockAllMusicCommand, bool>
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<UnlockAllMusicCommandHandler> logger;
|
|
|
|
|
|
|
|
|
|
public UnlockAllMusicCommandHandler(ICardDependencyAggregate aggregate,
|
|
|
|
|
ILogger<UnlockAllMusicCommandHandler> logger) : base(aggregate)
|
|
|
|
|
{
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 10:41:15 +01:00
|
|
|
|
[SuppressMessage("ReSharper.DPA", "DPA0007: Large number of DB records")]
|
|
|
|
|
[SuppressMessage("ReSharper.DPA", "DPA0006: Large number of DB commands")]
|
2023-02-21 19:55:32 +01:00
|
|
|
|
public override async Task<ServiceResult<bool>> Handle(UnlockAllMusicCommand request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2023-03-03 12:03:13 +01:00
|
|
|
|
var exists = await CardDbContext.CardDetails.AnyAsync(
|
|
|
|
|
detail => detail.CardId == request.CardId, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (!exists)
|
2023-02-21 19:55:32 +01:00
|
|
|
|
{
|
|
|
|
|
logger.LogWarning("Attempt to unlock for card {Card} that does not exist or is empty!", request.CardId);
|
|
|
|
|
return ServiceResult.Failed<bool>(ServiceError.CustomMessage("Unlock failed"));
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 12:03:13 +01:00
|
|
|
|
var unlockables = Config.UnlockRewards
|
|
|
|
|
.Where(config => config.RewardType == RewardType.Music)
|
|
|
|
|
.Select(config => new CardDetailDto
|
|
|
|
|
{
|
|
|
|
|
CardId = request.CardId,
|
|
|
|
|
Pcol1 = 10,
|
|
|
|
|
Pcol2 = config.TargetId,
|
|
|
|
|
Pcol3 = 0,
|
|
|
|
|
LastPlayTenpoId = "1337",
|
|
|
|
|
LastPlayTime = DateTime.Now,
|
|
|
|
|
ScoreUi2 = 1,
|
|
|
|
|
ScoreUi6 = 1
|
|
|
|
|
}.DtoToCardDetail());
|
|
|
|
|
|
|
|
|
|
await CardDbContext.CardDetails.UpsertRange(unlockables).RunAsync(cancellationToken);
|
2023-02-21 19:55:32 +01:00
|
|
|
|
await CardDbContext.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
return new ServiceResult<bool>(true);
|
|
|
|
|
}
|
|
|
|
|
}
|