Initial efcore migration and updates to make sure upserting novels (mostly) works. still need to do chapter handling
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user