using Application.Classes; using Application.DataTransferObjects.DesertStormParticipants; 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 DesertStormParticipantRepository(ApplicationContext context, ILogger logger, IMapper mapper) : IDesertStormParticipantRepository { public async Task> GetDesertStormParticipantAsync(Guid desertStormParticipantId, CancellationToken cancellationToken) { var desertStormParticipantById = await context.DesertStormParticipants .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(desertStormParticipant => desertStormParticipant.Id == desertStormParticipantId, cancellationToken); return desertStormParticipantById is null ? Result.Failure(new Error("", "")) : Result.Success(desertStormParticipantById); } public async Task> InsertDesertStormParticipantAsync(List createDesertStormParticipants, CancellationToken cancellationToken) { var desertStormParticipants = mapper.Map>(createDesertStormParticipants); await context.DesertStormParticipants.AddRangeAsync(desertStormParticipants, cancellationToken); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(true); } catch (Exception e) { logger.LogError(e, "{DateBaseErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task>> GetPlayerDesertStormParticipantsAsync(Guid playerId, int last, CancellationToken cancellationToken) { var desertStormPlayerParticipated = await context.DesertStormParticipants .Where(desertStormParticipant => desertStormParticipant.PlayerId == playerId) .OrderByDescending(desertStormParticipant => desertStormParticipant.DesertStorm.EventDate) .Take(last) .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .ToListAsync(cancellationToken); return Result.Success(desertStormPlayerParticipated); } public async Task> UpdateDesertStormParticipantAsync(UpdateDesertStormParticipantDto updateDesertStormParticipantDto, CancellationToken cancellationToken) { var desertStormParticipantToUpdate = await context.DesertStormParticipants .FirstOrDefaultAsync( desertStormParticipant => desertStormParticipant.Id == updateDesertStormParticipantDto.Id, cancellationToken); if (desertStormParticipantToUpdate is null) return Result.Failure(new Error("", "")); mapper.Map(updateDesertStormParticipantDto, desertStormParticipantToUpdate); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(desertStormParticipantToUpdate)); } catch (Exception e) { logger.LogError(e, "{DateBaseErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } }