SettlementPlanRepository.cs
1,802 bytes
| 1 | using App.Domain; |
|---|---|
| 2 | using App.Domain.Contracts; |
| 3 | using Microsoft.EntityFrameworkCore; |
| 4 | |
| 5 | namespace App.DAL.EF.Repositories; |
| 6 | |
| 7 | public class SettlementPlanRepository : BaseRepository<SettlementPlan>, ISettlementPlanRepository |
| 8 | { |
| 9 | public SettlementPlanRepository(AppDbContext dbContext) : base(dbContext) |
| 10 | { |
| 11 | } |
| 12 | |
| 13 | public override async Task<IEnumerable<SettlementPlan>> GetAllAsync() |
| 14 | { |
| 15 | return await DbContext.SettlementPlans |
| 16 | .Include(sp => sp.Trip) |
| 17 | .Include(sp => sp.CreatedByUser) |
| 18 | .Include(sp => sp.Payments) |
| 19 | .ToListAsync(); |
| 20 | } |
| 21 | |
| 22 | public override async Task<SettlementPlan?> GetByIdAsync(Guid id) |
| 23 | { |
| 24 | return await DbContext.SettlementPlans |
| 25 | .Include(sp => sp.Trip) |
| 26 | .Include(sp => sp.CreatedByUser) |
| 27 | .Include(sp => sp.Payments!) |
| 28 | .ThenInclude(p => p.FromUser) |
| 29 | .Include(sp => sp.Payments!) |
| 30 | .ThenInclude(p => p.ToUser) |
| 31 | .FirstOrDefaultAsync(sp => sp.Id == id); |
| 32 | } |
| 33 | |
| 34 | public async Task<SettlementPlan?> GetLatestByTripIdAsync(Guid tripId) |
| 35 | { |
| 36 | return await DbContext.SettlementPlans |
| 37 | .Where(sp => sp.TripId == tripId) |
| 38 | .OrderByDescending(sp => sp.CreatedAt) |
| 39 | .Include(sp => sp.Payments!) |
| 40 | .ThenInclude(p => p.FromUser) |
| 41 | .Include(sp => sp.Payments!) |
| 42 | .ThenInclude(p => p.ToUser) |
| 43 | .FirstOrDefaultAsync(); |
| 44 | } |
| 45 | |
| 46 | public async Task DeletePlanWithPaymentsAsync(Guid planId) |
| 47 | { |
| 48 | await DbContext.Set<SettlementPayment>() |
| 49 | .Where(p => p.SettlementPlanId == planId) |
| 50 | .ExecuteDeleteAsync(); |
| 51 | await DbContext.SettlementPlans |
| 52 | .Where(sp => sp.Id == planId) |
| 53 | .ExecuteDeleteAsync(); |
| 54 | } |
| 55 | } |
| 56 | |