using Application.Classes; using Application.DataTransferObjects.ZombieSiegeParticipant; 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 ZombieSiegeParticipantRepository(ApplicationContext context, IMapper mapper, ILogger logger) : IZombieSiegeParticipantRepository { public async Task> GetZombieSiegeParticipantAsync(Guid zombieSiegeParticipantId, CancellationToken cancellationToken) { var zombieSiegeParticipantById = await context.ZombieSiegeParticipants .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(zombieSiegeParticipant => zombieSiegeParticipant.Id == zombieSiegeParticipantId, cancellationToken); return zombieSiegeParticipantById is null ? Result.Failure(ZombieSiegeParticipantErrors.NotFound) : Result.Success(zombieSiegeParticipantById); } public async Task> InsertZombieSiegeParticipantsAsync(List createZombieSiegeParticipants, CancellationToken cancellationToken) { var zombieSiegeParticipants = mapper.Map>(createZombieSiegeParticipants); try { await context.ZombieSiegeParticipants.AddRangeAsync(zombieSiegeParticipants, cancellationToken); await context.SaveChangesAsync(cancellationToken); return Result.Success(true); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task>> GetPlayerZombieSiegeParticipantsAsync(Guid playerId, int last, CancellationToken cancellationToken) { var playerZombieSieges = await context.ZombieSiegeParticipants .Where(zombieSiegeParticipant => zombieSiegeParticipant.PlayerId == playerId) .OrderByDescending(zombieSiegeParticipant => zombieSiegeParticipant.ZombieSiege.EventDate) .ProjectTo(mapper.ConfigurationProvider) .Take(last) .AsNoTracking() .ToListAsync(cancellationToken); return Result.Success(playerZombieSieges); } public async Task> UpdateZombieSiegeParticipantAsync(UpdateZombieSiegeParticipantDto updateZombieSiegeParticipantDto, CancellationToken cancellationToken) { var zombieSiegeParticipantToUpdate = await context.ZombieSiegeParticipants .FirstOrDefaultAsync( zombieSiegeParticipant => zombieSiegeParticipant.Id == updateZombieSiegeParticipantDto.Id, cancellationToken); if (zombieSiegeParticipantToUpdate is null) return Result.Failure(ZombieSiegeErrors.NotFound); mapper.Map(updateZombieSiegeParticipantDto, zombieSiegeParticipantToUpdate); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(zombieSiegeParticipantToUpdate)); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } }