Changed db stuff
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using DBConnection.Interfaces;
|
|
||||||
using DBConnection.ModelBuilders;
|
using DBConnection.ModelBuilders;
|
||||||
using DBConnection.Models;
|
using DBConnection.Models;
|
||||||
using DBConnection.Seeders;
|
using DBConnection.Seeders;
|
||||||
@@ -18,12 +17,12 @@ public class AppDbContext : DbContext
|
|||||||
|
|
||||||
private readonly IEnumerable<ISeeder> _seeders =
|
private readonly IEnumerable<ISeeder> _seeders =
|
||||||
from t in Assembly.GetExecutingAssembly().GetTypes()
|
from t in Assembly.GetExecutingAssembly().GetTypes()
|
||||||
where t.IsClass && t.Namespace.Contains(nameof(DBConnection.Seeders)) && typeof(ISeeder).IsAssignableFrom(t)
|
where t.IsClass && (t.Namespace?.Contains(nameof(DBConnection.Seeders)) ?? false) && typeof(ISeeder).IsAssignableFrom(t)
|
||||||
select (ISeeder) Activator.CreateInstance(t);
|
select (ISeeder) Activator.CreateInstance(t);
|
||||||
|
|
||||||
private static readonly IEnumerable<IModelBuilder> ModelBuilders =
|
private static readonly IEnumerable<IModelBuilder> ModelBuilders =
|
||||||
from t in Assembly.GetExecutingAssembly().GetTypes()
|
from t in Assembly.GetExecutingAssembly().GetTypes()
|
||||||
where t.IsClass && t.Namespace.Contains(nameof(DBConnection.ModelBuilders)) && typeof(IModelBuilder).IsAssignableFrom(t)
|
where t.IsClass && (t.Namespace?.Contains(nameof(DBConnection.ModelBuilders)) ?? false) && typeof(IModelBuilder).IsAssignableFrom(t)
|
||||||
select (IModelBuilder) Activator.CreateInstance(t);
|
select (IModelBuilder) Activator.CreateInstance(t);
|
||||||
|
|
||||||
public AppDbContext(DbContextOptions options) : base(options)
|
public AppDbContext(DbContextOptions options) : base(options)
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public static class BuilderExtensions
|
|||||||
opt.UseNpgsql(dbConnectionString);
|
opt.UseNpgsql(dbConnectionString);
|
||||||
});
|
});
|
||||||
Type[] repositories = Assembly.GetExecutingAssembly().GetTypes()
|
Type[] repositories = Assembly.GetExecutingAssembly().GetTypes()
|
||||||
.Where(t => t.IsClass && !t.IsAbstract && t.Namespace.Contains(nameof(DBConnection.Repositories)) && typeof(IRepository).IsAssignableFrom(t)).ToArray();
|
.Where(t => t.IsClass && !t.IsAbstract && (t.Namespace?.Contains(nameof(DBConnection.Repositories)) ?? false) && typeof(IRepository).IsAssignableFrom(t)).ToArray();
|
||||||
foreach (var repo in repositories)
|
foreach (var repo in repositories)
|
||||||
{
|
{
|
||||||
var repoInterface = repo.GetInterfaces()
|
var repoInterface = repo.GetInterfaces()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DBConnection.Interfaces;
|
namespace DBConnection.ModelBuilders;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ModelBuilders are used for customizing DB models with properties not settable via annotations
|
/// ModelBuilders are used for customizing DB models with properties not settable via annotations
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using DBConnection.Interfaces;
|
|
||||||
using DBConnection.Models;
|
using DBConnection.Models;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ public class Author : BaseEntity
|
|||||||
[Key]
|
[Key]
|
||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
public List<Novel> Novels { get; set; }
|
||||||
}
|
}
|
||||||
@@ -38,7 +38,7 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var dbEntry = await Get(entity);
|
var dbEntry = await GetIncluded(entity);
|
||||||
DbContext.Entry(dbEntry).CurrentValues.SetValues(entity);
|
DbContext.Entry(dbEntry).CurrentValues.SetValues(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,14 +46,18 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
|||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual async Task<TEntityType?> Get(TEntityType entity)
|
public virtual async Task<TEntityType?> GetIncluded(TEntityType entity)
|
||||||
{
|
{
|
||||||
var keyValues = GetPrimaryKey(entity);
|
return await GetIncluded(dbEntity => GetPrimaryKey(dbEntity) == GetPrimaryKey(entity));
|
||||||
return await Get(keyValues);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
namespace DBConnection.Repositories.Interfaces;
|
||||||
|
|
||||||
public interface IRepository
|
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);
|
TEntityType Delete(TEntityType entity);
|
||||||
Task<TEntityType> Upsert(TEntityType entity);
|
Task<TEntityType> Upsert(TEntityType entity);
|
||||||
Task<TEntityType?> Get(TEntityType entity);
|
Task<TEntityType?> GetIncluded(TEntityType entity);
|
||||||
Task<TEntityType?> Get(params object?[]? keyValues);
|
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);
|
.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)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,5 +5,8 @@
|
|||||||
"Microsoft.AspNetCore": "Warning"
|
"Microsoft.AspNetCore": "Warning"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"DefaultConnection": "null"
|
||||||
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user