SplitPresetAdminService.cs
2,133 bytes
| 1 | using SplitApp.WebApp.Application.Contracts; |
|---|---|
| 2 | using SplitApp.WebApp.Application.DTO; |
| 3 | using SplitApp.WebApp.Application.Mappers; |
| 4 | using SplitApp.WebApp.Application.UsersService; |
| 5 | |
| 6 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 7 | |
| 8 | public class SplitPresetAdminService : ISplitPresetAdminService |
| 9 | { |
| 10 | private readonly IAppUnitOfWork _uow; |
| 11 | private readonly IUsersServiceClient _usersService; |
| 12 | |
| 13 | public SplitPresetAdminService(IAppUnitOfWork uow, IUsersServiceClient usersService) |
| 14 | { |
| 15 | _uow = uow; |
| 16 | _usersService = usersService; |
| 17 | } |
| 18 | |
| 19 | public async Task<List<SplitPresetBllDto>> GetAllAsync(string? search) |
| 20 | { |
| 21 | var presets = (await _uow.SplitPresets.GetAllAsync()).ToList(); |
| 22 | if (!string.IsNullOrEmpty(search)) |
| 23 | presets = presets.Where(s => s.Name.Contains(search)).ToList(); |
| 24 | return SplitPresetBllDtoFactory.CreateList(presets.OrderByDescending(s => s.Id), includeMembers: true); |
| 25 | } |
| 26 | |
| 27 | public async Task<SplitPresetBllDto?> GetByIdAsync(Guid id) |
| 28 | { |
| 29 | var entity = await _uow.SplitPresets.GetByIdAsync(id); |
| 30 | return entity == null ? null : SplitPresetBllDtoFactory.Create(entity, includeMembers: true); |
| 31 | } |
| 32 | |
| 33 | public async Task CreateAsync(SplitPresetBllDto entity) |
| 34 | { |
| 35 | var domainEntity = SplitPresetBllDtoFactory.ToEntity(entity); |
| 36 | domainEntity.Id = Guid.NewGuid(); |
| 37 | _uow.SplitPresets.Add(domainEntity); |
| 38 | await _uow.SaveChangesAsync(); |
| 39 | } |
| 40 | |
| 41 | public async Task DeleteAsync(Guid id) |
| 42 | { |
| 43 | await _uow.SplitPresets.RemoveAsync(id); |
| 44 | await _uow.SaveChangesAsync(); |
| 45 | } |
| 46 | |
| 47 | public async Task<List<TripBllDto>> GetTripsAsync() |
| 48 | { |
| 49 | var trips = await _uow.Trips.GetAllAsync(); |
| 50 | return TripBllDtoFactory.CreateList(trips.OrderBy(t => t.Name)); |
| 51 | } |
| 52 | |
| 53 | public async Task<List<AppUserBllDto>> GetUsersAsync() |
| 54 | { |
| 55 | var users = await _usersService.ListUsersAsync(); |
| 56 | return users.Select(u => new AppUserBllDto |
| 57 | { |
| 58 | Id = u.Id, |
| 59 | FirstName = u.FirstName, |
| 60 | LastName = u.LastName, |
| 61 | Email = u.Email, |
| 62 | }).ToList(); |
| 63 | } |
| 64 | } |
| 65 | |