Handler.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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.Product.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 product = await db.Product
  12. .AsNoTracking()
  13. .Include(c => c.Game)
  14. .Where(c => c.ID == request.ID)
  15. .Select(c => new Response(
  16. c.ID,
  17. c.GameID ?? 0,
  18. c.Game!.KorName,
  19. c.Name,
  20. c.Description,
  21. c.Thumbnail,
  22. c.Type,
  23. c.Price,
  24. c.DiscountType,
  25. c.DiscountValue,
  26. c.DiscountType == DiscountType.Percent
  27. ? (c.Price - (c.Price * c.DiscountValue / 100) < 0 ? 0 : c.Price - (c.Price * c.DiscountValue / 100))
  28. : c.DiscountType == DiscountType.Amount
  29. ? (c.Price - c.DiscountValue < 0 ? 0 : c.Price - c.DiscountValue)
  30. : c.Price,
  31. c.Stock,
  32. c.MinPurchase,
  33. c.MaxPurchase,
  34. c.IsActive,
  35. c.Order,
  36. c.SaleStartAt,
  37. c.SaleEndAt,
  38. c.CreatedAt,
  39. c.UpdatedAt,
  40. c.RequireDonationChannel,
  41. c.ItemKind,
  42. c.DurationDays,
  43. c.EffectPayload,
  44. c.IsGiftable
  45. ))
  46. .FirstOrDefaultAsync(ct);
  47. if (product is null)
  48. {
  49. return Result.Failure<Response>(Error.NotFound("Product.NotFound", "상품을 찾을 수 없습니다."));
  50. }
  51. return Result.Success(product);
  52. }
  53. }