SettlementPlanAdminService.cs
2,696 bytes
| 1 | using SplitApp.WebApp.Application.Contracts; |
|---|---|
| 2 | using SplitApp.WebApp.Application.DTO; |
| 3 | using SplitApp.WebApp.Application.Mappers; |
| 4 | using SplitApp.WebApp.Application.UsersService; |
| 5 | |
| 6 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 7 | |
| 8 | public class SettlementPlanAdminService : ISettlementPlanAdminService |
| 9 | { |
| 10 | private readonly IAppUnitOfWork _uow; |
| 11 | private readonly IUsersServiceClient _usersService; |
| 12 | |
| 13 | public SettlementPlanAdminService(IAppUnitOfWork uow, IUsersServiceClient usersService) |
| 14 | { |
| 15 | _uow = uow; |
| 16 | _usersService = usersService; |
| 17 | } |
| 18 | |
| 19 | public async Task<List<SettlementPlanBllDto>> GetAllAsync(Guid? tripId) |
| 20 | { |
| 21 | var plans = (await _uow.SettlementPlans.GetAllAsync()).ToList(); |
| 22 | if (tripId.HasValue) |
| 23 | plans = plans.Where(s => s.TripId == tripId.Value).ToList(); |
| 24 | return SettlementBllDtoFactory.CreateList(plans.OrderByDescending(s => s.Id)); |
| 25 | } |
| 26 | |
| 27 | public async Task<SettlementPlanBllDto?> GetByIdAsync(Guid id) |
| 28 | { |
| 29 | var entity = await _uow.SettlementPlans.GetByIdAsync(id); |
| 30 | return entity == null ? null : SettlementBllDtoFactory.Create(entity); |
| 31 | } |
| 32 | |
| 33 | public async Task CreateAsync(SettlementPlanBllDto entity) |
| 34 | { |
| 35 | var domainEntity = SettlementBllDtoFactory.ToEntity(entity); |
| 36 | domainEntity.Id = Guid.NewGuid(); |
| 37 | _uow.SettlementPlans.Add(domainEntity); |
| 38 | await _uow.SaveChangesAsync(); |
| 39 | } |
| 40 | |
| 41 | public async Task UpdateAsync(SettlementPlanBllDto entity) |
| 42 | { |
| 43 | var existing = await _uow.SettlementPlans.GetByIdAsync(entity.Id); |
| 44 | if (existing == null) return; |
| 45 | existing.TripId = entity.TripId; |
| 46 | existing.CreatedByUserId = entity.CreatedByUserId; |
| 47 | existing.TotalAmount = entity.TotalAmount; |
| 48 | existing.Status = entity.Status; |
| 49 | existing.CompletedAt = entity.CompletedAt; |
| 50 | _uow.SettlementPlans.Update(existing); |
| 51 | await _uow.SaveChangesAsync(); |
| 52 | } |
| 53 | |
| 54 | public async Task DeleteAsync(Guid id) |
| 55 | { |
| 56 | await _uow.SettlementPlans.RemoveAsync(id); |
| 57 | await _uow.SaveChangesAsync(); |
| 58 | } |
| 59 | |
| 60 | public Task<bool> ExistsAsync(Guid id) => _uow.SettlementPlans.ExistsAsync(id); |
| 61 | |
| 62 | public async Task<List<TripBllDto>> GetTripsAsync() |
| 63 | { |
| 64 | var trips = await _uow.Trips.GetAllAsync(); |
| 65 | return TripBllDtoFactory.CreateList(trips.OrderBy(t => t.Name)); |
| 66 | } |
| 67 | |
| 68 | public async Task<List<AppUserBllDto>> GetUsersAsync() |
| 69 | { |
| 70 | var users = await _usersService.ListUsersAsync(); |
| 71 | return users.Select(u => new AppUserBllDto |
| 72 | { |
| 73 | Id = u.Id, |
| 74 | FirstName = u.FirstName, |
| 75 | LastName = u.LastName, |
| 76 | Email = u.Email, |
| 77 | }).ToList(); |
| 78 | } |
| 79 | } |
| 80 | |