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