using Application.Classes; using Application.DataTransferObjects.MarshalGuardParticipant; using Application.Errors; using Application.Interfaces; using AutoMapper; using AutoMapper.QueryableExtensions; using Database; using Database.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; namespace Application.Repositories; public class MarshalGuardParticipantRepository(ApplicationContext context, IMapper mapper, ILogger logger) : IMarshalGuardParticipantRepository { public async Task> GetMarshalGuardParticipantAsync(Guid marshalGuardParticipantId, CancellationToken cancellationToken) { var marshalGuardParticipantById = await context.MarshalGuardParticipants .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(marshalGuardParticipant => marshalGuardParticipant.Id == marshalGuardParticipantId, cancellationToken); return marshalGuardParticipantById is null ? Result.Failure(new Error("", "")) : Result.Success(marshalGuardParticipantById); } public async Task> InsertMarshalGuardParticipantAsync(List createMarshalGuardParticipantsDto, CancellationToken cancellationToken) { var newMarshalGuardParticipants = mapper.Map>(createMarshalGuardParticipantsDto); await context.AddRangeAsync(newMarshalGuardParticipants, cancellationToken); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(true); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task>> GetPlayerMarshalParticipantsAsync(Guid playerId, int last, CancellationToken cancellationToken) { var playerMarshalParticipants = await context.MarshalGuardParticipants .Where(mp => mp.PlayerId == playerId) .OrderByDescending(mp => mp.MarshalGuard.EventDate) .Take(last) .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .ToListAsync(cancellationToken); return Result.Success(playerMarshalParticipants); } public async Task> UpdateMarshalGuardParticipantAsync(UpdateMarshalGuardParticipantDto updateMarshalGuardParticipantDto, CancellationToken cancellationToken) { var participantToUpdate = await context.MarshalGuardParticipants .FirstOrDefaultAsync( marshalGuardParticipant => marshalGuardParticipant.Id == updateMarshalGuardParticipantDto.Id, cancellationToken); if (participantToUpdate is null) return Result.Failure(MarshalGuardErrors.NotFound); mapper.Map(updateMarshalGuardParticipantDto, participantToUpdate); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(participantToUpdate)); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } }