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