PollAdminService.cs
2,665 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 PollAdminService : IPollAdminService |
| 9 | { |
| 10 | private readonly IAppUnitOfWork _uow; |
| 11 | private readonly IUsersServiceClient _usersService; |
| 12 | |
| 13 | public PollAdminService(IAppUnitOfWork uow, IUsersServiceClient usersService) |
| 14 | { |
| 15 | _uow = uow; |
| 16 | _usersService = usersService; |
| 17 | } |
| 18 | |
| 19 | public async Task<List<TripPollBllDto>> GetAllAsync(string? search) |
| 20 | { |
| 21 | var items = (await _uow.TripPolls.GetAllAsync()).ToList(); |
| 22 | if (!string.IsNullOrEmpty(search)) |
| 23 | items = items.Where(p => p.Question.Contains(search)).ToList(); |
| 24 | return PollBllDtoFactory.CreateList(items.OrderByDescending(p => p.Id), includeOptions: true); |
| 25 | } |
| 26 | |
| 27 | public async Task<TripPollBllDto?> GetByIdAsync(Guid id) |
| 28 | { |
| 29 | var entity = await _uow.TripPolls.GetByIdAsync(id); |
| 30 | return entity == null ? null : PollBllDtoFactory.Create(entity); |
| 31 | } |
| 32 | |
| 33 | public async Task CreateAsync(TripPollBllDto entity) |
| 34 | { |
| 35 | var domainEntity = PollBllDtoFactory.ToEntity(entity); |
| 36 | domainEntity.Id = Guid.NewGuid(); |
| 37 | _uow.TripPolls.Add(domainEntity); |
| 38 | await _uow.SaveChangesAsync(); |
| 39 | } |
| 40 | |
| 41 | public async Task UpdateAsync(TripPollBllDto entity) |
| 42 | { |
| 43 | var existing = await _uow.TripPolls.GetByIdAsync(entity.Id); |
| 44 | if (existing == null) return; |
| 45 | existing.TripId = entity.TripId; |
| 46 | existing.CreatedByUserId = entity.CreatedByUserId; |
| 47 | existing.Question = entity.Question; |
| 48 | existing.AllowMultipleVotes = entity.AllowMultipleVotes; |
| 49 | existing.IsAnonymous = entity.IsAnonymous; |
| 50 | existing.ClosedAt = entity.ClosedAt; |
| 51 | _uow.TripPolls.Update(existing); |
| 52 | await _uow.SaveChangesAsync(); |
| 53 | } |
| 54 | |
| 55 | public async Task DeleteAsync(Guid id) |
| 56 | { |
| 57 | await _uow.TripPolls.RemoveAsync(id); |
| 58 | await _uow.SaveChangesAsync(); |
| 59 | } |
| 60 | |
| 61 | public Task<bool> ExistsAsync(Guid id) => _uow.TripPolls.ExistsAsync(id); |
| 62 | |
| 63 | public async Task<List<TripBllDto>> GetTripsAsync() |
| 64 | { |
| 65 | var trips = await _uow.Trips.GetAllAsync(); |
| 66 | return TripBllDtoFactory.CreateList(trips); |
| 67 | } |
| 68 | |
| 69 | public async Task<List<AppUserBllDto>> GetUsersAsync() |
| 70 | { |
| 71 | var users = await _usersService.ListUsersAsync(); |
| 72 | return users.Select(u => new AppUserBllDto |
| 73 | { |
| 74 | Id = u.Id, |
| 75 | FirstName = u.FirstName, |
| 76 | LastName = u.LastName, |
| 77 | Email = u.Email, |
| 78 | }).ToList(); |
| 79 | } |
| 80 | } |
| 81 | |