TripAdminService.cs
2,344 bytes
| 1 | using SplitApp.WebApp.Application.DTO; |
|---|---|
| 2 | using SplitApp.WebApp.Application.Mappers; |
| 3 | using SplitApp.Modules.Trips.Domain.Entities; |
| 4 | using SplitApp.Modules.Trips.Domain.Enums; |
| 5 | using SplitApp.Modules.Expenses.Domain.Entities; |
| 6 | using SplitApp.Modules.Expenses.Domain.Enums; |
| 7 | using SplitApp.WebApp.Application.Contracts; |
| 8 | |
| 9 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 10 | |
| 11 | public class TripAdminService : ITripAdminService |
| 12 | { |
| 13 | private readonly IAppUnitOfWork _uow; |
| 14 | |
| 15 | public TripAdminService(IAppUnitOfWork uow) |
| 16 | { |
| 17 | _uow = uow; |
| 18 | } |
| 19 | |
| 20 | public async Task<List<TripBllDto>> GetAllAsync(string? search) |
| 21 | { |
| 22 | var items = (await _uow.Trips.GetAllAsync()).ToList(); |
| 23 | if (!string.IsNullOrEmpty(search)) |
| 24 | items = items.Where(t => t.Name.Contains(search)).ToList(); |
| 25 | return TripBllDtoFactory.CreateList(items.OrderByDescending(t => t.CreatedAt)); |
| 26 | } |
| 27 | |
| 28 | public async Task<List<CurrencyBllDto>> GetAllCurrenciesAsync() |
| 29 | { |
| 30 | var currencies = await _uow.GetRepository<Currency>().GetAllAsync(); |
| 31 | return CurrencyBllDtoFactory.CreateList(currencies); |
| 32 | } |
| 33 | |
| 34 | public async Task<TripBllDto?> GetByIdAsync(Guid id) |
| 35 | { |
| 36 | var trip = await _uow.Trips.GetByIdAsync(id); |
| 37 | return trip == null ? null : TripBllDtoFactory.Create(trip); |
| 38 | } |
| 39 | |
| 40 | public async Task CreateAsync(TripBllDto entity) |
| 41 | { |
| 42 | var domainEntity = TripBllDtoFactory.ToEntity(entity); |
| 43 | domainEntity.Id = Guid.NewGuid(); |
| 44 | _uow.Trips.Add(domainEntity); |
| 45 | await _uow.SaveChangesAsync(); |
| 46 | } |
| 47 | |
| 48 | public async Task UpdateAsync(TripBllDto entity) |
| 49 | { |
| 50 | var existing = await _uow.Trips.GetByIdAsync(entity.Id); |
| 51 | if (existing == null) return; |
| 52 | existing.Name = entity.Name; |
| 53 | existing.Description = entity.Description; |
| 54 | existing.Destination = entity.Destination; |
| 55 | existing.StartDate = entity.StartDate; |
| 56 | existing.EndDate = entity.EndDate; |
| 57 | existing.Status = entity.Status; |
| 58 | existing.DefaultCurrencyId = entity.DefaultCurrencyId; |
| 59 | _uow.Trips.Update(existing); |
| 60 | await _uow.SaveChangesAsync(); |
| 61 | } |
| 62 | |
| 63 | public async Task DeleteAsync(Guid id) |
| 64 | { |
| 65 | await _uow.Trips.RemoveAsync(id); |
| 66 | await _uow.SaveChangesAsync(); |
| 67 | } |
| 68 | |
| 69 | public Task<bool> ExistsAsync(Guid id) => _uow.Trips.ExistsAsync(id); |
| 70 | } |
| 71 | |