using Application.Classes; using Application.DataTransferObjects.CustomEventCategory; 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 CustomEventCategoryRepository(ApplicationContext dbContext, IMapper mapper, ILogger logger) : ICustomEventCategoryRepository { public async Task> GetCustomEventCategoryAsync(Guid customEventCategoryId, CancellationToken cancellationToken) { var customEventCategoryById = await dbContext.CustomEventCategories .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(customEventCategory => customEventCategory.Id == customEventCategoryId, cancellationToken); return customEventCategoryById is null ? Result.Failure(CustomEventCategoryErrors.NotFound) : Result.Success(customEventCategoryById); } public async Task>> GetAllianceCustomEventCategoriesAsync(Guid allianceId, CancellationToken cancellationToken) { var customEventCategories = await dbContext.CustomEventCategories .Where(customEventCategory => customEventCategory.AllianceId == allianceId) .ProjectTo(mapper.ConfigurationProvider) .OrderByDescending(customEventCategory => customEventCategory.Id) .AsNoTracking() .ToListAsync(cancellationToken); return Result.Success(customEventCategories); } public async Task> CreateCustomEventCategoryAsync(CreateCustomEventCategoryDto createCustomEventCategoryDto, CancellationToken cancellationToken) { try { var newCustomEventCategory = mapper.Map(createCustomEventCategoryDto); await dbContext.CustomEventCategories.AddAsync(newCustomEventCategory, cancellationToken); await dbContext.SaveChangesAsync(cancellationToken); var customEventCategoryDto = mapper.Map(newCustomEventCategory); return Result.Success(customEventCategoryDto); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task> UpdateCustomEventCategoryAsync(UpdateCustomEventCategoryDto updateCustomEventCategoryDto, CancellationToken cancellationToken) { var customEventCategoryToUpdate = await dbContext.CustomEventCategories .FirstOrDefaultAsync(customEventCategory => customEventCategory.Id == updateCustomEventCategoryDto.Id, cancellationToken); if (customEventCategoryToUpdate is null) { return Result.Failure(CustomEventCategoryErrors.NotFound); } try { mapper.Map(updateCustomEventCategoryDto, customEventCategoryToUpdate); await dbContext.SaveChangesAsync(cancellationToken); var customEventCategoryDto = mapper.Map(customEventCategoryToUpdate); return Result.Success(customEventCategoryDto); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task> DeleteCustomEventAsync(Guid customEventId, CancellationToken cancellationToken) { var customEventToDelete = await dbContext.CustomEventCategories .FirstOrDefaultAsync(customEvent => customEvent.Id == customEventId, cancellationToken); if (customEventToDelete is null) { return Result.Failure(CustomEventCategoryErrors.NotFound); } try { var customEvents = await dbContext.CustomEvents .Where(customEvent => customEvent.CustomEventCategoryId == customEventToDelete.Id) .ToListAsync(cancellationToken); if (customEvents.Any()) { dbContext.CustomEvents.RemoveRange(customEvents); } dbContext.CustomEventCategories.Remove(customEventToDelete); await dbContext.SaveChangesAsync(cancellationToken); return Result.Success(true); } catch (Exception e) { logger.LogError(e, "{ErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } }