using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using PodNoms.Api.Models; namespace PodNoms.Api.Persistence { public interface IEntryRepository : IRepository { Task> GetAllForSlugAsync(string podcastSlug); Task> GetAllForUserAsync(string userId); Task GetByUidAsync(string uid); Task LoadPodcastAsync(PodcastEntry entry); } public class EntryRepository : GenericRepository, IEntryRepository { public EntryRepository(PodNomsDbContext context, ILogger logger) : base(context, logger) { } public new async Task GetAsync(int id) { var ret = await GetAll() .Where(p => p.Id == id) .Include(p => p.Podcast) .Include(p => p.Podcast.AppUser) .FirstOrDefaultAsync(); return ret; } public async Task> GetAllForSlugAsync(string podcastSlug) { var entries = await GetAll() .Where(e => e.Podcast.Slug == podcastSlug) .Include(e => e.Podcast) .ToListAsync(); return entries; } public async Task> GetAllForUserAsync(string userId) { var entries = await GetAll() .Where(e => e.Podcast.AppUser.Id == userId) .Include(e => e.Podcast) .ToListAsync(); return entries; } public async Task GetByUidAsync(string uid) { var entry = await GetAll() .Include(e => e.Podcast) .SingleOrDefaultAsync(e => e.NewId.ToString().Equals(uid)) ; return entry; } public async Task LoadPodcastAsync(PodcastEntry entry) { await GetContext().Entry(entry) .Reference(e => e.Podcast) .LoadAsync(); } } }