mirror of
https://github.com/zoriya/Kyoo.git
synced 2026-05-30 19:05:16 -04:00
Removing most of the things needed for a custom many to many
This commit is contained in:
@@ -15,7 +15,7 @@ using Microsoft.EntityFrameworkCore.Metadata;
|
||||
|
||||
namespace Kyoo.Controllers
|
||||
{
|
||||
public abstract class LocalRepository<T>
|
||||
public abstract class LocalRepository<T> : IRepository<T>
|
||||
where T : class, IResource
|
||||
{
|
||||
protected readonly DbContext Database;
|
||||
@@ -53,6 +53,8 @@ namespace Kyoo.Controllers
|
||||
return Database.Set<T>().FirstOrDefaultAsync(predicate);
|
||||
}
|
||||
|
||||
public abstract Task<ICollection<T>> Search(string query);
|
||||
|
||||
public virtual Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null,
|
||||
Sort<T> sort = default,
|
||||
Pagination limit = default)
|
||||
@@ -256,112 +258,4 @@ namespace Kyoo.Controllers
|
||||
await Delete(slug);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class LocalRepository<T, TInternal> : LocalRepository<TInternal>, IRepository<T>
|
||||
where T : class, IResource
|
||||
where TInternal : class, T, new()
|
||||
{
|
||||
protected LocalRepository(DbContext database) : base(database) { }
|
||||
|
||||
public new Task<T> Get(int id)
|
||||
{
|
||||
return base.Get(id).Cast<T>();
|
||||
}
|
||||
|
||||
public new Task<T> Get(string slug)
|
||||
{
|
||||
return base.Get(slug).Cast<T>();
|
||||
}
|
||||
|
||||
public Task<T> Get(Expression<Func<T, bool>> predicate)
|
||||
{
|
||||
return Get(predicate.Convert<Func<TInternal, bool>>()).Cast<T>();
|
||||
}
|
||||
|
||||
public abstract Task<ICollection<T>> Search(string query);
|
||||
|
||||
public virtual Task<ICollection<T>> GetAll(Expression<Func<T, bool>> where = null,
|
||||
Sort<T> sort = default,
|
||||
Pagination limit = default)
|
||||
{
|
||||
return ApplyFilters(Database.Set<TInternal>(), where, sort, limit);
|
||||
}
|
||||
|
||||
protected virtual async Task<ICollection<T>> ApplyFilters(IQueryable<TInternal> query,
|
||||
Expression<Func<T, bool>> where = null,
|
||||
Sort<T> sort = default,
|
||||
Pagination limit = default)
|
||||
{
|
||||
ICollection<TInternal> items = await ApplyFilters(query,
|
||||
base.Get,
|
||||
DefaultSort,
|
||||
where.Convert<Func<TInternal, bool>>(),
|
||||
sort.To<TInternal>(),
|
||||
limit);
|
||||
|
||||
return items.ToList<T>();
|
||||
}
|
||||
|
||||
public virtual Task<int> GetCount(Expression<Func<T, bool>> where = null)
|
||||
{
|
||||
IQueryable<TInternal> query = Database.Set<TInternal>();
|
||||
if (where != null)
|
||||
query = query.Where(where.Convert<Func<TInternal, bool>>());
|
||||
return query.CountAsync();
|
||||
}
|
||||
|
||||
Task<T> IRepository<T>.Create(T item)
|
||||
{
|
||||
if (item == null)
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
TInternal obj = item as TInternal ?? new TInternal();
|
||||
if (!(item is TInternal))
|
||||
Utility.Assign(obj, item);
|
||||
|
||||
return Create(obj).Cast<T>()
|
||||
.Then(x => item.ID = x.ID);
|
||||
}
|
||||
|
||||
Task<T> IRepository<T>.CreateIfNotExists(T item, bool silentFail)
|
||||
{
|
||||
if (item == null)
|
||||
throw new ArgumentNullException(nameof(item));
|
||||
TInternal obj = item as TInternal ?? new TInternal();
|
||||
if (!(item is TInternal))
|
||||
Utility.Assign(obj, item);
|
||||
|
||||
return CreateIfNotExists(obj, silentFail).Cast<T>()
|
||||
.Then(x => item.ID = x.ID);
|
||||
}
|
||||
|
||||
public Task<T> Edit(T edited, bool resetOld)
|
||||
{
|
||||
if (edited == null)
|
||||
throw new ArgumentNullException(nameof(edited));
|
||||
if (edited is TInternal intern)
|
||||
return Edit(intern, resetOld).Cast<T>();
|
||||
TInternal obj = new();
|
||||
Utility.Assign(obj, edited);
|
||||
return base.Edit(obj, resetOld).Cast<T>();
|
||||
}
|
||||
|
||||
public abstract override Task Delete([NotNull] TInternal obj);
|
||||
|
||||
Task IRepository<T>.Delete(T obj)
|
||||
{
|
||||
if (obj == null)
|
||||
throw new ArgumentNullException(nameof(obj));
|
||||
if (obj is TInternal intern)
|
||||
return Delete(intern);
|
||||
TInternal item = new();
|
||||
Utility.Assign(item, obj);
|
||||
return Delete(item);
|
||||
}
|
||||
|
||||
public virtual async Task DeleteRange(IEnumerable<T> objs)
|
||||
{
|
||||
foreach (T obj in objs)
|
||||
await ((IRepository<T>)this).Delete(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user