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