Made a generic repository to use !minor

This commit is contained in:
Jamie.Rees 2017-10-17 15:03:00 +01:00
commit 894945f652
8 changed files with 99 additions and 149 deletions

View file

@ -66,8 +66,7 @@ namespace Ombi.Store.Context
.WithMany(b => b.Episodes)
.HasPrincipalKey(x => x.EmbyId)
.HasForeignKey(p => p.ParentId);
builder.Ignore<Logs>();
base.OnModelCreating(builder);
}

View file

@ -5,12 +5,10 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public interface IPlexContentRepository
public interface IPlexContentRepository : IRepository<PlexContent>
{
Task<PlexContent> Add(PlexContent content);
Task AddRange(IEnumerable<PlexContent> content);
Task<bool> ContentExists(string providerId);
Task<IEnumerable<PlexContent>> GetAll();
Task<PlexContent> Get(string providerId);
Task<PlexContent> GetByKey(int key);
Task Update(PlexContent existingContent);
@ -18,6 +16,5 @@ namespace Ombi.Store.Repository
Task<PlexEpisode> Add(PlexEpisode content);
Task<PlexEpisode> GetEpisodeByKey(int key);
Task AddRange(IEnumerable<PlexEpisode> content);
IQueryable<PlexContent> Get();
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Query;
using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public interface IRepository<T> where T : Entity
{
Task<T> Find(object key);
IQueryable<T> GetAll();
Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate);
Task AddRange(IEnumerable<T> content);
IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath)
where TEntity : class;
}
}

View file

@ -34,49 +34,27 @@ using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public class PlexContentRepository : IPlexContentRepository
public class PlexContentRepository : Repository<PlexContent>, IPlexContentRepository
{
public PlexContentRepository(IOmbiContext db)
public PlexContentRepository(IOmbiContext db) : base(db)
{
Db = db;
}
private IOmbiContext Db { get; }
public async Task<IEnumerable<PlexContent>> GetAll()
{
return await Db.PlexContent.ToListAsync();
}
public async Task AddRange(IEnumerable<PlexContent> content)
{
Db.PlexContent.AddRange(content);
await Db.SaveChangesAsync();
}
public async Task<bool> ContentExists(string providerId)
{
return await Db.PlexContent.AnyAsync(x => x.ProviderId == providerId);
}
public async Task<PlexContent> Add(PlexContent content)
{
await Db.PlexContent.AddAsync(content);
await Db.SaveChangesAsync();
return content;
}
public async Task<PlexContent> Get(string providerId)
{
return await Db.PlexContent.FirstOrDefaultAsync(x => x.ProviderId == providerId);
}
public IQueryable<PlexContent> Get()
{
return Db.PlexContent.AsQueryable();
}
public async Task<PlexContent> GetByKey(int key)
{
return await Db.PlexContent.Include(x => x.Seasons).FirstOrDefaultAsync(x => x.Key == key);

View file

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
using Ombi.Store.Context;
using Ombi.Store.Entities;
namespace Ombi.Store.Repository
{
public class Repository<T> : IRepository<T> where T : Entity
{
public Repository(IOmbiContext ctx)
{
_ctx = ctx;
_db = _ctx.Set<T>();
}
private readonly DbSet<T> _db;
private readonly IOmbiContext _ctx;
public async Task<T> Find(object key)
{
return await _db.FindAsync(key);
}
public IQueryable<T> GetAll()
{
return _db.AsQueryable();
}
public async Task<T> FirstOrDefaultAsync(Expression<Func<T,bool>> predicate)
{
return await _db.FirstOrDefaultAsync(predicate);
}
public async Task AddRange(IEnumerable<T> content)
{
_db.AddRange(content);
await _ctx.SaveChangesAsync();
}
public async Task<T> Add(T content)
{
await _db.AddAsync(content);
await _ctx.SaveChangesAsync();
return content;
}
public IIncludableQueryable<TEntity, TProperty> Include<TEntity, TProperty>(
IQueryable<TEntity> source, Expression<Func<TEntity, TProperty>> navigationPropertyPath)
where TEntity : class
{
return source.Include(navigationPropertyPath);
}
}
}