mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-16 02:02:55 -07:00
Made a generic repository to use !minor
This commit is contained in:
parent
ba04a9d1b1
commit
894945f652
8 changed files with 99 additions and 149 deletions
58
src/Ombi.Store/Repository/Repository.cs
Normal file
58
src/Ombi.Store/Repository/Repository.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue