Files
CMS/src/CMSMicroservice.Application/DiscountShopCQ/Queries/GetAllDiscountOrders/GetAllDiscountOrdersQueryHandler.cs
T
masoodafar-web f0117eb1d5
Build and Deploy to Kubernetes / build-and-deploy (push) Successful in 1m53s
Add migration for DiscountProductImages table
- Created a new table `DiscountProductImages` in the CMS schema.
- Added columns for image details including `Title`, `AltText`, `ImagePath`, `ThumbnailPath`, `SortOrder`, `IsActive`, `Created`, `CreatedBy`, `LastModified`, `LastModifiedBy`, and `IsDeleted`.
- Established a foreign key relationship with the `DiscountProducts` table.
- Created indexes on `DiscountProductId` and a composite index on `DiscountProductId` and `SortOrder`.
2026-01-01 02:26:33 +03:30

125 lines
4.4 KiB
C#

using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Application.Common.Models;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetAllDiscountOrders;
public class GetAllDiscountOrdersQueryHandler : IRequestHandler<GetAllDiscountOrdersQuery, GetAllDiscountOrdersResponseDto>
{
private readonly IApplicationDbContext _context;
public GetAllDiscountOrdersQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetAllDiscountOrdersResponseDto> Handle(GetAllDiscountOrdersQuery request, CancellationToken cancellationToken)
{
var query = _context.DiscountOrders
.Include(o => o.User)
.Include(o => o.UserAddress)
.AsQueryable();
// فیلتر بر اساس شناسه کاربر
if (request.UserId.HasValue)
{
query = query.Where(o => o.UserId == request.UserId.Value);
}
// فیلتر بر اساس وضعیت پرداخت
if (request.PaymentStatus.HasValue)
{
query = query.Where(o => o.PaymentStatus == request.PaymentStatus.Value);
}
// فیلتر بر اساس وضعیت ارسال
if (request.DeliveryStatus.HasValue)
{
query = query.Where(o => o.DeliveryStatus == request.DeliveryStatus.Value);
}
// جستجو بر اساس موبایل کاربر
if (!string.IsNullOrWhiteSpace(request.UserMobile))
{
query = query.Where(o => o.User.Mobile.Contains(request.UserMobile));
}
// جستجو بر اساس کد رهگیری
if (!string.IsNullOrWhiteSpace(request.TrackingCode))
{
query = query.Where(o => o.TrackingCode != null && o.TrackingCode.Contains(request.TrackingCode));
}
// فیلتر از تاریخ
if (request.FromDate.HasValue)
{
query = query.Where(o => o.Created >= request.FromDate.Value);
}
// فیلتر تا تاریخ
if (request.ToDate.HasValue)
{
query = query.Where(o => o.Created <= request.ToDate.Value);
}
// فیلتر حداقل مبلغ
if (request.MinAmount.HasValue)
{
query = query.Where(o => o.TotalAmount >= request.MinAmount.Value);
}
// فیلتر حداکثر مبلغ
if (request.MaxAmount.HasValue)
{
query = query.Where(o => o.TotalAmount <= request.MaxAmount.Value);
}
var totalCount = await query.CountAsync(cancellationToken);
// Apply pagination
var pagination = request.PaginationQuery ?? new PaginationState { PageNumber = 1, PageSize = 20 };
var orders = await query
.OrderByDescending(o => o.Created)
.Skip((pagination.PageNumber - 1) * pagination.PageSize)
.Take(pagination.PageSize)
.Select(o => new AdminOrderDto
{
Id = o.Id,
UserId = o.UserId,
UserFullName = (o.User.FirstName ?? "") + " " + (o.User.LastName ?? ""),
UserMobile = o.User.Mobile,
TotalAmount = o.TotalAmount,
DiscountBalanceUsed = o.DiscountBalanceUsed,
GatewayAmountPaid = o.GatewayAmountPaid,
VatAmount = o.VatAmount,
PaymentStatus = o.PaymentStatus,
PaymentDate = o.PaymentDate,
DeliveryStatus = o.DeliveryStatus,
DeliveryDate = null, // TODO: Add DeliveryDate to DiscountOrder if needed
ShippingAddress = o.UserAddress.Address,
ReceiverName = o.UserAddress.Title,
ReceiverMobile = o.User.Mobile,
TrackingCode = o.TrackingCode,
AdminNote = o.DeliveryDescription,
Created = o.Created,
LastModified = o.LastModified,
ItemsCount = o.OrderDetails.Count
})
.ToListAsync(cancellationToken);
return new GetAllDiscountOrdersResponseDto
{
MetaData = new MetaData
{
TotalCount = totalCount,
PageSize = pagination.PageSize,
CurrentPage = pagination.PageNumber,
TotalPage = (int)Math.Ceiling(totalCount / (double)pagination.PageSize)
},
Models = orders
};
}
}