| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Domain.Entities.Store.ValueObject;
- using Microsoft.EntityFrameworkCore;
- using SharedKernel.Results;
- namespace Application.Features.Admin.Store.Coupon.Get;
- public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Result<Response>>
- {
- public async Task<Result<Response>> Handle(Query request, CancellationToken ct)
- {
- var coupon = await db.Coupon
- .AsNoTracking()
- .Include(c => c.Product)
- .Include(c => c.Game)
- .Where(c => c.ID == request.ID)
- .Select(c => new Response(
- c.ID,
- c.ProductID,
- c.Product!.Name,
- c.GameID,
- c.Game!.KorName,
- c.Name,
- c.UsagePolicy,
- c.ExpiresAt,
- c.GeneratedCount,
- c.UsedCount,
- db.CouponCode.Count(cc => cc.CouponID == c.ID && cc.Status == CouponCodeStatus.Available),
- c.LowStockThreshold,
- c.CreatedAt,
- c.UpdatedAt
- ))
- .FirstOrDefaultAsync(ct);
- if (coupon is null)
- {
- return Result.Failure<Response>(Error.NotFound("Coupon.NotFound", "쿠폰을 찾을 수 없습니다."));
- }
- return Result.Success(coupon);
- }
- }
|