SettlementPaymentAdminService.cs
2,765 bytes
| 1 | using SplitApp.WebApp.Application.DTO; |
|---|---|
| 2 | using SplitApp.WebApp.Application.Mappers; |
| 3 | using SplitApp.WebApp.Application.Contracts; |
| 4 | |
| 5 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 6 | |
| 7 | public class SettlementPaymentAdminService : ISettlementPaymentAdminService |
| 8 | { |
| 9 | private readonly IAppUnitOfWork _uow; |
| 10 | |
| 11 | public SettlementPaymentAdminService(IAppUnitOfWork uow) |
| 12 | { |
| 13 | _uow = uow; |
| 14 | } |
| 15 | |
| 16 | public async Task<List<SettlementPaymentBllDto>> GetAllAsync(string? search) |
| 17 | { |
| 18 | var payments = (await _uow.SettlementPayments.GetAllAsync()).ToList(); |
| 19 | if (!string.IsNullOrEmpty(search)) |
| 20 | payments = payments.Where(s => |
| 21 | (s.FromUser?.Email != null && s.FromUser.Email.Contains(search)) || |
| 22 | (s.ToUser?.Email != null && s.ToUser.Email.Contains(search))).ToList(); |
| 23 | return SettlementPaymentBllDtoFactory.CreateList(payments.OrderByDescending(s => s.Id)); |
| 24 | } |
| 25 | |
| 26 | public async Task<SettlementPaymentBllDto?> GetByIdAsync(Guid id) |
| 27 | { |
| 28 | var entity = await _uow.SettlementPayments.GetByIdAsync(id); |
| 29 | return entity == null ? null : SettlementPaymentBllDtoFactory.Create(entity); |
| 30 | } |
| 31 | |
| 32 | public async Task CreateAsync(SettlementPaymentBllDto entity) |
| 33 | { |
| 34 | var domainEntity = SettlementPaymentBllDtoFactory.ToEntity(entity); |
| 35 | domainEntity.Id = Guid.NewGuid(); |
| 36 | _uow.SettlementPayments.Add(domainEntity); |
| 37 | await _uow.SaveChangesAsync(); |
| 38 | } |
| 39 | |
| 40 | public async Task UpdateAsync(SettlementPaymentBllDto entity) |
| 41 | { |
| 42 | var existing = await _uow.SettlementPayments.GetByIdAsync(entity.Id); |
| 43 | if (existing == null) return; |
| 44 | existing.SettlementPlanId = entity.SettlementPlanId; |
| 45 | existing.FromUserId = entity.FromUserId; |
| 46 | existing.ToUserId = entity.ToUserId; |
| 47 | existing.Amount = entity.Amount; |
| 48 | existing.Status = entity.Status; |
| 49 | existing.MarkedPaidAt = entity.MarkedPaidAt; |
| 50 | existing.ConfirmedAt = entity.ConfirmedAt; |
| 51 | _uow.SettlementPayments.Update(existing); |
| 52 | await _uow.SaveChangesAsync(); |
| 53 | } |
| 54 | |
| 55 | public async Task DeleteAsync(Guid id) |
| 56 | { |
| 57 | await _uow.SettlementPayments.RemoveAsync(id); |
| 58 | await _uow.SaveChangesAsync(); |
| 59 | } |
| 60 | |
| 61 | public async Task<List<SettlementPlanBllDto>> GetSettlementPlansAsync() |
| 62 | { |
| 63 | var plans = await _uow.SettlementPlans.GetAllAsync(); |
| 64 | return SettlementBllDtoFactory.CreateList(plans); |
| 65 | } |
| 66 | |
| 67 | public async Task<List<AppUserBllDto>> GetUsersAsync() |
| 68 | { |
| 69 | var users = await _uow.Users.GetAllAsync(); |
| 70 | return users.Select(u => new AppUserBllDto |
| 71 | { |
| 72 | Id = u.Id, |
| 73 | FirstName = u.FirstName, |
| 74 | LastName = u.LastName, |
| 75 | Email = u.Email |
| 76 | }).ToList(); |
| 77 | } |
| 78 | } |
| 79 | |