Basics setup, going to change how repos work
This commit is contained in:
59
DBConnection/Repositories/BaseRepository.cs
Normal file
59
DBConnection/Repositories/BaseRepository.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Reflection;
|
||||
using DBConnection.Models;
|
||||
using DBConnection.Repositories.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NuGet.Configuration;
|
||||
|
||||
namespace DBConnection.Repositories;
|
||||
|
||||
public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> where TEntityType : BaseEntity
|
||||
{
|
||||
protected readonly AppDbContext DbContext;
|
||||
|
||||
private object?[]? GetPrimaryKey(TEntityType entity)
|
||||
{
|
||||
var keyProperties = DbContext.Model.FindEntityType(typeof(TEntityType))?.FindPrimaryKey()?.Properties.Select(p => p.Name);
|
||||
return keyProperties?.Select(p => entity.GetType().GetProperty(p)?.GetValue(entity, null)).ToArray();
|
||||
}
|
||||
|
||||
protected abstract IQueryable<TEntityType> GetAllIncludedQueryable();
|
||||
|
||||
public BaseRepository(AppDbContext dbContext)
|
||||
{
|
||||
DbContext = dbContext;
|
||||
}
|
||||
|
||||
public virtual TEntityType Delete(TEntityType entity)
|
||||
{
|
||||
DbContext.Set<TEntityType>().Remove(entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType> Upsert(TEntityType entity)
|
||||
{
|
||||
bool exists = await DbContext.Set<TEntityType>().ContainsAsync(entity);
|
||||
if (!exists)
|
||||
{
|
||||
DbContext.Set<TEntityType>().Add(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
var dbEntry = await Get(entity);
|
||||
DbContext.Entry(dbEntry).CurrentValues.SetValues(entity);
|
||||
}
|
||||
|
||||
await DbContext.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType?> Get(TEntityType entity)
|
||||
{
|
||||
var keyValues = GetPrimaryKey(entity);
|
||||
return await Get(keyValues);
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType?> Get(params object?[]? keyValues)
|
||||
{
|
||||
return await DbContext.Set<TEntityType>().Include(j => j.DateCreated).FindAsync(keyValues);
|
||||
}
|
||||
}
|
||||
8
DBConnection/Repositories/Interfaces/INovelRepository.cs
Normal file
8
DBConnection/Repositories/Interfaces/INovelRepository.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using DBConnection.Models;
|
||||
|
||||
namespace DBConnection.Repositories.Interfaces;
|
||||
|
||||
public interface INovelRepository : IRepository<Novel>
|
||||
{
|
||||
|
||||
}
|
||||
14
DBConnection/Repositories/Interfaces/IRepository.cs
Normal file
14
DBConnection/Repositories/Interfaces/IRepository.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace DBConnection.Repositories.Interfaces;
|
||||
|
||||
public interface IRepository
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public interface IRepository<TEntityType> : IRepository where TEntityType : class
|
||||
{
|
||||
TEntityType Delete(TEntityType entity);
|
||||
Task<TEntityType> Upsert(TEntityType entity);
|
||||
Task<TEntityType?> Get(TEntityType entity);
|
||||
Task<TEntityType?> Get(params object?[]? keyValues);
|
||||
}
|
||||
22
DBConnection/Repositories/NovelRepository.cs
Normal file
22
DBConnection/Repositories/NovelRepository.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using DBConnection.Models;
|
||||
using DBConnection.Repositories.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DBConnection.Repositories;
|
||||
|
||||
public class NovelRepository : BaseRepository<Novel>, INovelRepository
|
||||
{
|
||||
|
||||
public NovelRepository(AppDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IQueryable<Novel> GetAllIncludedQueryable()
|
||||
{
|
||||
return DbContext.Novels
|
||||
.Include(i => i.Author)
|
||||
.Include(i => i.Chapters)
|
||||
.Include(i => i.Tags);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user