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