60 lines
2.3 KiB
C#
60 lines
2.3 KiB
C#
using Distribution.Data;
|
|
using Distribution.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Distribution.Features.DeliveryRide;
|
|
|
|
public class DeliveryTaskRepository(AppDBContext context) : IDeliveryTaskRepository
|
|
{
|
|
public async Task<DeliveryTask?> GetByIdAsync(Guid id) =>
|
|
await context.DeliveryTasks
|
|
.Include(t => t.ShopPO)
|
|
.ThenInclude(o => o.Store)
|
|
.Include(t => t.ShopPO)
|
|
.ThenInclude(o => o.Items.Where(i => i.IsActive))
|
|
.ThenInclude(i => i.Item)
|
|
.FirstOrDefaultAsync(t => t.Id == id);
|
|
|
|
public async Task<List<DeliveryTask>> GetByRideIdAsync(Guid rideId) =>
|
|
await context.DeliveryTasks
|
|
.Include(t => t.ShopPO)
|
|
.ThenInclude(o => o.Store)
|
|
.Include(t => t.ShopPO)
|
|
.ThenInclude(o => o.Items.Where(i => i.IsActive))
|
|
.ThenInclude(i => i.Item)
|
|
.Where(t => t.FK_RideId == rideId)
|
|
.OrderBy(t => t.Sequence)
|
|
.ToListAsync();
|
|
|
|
public Task<List<DeliveryTask>> GetAllAsync(DeliveryTaskStatus? status, int page, int pageSize)
|
|
{
|
|
var query = context.DeliveryTasks
|
|
.Include(t => t.ShopPO)
|
|
.ThenInclude(o => o.Store)
|
|
.Include(t => t.ShopPO)
|
|
.ThenInclude(o => o.Items.Where(i => i.IsActive))
|
|
.ThenInclude(i => i.Item)
|
|
.AsQueryable();
|
|
if (status.HasValue) query = query.Where(t => t.Status == status.Value);
|
|
return query.OrderByDescending(t => t.CreatedAt)
|
|
.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
|
|
}
|
|
|
|
public Task<int> GetCountAsync(DeliveryTaskStatus? status)
|
|
{
|
|
var query = context.DeliveryTasks.AsQueryable();
|
|
if (status.HasValue) query = query.Where(t => t.Status == status.Value);
|
|
return query.CountAsync();
|
|
}
|
|
|
|
public async Task<bool> ExistsForOrderAndRideAsync(Guid shopPOId, Guid rideId) =>
|
|
await context.DeliveryTasks
|
|
.AnyAsync(t => t.FK_ShopPOId == shopPOId && t.FK_RideId == rideId);
|
|
|
|
public async Task AddAsync(DeliveryTask task) =>
|
|
await context.DeliveryTasks.AddAsync(task);
|
|
|
|
public void Update(DeliveryTask task) =>
|
|
context.DeliveryTasks.Update(task);
|
|
}
|