| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Microsoft.EntityFrameworkCore;
- using SharedKernel.Results;
- namespace Application.Features.Admin.Store.Game.Delete;
- public sealed class Handler(IAppDbContext db) : ICommandHandler<Command, Result>
- {
- public async Task<Result> Handle(Command request, CancellationToken ct)
- {
- if (request.IDs is null || request.IDs.Length == 0)
- {
- return Result.Failure(Error.Problem("Game.NoIDs", "삭제할 게임을 선택해 주세요."));
- }
- // 사용중 상품 존재 시 삭제 거부
- var usedGameIDs = await db.Product
- .Where(p => request.IDs.Contains(p.GameID))
- .Select(p => p.GameID)
- .Distinct()
- .ToListAsync(ct);
- if (usedGameIDs.Count > 0)
- {
- return Result.Failure(Error.Conflict("Game.InUse", $"상품에서 사용 중인 게임은 삭제할 수 없습니다. (해당 게임 ID: {string.Join(", ", usedGameIDs)})"));
- }
- // 사용중 쿠폰도 차단
- var usedByCoupons = await db.Coupon
- .Where(c => request.IDs.Contains(c.GameID))
- .Select(c => c.GameID)
- .Distinct()
- .ToListAsync(ct);
- if (usedByCoupons.Count > 0)
- {
- return Result.Failure(Error.Conflict("Game.InUseByCoupon", $"쿠폰에서 사용 중인 게임은 삭제할 수 없습니다. (해당 게임 ID: {string.Join(", ", usedByCoupons)})"));
- }
- var games = await db.Game.Where(c => request.IDs.Contains(c.ID)).ToListAsync(ct);
- if (games.Count == 0)
- {
- return Result.Failure(Error.NotFound("Game.NotFound", "삭제할 게임을 찾을 수 없습니다."));
- }
- db.Game.RemoveRange(games);
- await db.SaveChangesAsync(ct);
- return Result.Success();
- }
- }
|