InvitationAdminService.cs
2,531 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 InvitationAdminService : IInvitationAdminService |
| 8 | { |
| 9 | private readonly IAppUnitOfWork _uow; |
| 10 | |
| 11 | public InvitationAdminService(IAppUnitOfWork uow) |
| 12 | { |
| 13 | _uow = uow; |
| 14 | } |
| 15 | |
| 16 | public async Task<List<TripInvitationBllDto>> GetAllAsync(string? search) |
| 17 | { |
| 18 | var invitations = (await _uow.TripInvitations.GetAllAsync()).ToList(); |
| 19 | if (!string.IsNullOrEmpty(search)) |
| 20 | invitations = invitations.Where(i => i.Token.Contains(search)).ToList(); |
| 21 | return InvitationBllDtoFactory.CreateList(invitations.OrderByDescending(i => i.Id)); |
| 22 | } |
| 23 | |
| 24 | public async Task<TripInvitationBllDto?> GetByIdAsync(Guid id) |
| 25 | { |
| 26 | var entity = await _uow.TripInvitations.GetByIdAsync(id); |
| 27 | return entity == null ? null : InvitationBllDtoFactory.Create(entity); |
| 28 | } |
| 29 | |
| 30 | public async Task CreateAsync(TripInvitationBllDto entity) |
| 31 | { |
| 32 | var domainEntity = InvitationBllDtoFactory.ToEntity(entity); |
| 33 | domainEntity.Id = Guid.NewGuid(); |
| 34 | if (string.IsNullOrEmpty(domainEntity.Token)) |
| 35 | domainEntity.Token = Guid.NewGuid().ToString("N"); |
| 36 | _uow.TripInvitations.Add(domainEntity); |
| 37 | await _uow.SaveChangesAsync(); |
| 38 | } |
| 39 | |
| 40 | public async Task UpdateAsync(TripInvitationBllDto entity) |
| 41 | { |
| 42 | var existing = await _uow.TripInvitations.GetByIdAsync(entity.Id); |
| 43 | if (existing == null) return; |
| 44 | existing.TripId = entity.TripId; |
| 45 | existing.InvitedByUserId = entity.InvitedByUserId; |
| 46 | existing.Token = entity.Token; |
| 47 | existing.Status = entity.Status; |
| 48 | existing.ExpiresAt = entity.ExpiresAt; |
| 49 | existing.RespondedAt = entity.RespondedAt; |
| 50 | _uow.TripInvitations.Update(existing); |
| 51 | await _uow.SaveChangesAsync(); |
| 52 | } |
| 53 | |
| 54 | public async Task DeleteAsync(Guid id) |
| 55 | { |
| 56 | await _uow.TripInvitations.RemoveAsync(id); |
| 57 | await _uow.SaveChangesAsync(); |
| 58 | } |
| 59 | |
| 60 | public async Task<List<TripBllDto>> GetTripsAsync() |
| 61 | { |
| 62 | var trips = await _uow.Trips.GetAllAsync(); |
| 63 | return TripBllDtoFactory.CreateList(trips.OrderBy(t => t.Name)); |
| 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 | |