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