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