using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using API.DTOs.MediaErrors; using API.Entities; using API.Helpers; using AutoMapper; using AutoMapper.QueryableExtensions; using Microsoft.EntityFrameworkCore; namespace API.Data.Repositories; #nullable enable public interface IMediaErrorRepository { void Attach(MediaError error); void Remove(MediaError error); void Remove(IList errors); Task Find(string filename); IEnumerable GetAllErrorDtosAsync(); Task ExistsAsync(MediaError error); Task DeleteAll(); Task> GetAllErrorsAsync(IList comments); } public class MediaErrorRepository : IMediaErrorRepository { private readonly DataContext _context; private readonly IMapper _mapper; public MediaErrorRepository(DataContext context, IMapper mapper) { _context = context; _mapper = mapper; } public void Attach(MediaError? error) { if (error == null) return; _context.MediaError.Attach(error); } public void Remove(MediaError? error) { if (error == null) return; _context.MediaError.Remove(error); } public void Remove(IList errors) { _context.MediaError.RemoveRange(errors); } public Task Find(string filename) { return _context.MediaError.Where(e => e.FilePath == filename).SingleOrDefaultAsync(); } public IEnumerable GetAllErrorDtosAsync() { var query = _context.MediaError .OrderByDescending(m => m.Created) .ProjectTo(_mapper.ConfigurationProvider) .AsNoTracking(); return query.AsEnumerable(); } public Task ExistsAsync(MediaError error) { return _context.MediaError.AnyAsync(m => m.FilePath.Equals(error.FilePath) && m.Comment.Equals(error.Comment) && m.Details.Equals(error.Details) ); } public async Task DeleteAll() { _context.MediaError.RemoveRange(await _context.MediaError.ToListAsync()); await _context.SaveChangesAsync(); } public Task> GetAllErrorsAsync(IList comments) { return _context.MediaError .Where(m => comments.Contains(m.Comment)) .ToListAsync(); } }