BudgetCategoryAdminService.cs
3,016 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 | using SplitApp.Shared.Kernel.Domain; |
| 9 | using SplitApp.Shared.Kernel.Localization; |
| 10 | |
| 11 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 12 | |
| 13 | public class BudgetCategoryAdminService : IBudgetCategoryAdminService |
| 14 | { |
| 15 | private readonly IAppUnitOfWork _uow; |
| 16 | |
| 17 | public BudgetCategoryAdminService(IAppUnitOfWork uow) |
| 18 | { |
| 19 | _uow = uow; |
| 20 | } |
| 21 | |
| 22 | public async Task<List<BudgetCategoryBllDto>> GetAllAsync(Guid? tripId, string? search) |
| 23 | { |
| 24 | var items = (await _uow.BudgetCategories.GetAllAsync()).ToList(); |
| 25 | if (tripId.HasValue) |
| 26 | items = items.Where(b => b.TripId == tripId.Value).ToList(); |
| 27 | if (!string.IsNullOrEmpty(search)) |
| 28 | items = items.Where(b => b.Name.ToString().Contains(search, StringComparison.OrdinalIgnoreCase)).ToList(); |
| 29 | return BudgetCategoryBllDtoFactory.CreateList(items.OrderBy(b => b.DisplayOrder)); |
| 30 | } |
| 31 | |
| 32 | public async Task<List<TripBllDto>> GetAllTripsAsync() |
| 33 | { |
| 34 | var trips = await _uow.Trips.GetAllAsync(); |
| 35 | return TripBllDtoFactory.CreateList(trips.OrderBy(t => t.Name)); |
| 36 | } |
| 37 | |
| 38 | public async Task<BudgetCategoryBllDto?> GetByIdAsync(Guid id) |
| 39 | { |
| 40 | var category = await _uow.BudgetCategories.GetByIdAsync(id); |
| 41 | return category == null ? null : BudgetCategoryBllDtoFactory.Create(category); |
| 42 | } |
| 43 | |
| 44 | public async Task CreateAsync(BudgetCategoryBllDto entity, string? nameEn, string? nameEt) |
| 45 | { |
| 46 | var domainEntity = BudgetCategoryBllDtoFactory.ToEntity(entity); |
| 47 | ApplyLangStr(domainEntity, nameEn, nameEt); |
| 48 | domainEntity.Id = Guid.NewGuid(); |
| 49 | _uow.BudgetCategories.Add(domainEntity); |
| 50 | await _uow.SaveChangesAsync(); |
| 51 | } |
| 52 | |
| 53 | public async Task UpdateAsync(BudgetCategoryBllDto entity, string? nameEn, string? nameEt) |
| 54 | { |
| 55 | var existing = await _uow.BudgetCategories.GetByIdAsync(entity.Id); |
| 56 | if (existing == null) return; |
| 57 | existing.TripId = entity.TripId; |
| 58 | existing.IconName = entity.IconName; |
| 59 | existing.PlannedAmount = entity.PlannedAmount; |
| 60 | existing.DisplayOrder = entity.DisplayOrder; |
| 61 | ApplyLangStr(existing, nameEn, nameEt); |
| 62 | _uow.BudgetCategories.Update(existing); |
| 63 | await _uow.SaveChangesAsync(); |
| 64 | } |
| 65 | |
| 66 | public async Task DeleteAsync(Guid id) |
| 67 | { |
| 68 | await _uow.BudgetCategories.RemoveAsync(id); |
| 69 | await _uow.SaveChangesAsync(); |
| 70 | } |
| 71 | |
| 72 | public Task<bool> ExistsAsync(Guid id) => _uow.BudgetCategories.ExistsAsync(id); |
| 73 | |
| 74 | private static void ApplyLangStr(BudgetCategory entity, string? nameEn, string? nameEt) |
| 75 | { |
| 76 | var name = new LangStr(nameEn ?? "", "en"); |
| 77 | name.SetTranslation(nameEt ?? nameEn ?? "", "et"); |
| 78 | entity.Name = name; |
| 79 | } |
| 80 | } |
| 81 | |