using Application.Classes; using Application.DataTransferObjects; using Application.DataTransferObjects.ZombieSiege; 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 ZombieSiegeRepository(ApplicationContext context, IMapper mapper, ILogger logger) : IZombieSiegeRepository { public async Task> GetZombieSiegeAsync(Guid zombieSiegeId, CancellationToken cancellationToken) { var zombieSiegeById = await context.ZombieSieges .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(zombieSiege => zombieSiege.Id == zombieSiegeId, cancellationToken); return zombieSiegeById is null ? Result.Failure(ZombieSiegeErrors.NotFound) : Result.Success(zombieSiegeById); } public async Task> GetZombieSiegeDetailAsync(Guid zombieSiegeId, CancellationToken cancellationToken) { var zombieSiegeDetail = await context.ZombieSieges .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(zombieSiege => zombieSiege.Id == zombieSiegeId, cancellationToken); return zombieSiegeDetail is null ? Result.Failure(ZombieSiegeErrors.NotFound) : Result.Success(zombieSiegeDetail); } public async Task>> GetAllianceZombieSiegesAsync(Guid allianceId, int pageNumber, int pageSize, CancellationToken cancellationToken) { var query = context.ZombieSieges .Where(zombieSiege => zombieSiege.AllianceId == allianceId) .OrderByDescending(zombieSiege => zombieSiege.EventDate) .AsNoTracking(); var totalRecord = await query.CountAsync(cancellationToken); var pagedZombieSieges = await query .Skip((pageNumber - 1) * pageSize) .Take(pageSize) .ProjectTo(mapper.ConfigurationProvider) .ToListAsync(cancellationToken); return Result.Success(new PagedResponseDto { Data = pagedZombieSieges, TotalRecords = totalRecord, PageSize = pageSize, PageNumber = pageNumber }); } public async Task> CreateZombieSiegeAsync(CreateZombieSiegeDto createZombieSiegeDto, string createdBy, CancellationToken cancellationToken) { var newZombieSiege = mapper.Map(createZombieSiegeDto); newZombieSiege.CreatedBy = createdBy; try { await context.ZombieSieges.AddAsync(newZombieSiege, cancellationToken); await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(newZombieSiege)); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task> UpdateZombieSiegeAsync(UpdateZombieSiegeDto updateZombieSiegeDto, string modifiedBy, CancellationToken cancellationToken) { var zombieSiegeToUpdate = await context.ZombieSieges .FirstOrDefaultAsync(zombieSiege => zombieSiege.Id == updateZombieSiegeDto.Id, cancellationToken); if (zombieSiegeToUpdate is null) return Result.Failure(ZombieSiegeErrors.NotFound); mapper.Map(updateZombieSiegeDto, zombieSiegeToUpdate); zombieSiegeToUpdate.ModifiedBy = modifiedBy; try { await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(zombieSiegeToUpdate)); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task> DeleteZombieSiegeAsync(Guid zombieSiegeId, CancellationToken cancellationToken) { var zombieSiegeToDelete = await context.ZombieSieges .FirstOrDefaultAsync(zombieSiege => zombieSiege.Id == zombieSiegeId, cancellationToken); if (zombieSiegeToDelete is null) return Result.Failure(ZombieSiegeErrors.NotFound); try { context.ZombieSieges.Remove(zombieSiegeToDelete); await context.SaveChangesAsync(cancellationToken); return Result.Success(true); } catch (Exception e) { logger.LogError(e, e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } }