Updated lots of stuff, got multi scrape working, need to test not-nullable chapter novel ids with our current model, now supports sqlite and postgres concurrently (and easy add more), need to get it deployed/do auth
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2022-07-16 17:17:43 -04:00
parent eab3399268
commit d98324c11e
73 changed files with 1591 additions and 680 deletions

View File

@@ -1,6 +1,7 @@
using DBConnection.Models;
using DBConnection.Contexts;
using DBConnection.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories;

View File

@@ -1,8 +1,9 @@
using System.Reflection;
using DBConnection.Models;
using DBConnection.Contexts;
using DBConnection.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using NuGet.Configuration;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories;
@@ -30,7 +31,22 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
return entity;
}
public virtual async Task<TEntityType> Upsert(TEntityType entity)
public virtual async Task<IEnumerable<TEntityType>> UpsertMany(IEnumerable<TEntityType> entities, bool saveAfter=true)
{
var newEntities = new List<TEntityType>();
foreach (var entity in entities)
{
newEntities.Add(await Upsert(entity, false));
}
if (saveAfter)
{
await DbContext.SaveChangesAsync();
}
return newEntities;
}
public virtual async Task<TEntityType> Upsert(TEntityType entity, bool saveAfter=true)
{
bool exists = await DbContext.Set<TEntityType>().ContainsAsync(entity);
if (!exists)
@@ -40,10 +56,16 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
else
{
var dbEntry = await GetIncluded(entity);
DbContext.Entry(dbEntry).CurrentValues.SetValues(entity);
entity.DateCreated = dbEntry.DateCreated;
var entry = DbContext.Entry(dbEntry);
entry.CurrentValues.SetValues(entity);
entity = dbEntry;
}
await DbContext.SaveChangesAsync();
if (saveAfter)
{
await DbContext.SaveChangesAsync();
}
return entity;
}

View File

@@ -0,0 +1,18 @@
using DBConnection.Contexts;
using DBConnection.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories;
public class ChapterRepository : BaseRepository<Chapter>, IChapterRepository
{
public ChapterRepository(AppDbContext dbContext) : base(dbContext)
{
}
protected override IQueryable<Chapter> GetAllIncludedQueryable()
{
return DbContext.Chapters.AsQueryable();
}
}

View File

@@ -1,4 +1,4 @@
using DBConnection.Models;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories.Interfaces;

View File

@@ -0,0 +1,8 @@
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories.Interfaces;
public interface IChapterRepository : IRepository<Chapter>
{
}

View File

@@ -1,4 +1,4 @@
using DBConnection.Models;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories.Interfaces;

View File

@@ -1,4 +1,4 @@
using DBConnection.Models;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories.Interfaces;
@@ -10,9 +10,10 @@ public interface IRepository
public interface IRepository<TEntityType> : IRepository where TEntityType : BaseEntity
{
TEntityType Delete(TEntityType entity);
Task<TEntityType> Upsert(TEntityType entity);
Task<TEntityType> Upsert(TEntityType entity, bool saveAfter=true);
Task<TEntityType?> GetIncluded(TEntityType entity);
Task<TEntityType?> GetIncluded(Func<TEntityType, bool> predicate);
Task<IEnumerable<TEntityType>> GetWhereIncluded(Func<TEntityType, bool> predicate);
Task<IEnumerable<TEntityType>> GetAllIncluded();
Task<IEnumerable<TEntityType>> UpsertMany(IEnumerable<TEntityType> entities, bool saveAfter=true);
}

View File

@@ -1,8 +1,7 @@
using DBConnection.Models;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories.Interfaces;
public interface ITagRepository : IRepository<Tag>
{
}

View File

@@ -1,6 +1,7 @@
using DBConnection.Models;
using DBConnection.Contexts;
using DBConnection.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories;
@@ -8,51 +9,44 @@ public class NovelRepository : BaseRepository<Novel>, INovelRepository
{
private readonly IAuthorRepository _authorRepository;
private readonly ITagRepository _tagRepository;
public NovelRepository(AppDbContext dbContext, IAuthorRepository authorRepository, ITagRepository tagRepository) : base(dbContext)
private readonly IChapterRepository _chapterRepository;
public NovelRepository(AppDbContext dbContext, IAuthorRepository authorRepository, ITagRepository tagRepository, IChapterRepository chapterRepository) : base(dbContext)
{
_authorRepository = authorRepository;
_tagRepository = tagRepository;
_chapterRepository = chapterRepository;
}
public override async Task<Novel> Upsert(Novel entity)
public override async Task<Novel> Upsert(Novel entity, bool saveAfter=true)
{
var dbEntity = await GetIncluded(entity) ?? entity;
// Author
dbEntity.Author = await _authorRepository.GetIncluded(entity.Author) ?? entity.Author;
if (entity.Author != null)
{
entity.Author = await _authorRepository.Upsert(entity.Author, saveAfter);
}
//Tags
List<Tag> newTags = new List<Tag>();
foreach (var tag in entity.Tags)
{
newTags.Add(await _tagRepository.GetIncluded(tag) ?? tag);
}
dbEntity.Tags.Clear();
dbEntity.Tags = newTags;
var newTags = await _tagRepository.UpsertMany(entity.Tags, false);
entity.Tags.Clear();
entity.Tags = newTags.ToList();
//chapters
var newChapters = new List<Chapter>();
foreach (var chapter in entity.Chapters.ToList())
{
var existingChapter = await DbContext.Chapters.FindAsync(chapter.Url);
if (existingChapter == null)
{
newChapters.Add(chapter);
}
else
{
existingChapter.Name = chapter.Name;
existingChapter.DateUpdated = chapter.DateUpdated;
newChapters.Add(existingChapter);
}
}
dbEntity.Chapters.Clear();
dbEntity.Chapters = newChapters;
var newChapters = await _chapterRepository.UpsertMany(entity.Chapters, false);
entity.Chapters.Clear();
entity.Chapters = newChapters.ToList();
// update in db
var dbEntity = await GetIncluded(entity) ?? entity;
entity.Guid = dbEntity.Guid;
DbContext.Entry(dbEntity).CurrentValues.SetValues(entity);
if (DbContext.Entry(dbEntity).State == EntityState.Detached)
{
dbEntity.Guid = Guid.NewGuid();
DbContext.Add(dbEntity);
}
await DbContext.SaveChangesAsync();
if (saveAfter)
{
await DbContext.SaveChangesAsync();
}
return dbEntity;
}

View File

@@ -1,6 +1,7 @@
using DBConnection.Models;
using DBConnection.Contexts;
using DBConnection.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories;