Initial efcore migration and updates to make sure upserting novels (mostly) works. still need to do chapter handling

This commit is contained in:
2022-07-14 23:12:12 -04:00
parent 5402923e9f
commit 5337e7ccb8
25 changed files with 962 additions and 64 deletions

View File

@@ -6,9 +6,32 @@ namespace DBConnection.Repositories;
public class NovelRepository : BaseRepository<Novel>, INovelRepository
{
public NovelRepository(AppDbContext dbContext) : base(dbContext)
private readonly IAuthorRepository _authorRepository;
private readonly ITagRepository _tagRepository;
public NovelRepository(AppDbContext dbContext, IAuthorRepository authorRepository, ITagRepository tagRepository) : base(dbContext)
{
_authorRepository = authorRepository;
_tagRepository = tagRepository;
}
public override async Task<Novel> Upsert(Novel entity)
{
var dbEntity = await GetIncluded(entity) ?? entity;
dbEntity.Author = await _authorRepository.GetIncluded(entity.Author) ?? entity.Author;
List<Tag> newTags = new List<Tag>();
foreach (var tag in dbEntity.Tags)
{
newTags.Add(await _tagRepository.GetIncluded(tag) ?? tag);
}
dbEntity.Tags.Clear();
dbEntity.Tags = newTags;
if (DbContext.Entry(dbEntity).State == EntityState.Detached)
{
DbContext.Add(dbEntity);
}
await DbContext.SaveChangesAsync();
return dbEntity;
}
protected override IQueryable<Novel> GetAllIncludedQueryable()