TripParticipantAdminService.cs
2,960 bytes
| 1 | using SplitApp.WebApp.Application.DTO; |
|---|---|
| 2 | using SplitApp.WebApp.Application.Mappers; |
| 3 | using SplitApp.WebApp.Application.Contracts; |
| 4 | |
| 5 | namespace SplitApp.WebApp.Application.Services.Admin; |
| 6 | |
| 7 | public class TripParticipantAdminService : ITripParticipantAdminService |
| 8 | { |
| 9 | private readonly IAppUnitOfWork _uow; |
| 10 | |
| 11 | public TripParticipantAdminService(IAppUnitOfWork uow) |
| 12 | { |
| 13 | _uow = uow; |
| 14 | } |
| 15 | |
| 16 | public async Task<List<TripParticipantBllDto>> GetAllAsync(Guid? tripId, string? search) |
| 17 | { |
| 18 | var participants = (await _uow.TripParticipants.GetAllAsync()).ToList(); |
| 19 | |
| 20 | if (tripId.HasValue) |
| 21 | participants = participants.Where(tp => tp.TripId == tripId.Value).ToList(); |
| 22 | |
| 23 | if (!string.IsNullOrEmpty(search)) |
| 24 | participants = participants.Where(tp => tp.User != null && ( |
| 25 | tp.User.FirstName.Contains(search) || |
| 26 | tp.User.LastName.Contains(search) || |
| 27 | (tp.User.Email != null && tp.User.Email.Contains(search)))).ToList(); |
| 28 | |
| 29 | return TripParticipantBllDtoFactory.CreateList(participants.OrderByDescending(tp => tp.JoinedAt)); |
| 30 | } |
| 31 | |
| 32 | public async Task<TripParticipantBllDto?> GetByIdAsync(Guid id) |
| 33 | { |
| 34 | var entity = await _uow.TripParticipants.GetByIdAsync(id); |
| 35 | return entity == null ? null : TripParticipantBllDtoFactory.Create(entity); |
| 36 | } |
| 37 | |
| 38 | public async Task CreateAsync(TripParticipantBllDto entity) |
| 39 | { |
| 40 | var domainEntity = TripParticipantBllDtoFactory.ToEntity(entity); |
| 41 | domainEntity.Id = Guid.NewGuid(); |
| 42 | _uow.TripParticipants.Add(domainEntity); |
| 43 | await _uow.SaveChangesAsync(); |
| 44 | } |
| 45 | |
| 46 | public async Task UpdateAsync(TripParticipantBllDto entity) |
| 47 | { |
| 48 | var existing = await _uow.TripParticipants.GetByIdAsync(entity.Id); |
| 49 | if (existing == null) return; |
| 50 | existing.TripId = entity.TripId; |
| 51 | existing.UserId = entity.UserId; |
| 52 | existing.Role = entity.Role; |
| 53 | existing.Nickname = entity.Nickname; |
| 54 | existing.JoinedAt = entity.JoinedAt; |
| 55 | existing.LeftAt = entity.LeftAt; |
| 56 | existing.IsActive = entity.IsActive; |
| 57 | _uow.TripParticipants.Update(existing); |
| 58 | await _uow.SaveChangesAsync(); |
| 59 | } |
| 60 | |
| 61 | public async Task DeleteAsync(Guid id) |
| 62 | { |
| 63 | await _uow.TripParticipants.RemoveAsync(id); |
| 64 | await _uow.SaveChangesAsync(); |
| 65 | } |
| 66 | |
| 67 | public Task<bool> ExistsAsync(Guid id) => _uow.TripParticipants.ExistsAsync(id); |
| 68 | |
| 69 | public async Task<List<TripBllDto>> GetTripsAsync() |
| 70 | { |
| 71 | var trips = await _uow.Trips.GetAllAsync(); |
| 72 | return TripBllDtoFactory.CreateList(trips.OrderBy(t => t.Name)); |
| 73 | } |
| 74 | |
| 75 | public async Task<List<AppUserBllDto>> GetUsersAsync() |
| 76 | { |
| 77 | var users = await _uow.Users.GetAllAsync(); |
| 78 | return users.Select(u => new AppUserBllDto |
| 79 | { |
| 80 | Id = u.Id, |
| 81 | FirstName = u.FirstName, |
| 82 | LastName = u.LastName, |
| 83 | Email = u.Email |
| 84 | }).ToList(); |
| 85 | } |
| 86 | } |
| 87 | |