Handler.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Domain.Entities.Store.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. using SharedKernel.Results;
  6. namespace Application.Features.Admin.Store.Coupon.Get;
  7. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
  8. {
  9. public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
  10. {
  11. var coupon = await db.Coupon
  12. .AsNoTracking()
  13. .Include(c => c.Product)
  14. .Include(c => c.Game)
  15. .Where(c => c.ID == request.ID)
  16. .Select(c => new Response(
  17. c.ID,
  18. c.ProductID,
  19. c.Product!.Name,
  20. c.GameID,
  21. c.Game!.KorName,
  22. c.Name,
  23. c.UsagePolicy,
  24. c.ExpiresAt,
  25. c.GeneratedCount,
  26. c.UsedCount,
  27. db.CouponCode.Count(cc => cc.CouponID == c.ID && cc.Status == CouponCodeStatus.Available),
  28. c.LowStockThreshold,
  29. c.CreatedAt,
  30. c.UpdatedAt
  31. ))
  32. .FirstOrDefaultAsync(ct);
  33. if (coupon is null)
  34. {
  35. return Result.Failure<Response>(Error.NotFound("Coupon.NotFound", "쿠폰을 찾을 수 없습니다."));
  36. }
  37. return Result.Success(coupon);
  38. }
  39. }