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;
@@ -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;
}