CurrencyAdminService.cs
2,665 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 CurrencyAdminService : ICurrencyAdminService |
| 14 | { |
| 15 | private readonly IAppUnitOfWork _uow; |
| 16 | |
| 17 | public CurrencyAdminService(IAppUnitOfWork uow) |
| 18 | { |
| 19 | _uow = uow; |
| 20 | } |
| 21 | |
| 22 | public async Task<List<CurrencyBllDto>> GetAllAsync(string? search) |
| 23 | { |
| 24 | var items = (await _uow.GetRepository<Currency>().GetAllAsync()).OrderBy(c => c.Code).ToList(); |
| 25 | if (!string.IsNullOrEmpty(search)) |
| 26 | items = items.Where(c => |
| 27 | c.Code.Contains(search, StringComparison.OrdinalIgnoreCase) || |
| 28 | c.Name.ToString().Contains(search, StringComparison.OrdinalIgnoreCase)).ToList(); |
| 29 | return CurrencyBllDtoFactory.CreateList(items); |
| 30 | } |
| 31 | |
| 32 | public async Task<CurrencyBllDto?> GetByIdAsync(Guid id) |
| 33 | { |
| 34 | var entity = await _uow.GetRepository<Currency>().GetByIdAsync(id); |
| 35 | return entity == null ? null : CurrencyBllDtoFactory.Create(entity); |
| 36 | } |
| 37 | |
| 38 | public async Task CreateAsync(CurrencyBllDto entity, string? nameEn, string? nameEt) |
| 39 | { |
| 40 | var domainEntity = CurrencyBllDtoFactory.ToEntity(entity); |
| 41 | ApplyLangStr(domainEntity, nameEn, nameEt); |
| 42 | domainEntity.Id = Guid.NewGuid(); |
| 43 | _uow.GetRepository<Currency>().Add(domainEntity); |
| 44 | await _uow.SaveChangesAsync(); |
| 45 | } |
| 46 | |
| 47 | public async Task UpdateAsync(CurrencyBllDto entity, string? nameEn, string? nameEt) |
| 48 | { |
| 49 | var existing = await _uow.GetRepository<Currency>().GetByIdAsync(entity.Id); |
| 50 | if (existing == null) return; |
| 51 | existing.Code = entity.Code; |
| 52 | existing.Symbol = entity.Symbol; |
| 53 | ApplyLangStr(existing, nameEn, nameEt); |
| 54 | _uow.GetRepository<Currency>().Update(existing); |
| 55 | await _uow.SaveChangesAsync(); |
| 56 | } |
| 57 | |
| 58 | public async Task DeleteAsync(Guid id) |
| 59 | { |
| 60 | await _uow.GetRepository<Currency>().RemoveAsync(id); |
| 61 | await _uow.SaveChangesAsync(); |
| 62 | } |
| 63 | |
| 64 | public Task<bool> ExistsAsync(Guid id) => _uow.GetRepository<Currency>().ExistsAsync(id); |
| 65 | |
| 66 | private static void ApplyLangStr(Currency entity, string? nameEn, string? nameEt) |
| 67 | { |
| 68 | var name = new LangStr(nameEn ?? "", "en"); |
| 69 | name.SetTranslation(nameEt ?? nameEn ?? "", "et"); |
| 70 | entity.Name = name; |
| 71 | } |
| 72 | } |
| 73 | |