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