using Application.Classes; using Application.DataTransferObjects.Note; 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 NoteRepository(ApplicationContext context, IMapper mapper, ILogger logger) : INoteRepository { public async Task> GetNoteAsync(Guid noteId, CancellationToken cancellationToken) { var noteById = await context.Notes .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .FirstOrDefaultAsync(note => note.Id == noteId, cancellationToken); return noteById is null ? Result.Failure(NoteErrors.NotFound) : Result.Success(noteById); } public async Task>> GetPlayerNotesAsync(Guid playerId, CancellationToken cancellationToken) { var playerNotes = await context.Notes .Where(note => note.PlayerId == playerId) .ProjectTo(mapper.ConfigurationProvider) .AsNoTracking() .OrderByDescending(note => note.CreatedOn) .ToListAsync(cancellationToken); return Result.Success(playerNotes); } public async Task> CreateNoteAsync(CreateNoteDto createNoteDto, string createdBy, CancellationToken cancellationToken) { var newNote = mapper.Map(createNoteDto); newNote.CreatedBy = createdBy; await context.Notes.AddAsync(newNote, cancellationToken); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(newNote)); } catch (Exception e) { logger.LogError(e, "{DateBaseErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task> UpdateNoteAsync(UpdateNoteDto updateNoteDto, string modifiedBy, CancellationToken cancellationToken) { var noteToUpdate = await context.Notes .FirstOrDefaultAsync(note => note.Id == updateNoteDto.Id, cancellationToken); if (noteToUpdate is null) return Result.Failure(NoteErrors.NotFound); mapper.Map(updateNoteDto, noteToUpdate); noteToUpdate.ModifiedBy = modifiedBy; try { await context.SaveChangesAsync(cancellationToken); return Result.Success(mapper.Map(noteToUpdate)); } catch (Exception e) { logger.LogError(e, "{DateBaseErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } public async Task> DeleteNoteAsync(Guid noteId, CancellationToken cancellationToken) { var noteToDelete = await context.Notes .FirstOrDefaultAsync(note => note.Id == noteId, cancellationToken); if (noteToDelete is null) return Result.Failure(NoteErrors.NotFound); context.Notes.Remove(noteToDelete); try { await context.SaveChangesAsync(cancellationToken); return Result.Success(true); } catch (Exception e) { logger.LogError(e, "{DateBaseErrorMessage}", e.Message); return Result.Failure(GeneralErrors.DatabaseError); } } }