Changed db stuff
This commit is contained in:
@@ -38,7 +38,7 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
||||
}
|
||||
else
|
||||
{
|
||||
var dbEntry = await Get(entity);
|
||||
var dbEntry = await GetIncluded(entity);
|
||||
DbContext.Entry(dbEntry).CurrentValues.SetValues(entity);
|
||||
}
|
||||
|
||||
@@ -46,14 +46,18 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType?> Get(TEntityType entity)
|
||||
public virtual async Task<TEntityType?> GetIncluded(TEntityType entity)
|
||||
{
|
||||
var keyValues = GetPrimaryKey(entity);
|
||||
return await Get(keyValues);
|
||||
return await GetIncluded(dbEntity => GetPrimaryKey(dbEntity) == GetPrimaryKey(entity));
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType?> Get(params object?[]? keyValues)
|
||||
|
||||
public virtual async Task<TEntityType?> GetIncluded(Func<TEntityType, bool> predicate)
|
||||
{
|
||||
return await DbContext.Set<TEntityType>().Include(j => j.DateCreated).FindAsync(keyValues);
|
||||
return GetAllIncludedQueryable().FirstOrDefault(predicate);
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntityType>> GetWhereIncluded(Func<TEntityType, bool> predicate)
|
||||
{
|
||||
return GetAllIncludedQueryable().AsEnumerable().Where(predicate);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
using DBConnection.Models;
|
||||
|
||||
namespace DBConnection.Repositories.Interfaces;
|
||||
|
||||
public interface IRepository
|
||||
@@ -5,10 +7,11 @@ public interface IRepository
|
||||
|
||||
}
|
||||
|
||||
public interface IRepository<TEntityType> : IRepository where TEntityType : class
|
||||
public interface IRepository<TEntityType> : IRepository where TEntityType : BaseEntity
|
||||
{
|
||||
TEntityType Delete(TEntityType entity);
|
||||
Task<TEntityType> Upsert(TEntityType entity);
|
||||
Task<TEntityType?> Get(TEntityType entity);
|
||||
Task<TEntityType?> Get(params object?[]? keyValues);
|
||||
Task<TEntityType?> GetIncluded(TEntityType entity);
|
||||
Task<TEntityType?> GetIncluded(Func<TEntityType, bool> predicate);
|
||||
Task<IEnumerable<TEntityType>> GetWhereIncluded(Func<TEntityType, bool> predicate);
|
||||
}
|
||||
@@ -19,4 +19,19 @@ public class NovelRepository : BaseRepository<Novel>, INovelRepository
|
||||
.Include(i => i.Tags);
|
||||
}
|
||||
|
||||
public async Task<Novel?> GetNovel(string url)
|
||||
{
|
||||
return await GetIncluded(i => i.Url == url);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Novel>> LookupNovelsByName(string name)
|
||||
{
|
||||
return await GetWhereIncluded(n => n.Title.Contains(name));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Novel>> LookupNovelsByTag(string tag)
|
||||
{
|
||||
return await GetWhereIncluded(n => n.Tags.Any(nt => nt.TagValue.Contains(tag)));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user