TripParticipantRepository.cs
1,623 bytes
| 1 | using App.Domain; |
|---|---|
| 2 | using App.Domain.Contracts; |
| 3 | using Microsoft.EntityFrameworkCore; |
| 4 | |
| 5 | namespace App.DAL.EF.Repositories; |
| 6 | |
| 7 | public class TripParticipantRepository : BaseRepository<TripParticipant>, ITripParticipantRepository |
| 8 | { |
| 9 | public TripParticipantRepository(AppDbContext dbContext) : base(dbContext) |
| 10 | { |
| 11 | } |
| 12 | |
| 13 | public override async Task<IEnumerable<TripParticipant>> GetAllAsync() |
| 14 | { |
| 15 | return await DbContext.TripParticipants |
| 16 | .Include(tp => tp.Trip) |
| 17 | .Include(tp => tp.User) |
| 18 | .ToListAsync(); |
| 19 | } |
| 20 | |
| 21 | public override async Task<TripParticipant?> GetByIdAsync(Guid id) |
| 22 | { |
| 23 | return await DbContext.TripParticipants |
| 24 | .Include(tp => tp.Trip) |
| 25 | .Include(tp => tp.User) |
| 26 | .FirstOrDefaultAsync(tp => tp.Id == id); |
| 27 | } |
| 28 | |
| 29 | public async Task<bool> IsParticipantAsync(Guid tripId, Guid userId) |
| 30 | { |
| 31 | return await DbContext.TripParticipants |
| 32 | .AnyAsync(tp => tp.TripId == tripId && tp.UserId == userId && tp.IsActive); |
| 33 | } |
| 34 | |
| 35 | public async Task<bool> IsOrganizerAsync(Guid tripId, Guid userId) |
| 36 | { |
| 37 | return await DbContext.TripParticipants |
| 38 | .AnyAsync(tp => tp.TripId == tripId && tp.UserId == userId && tp.IsActive |
| 39 | && tp.Role == EParticipantRole.Organizer); |
| 40 | } |
| 41 | |
| 42 | public async Task<IEnumerable<TripParticipant>> GetByTripIdAsync(Guid tripId) |
| 43 | { |
| 44 | return await DbContext.TripParticipants |
| 45 | .Where(tp => tp.TripId == tripId && tp.IsActive) |
| 46 | .Include(tp => tp.User) |
| 47 | .OrderBy(tp => tp.User!.FirstName) |
| 48 | .ToListAsync(); |
| 49 | } |
| 50 | } |
| 51 | |