ExpenseAdminService.cs
3,429 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 | using SplitApp.Modules.Users.Domain.Entities; |
| 10 | |
| 11 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 12 | |
| 13 | public class ExpenseAdminService : IExpenseAdminService |
| 14 | { |
| 15 | private readonly IAppUnitOfWork _uow; |
| 16 | |
| 17 | public ExpenseAdminService(IAppUnitOfWork uow) |
| 18 | { |
| 19 | _uow = uow; |
| 20 | } |
| 21 | |
| 22 | public async Task<List<ExpenseBllDto>> GetAllAsync(Guid? tripId, string? search) |
| 23 | { |
| 24 | var items = (await _uow.Expenses.GetAllAsync()).ToList(); |
| 25 | if (tripId.HasValue) |
| 26 | items = items.Where(e => e.TripId == tripId.Value).ToList(); |
| 27 | if (!string.IsNullOrEmpty(search)) |
| 28 | items = items.Where(e => e.Description != null && e.Description.Contains(search)).ToList(); |
| 29 | return ExpenseBllDtoFactory.CreateList(items.OrderByDescending(e => e.ExpenseDate)); |
| 30 | } |
| 31 | |
| 32 | public async Task<ExpenseBllDto?> GetByIdAsync(Guid id) |
| 33 | { |
| 34 | var entity = await _uow.Expenses.GetByIdAsync(id); |
| 35 | return entity == null ? null : ExpenseBllDtoFactory.Create(entity); |
| 36 | } |
| 37 | |
| 38 | public async Task CreateAsync(ExpenseBllDto entity) |
| 39 | { |
| 40 | var domainEntity = ExpenseBllDtoFactory.ToEntity(entity); |
| 41 | domainEntity.Id = Guid.NewGuid(); |
| 42 | _uow.Expenses.Add(domainEntity); |
| 43 | await _uow.SaveChangesAsync(); |
| 44 | } |
| 45 | |
| 46 | public async Task UpdateAsync(ExpenseBllDto entity) |
| 47 | { |
| 48 | var existing = await _uow.Expenses.GetByIdAsync(entity.Id); |
| 49 | if (existing == null) return; |
| 50 | existing.TripId = entity.TripId; |
| 51 | existing.PaidByUserId = entity.PaidByUserId; |
| 52 | existing.BudgetCategoryId = entity.BudgetCategoryId; |
| 53 | existing.CurrencyId = entity.CurrencyId; |
| 54 | existing.Amount = entity.Amount; |
| 55 | existing.Description = entity.Description; |
| 56 | existing.ExpenseDate = entity.ExpenseDate; |
| 57 | existing.SplitMethod = entity.SplitMethod; |
| 58 | _uow.Expenses.Update(existing); |
| 59 | await _uow.SaveChangesAsync(); |
| 60 | } |
| 61 | |
| 62 | public async Task DeleteAsync(Guid id) |
| 63 | { |
| 64 | await _uow.Expenses.RemoveAsync(id); |
| 65 | await _uow.SaveChangesAsync(); |
| 66 | } |
| 67 | |
| 68 | public Task<bool> ExistsAsync(Guid id) => _uow.Expenses.ExistsAsync(id); |
| 69 | |
| 70 | public async Task<List<TripBllDto>> GetTripsAsync() |
| 71 | { |
| 72 | var trips = await _uow.Trips.GetAllAsync(); |
| 73 | return TripBllDtoFactory.CreateList(trips); |
| 74 | } |
| 75 | |
| 76 | public async Task<List<AppUserBllDto>> GetUsersAsync() |
| 77 | { |
| 78 | var users = await _uow.Users.GetAllAsync(); |
| 79 | return users.Select(u => new AppUserBllDto |
| 80 | { |
| 81 | Id = u.Id, |
| 82 | FirstName = u.FirstName, |
| 83 | LastName = u.LastName, |
| 84 | Email = u.Email |
| 85 | }).ToList(); |
| 86 | } |
| 87 | |
| 88 | public async Task<List<CurrencyBllDto>> GetCurrenciesAsync() |
| 89 | { |
| 90 | var currencies = await _uow.GetRepository<Currency>().GetAllAsync(); |
| 91 | return CurrencyBllDtoFactory.CreateList(currencies); |
| 92 | } |
| 93 | |
| 94 | public async Task<List<BudgetCategoryBllDto>> GetBudgetCategoriesAsync() |
| 95 | { |
| 96 | var categories = await _uow.BudgetCategories.GetAllAsync(); |
| 97 | return BudgetCategoryBllDtoFactory.CreateList(categories); |
| 98 | } |
| 99 | } |
| 100 | |