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