This commit is contained in:
@@ -47,6 +47,11 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
||||
return entity;
|
||||
}
|
||||
|
||||
public virtual async Task<IEnumerable<TEntityType>> GetAllIncluded()
|
||||
{
|
||||
return await GetWhereIncluded(i => true);
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType?> GetIncluded(TEntityType entity)
|
||||
{
|
||||
return await GetIncluded(dbEntity => GetPrimaryKey(dbEntity).SequenceEqual(GetPrimaryKey(entity)));
|
||||
|
||||
@@ -4,5 +4,5 @@ namespace DBConnection.Repositories.Interfaces;
|
||||
|
||||
public interface INovelRepository : IRepository<Novel>
|
||||
{
|
||||
|
||||
Task<Novel?> GetNovel(Guid guid);
|
||||
}
|
||||
@@ -14,4 +14,5 @@ public interface IRepository<TEntityType> : IRepository where TEntityType : Base
|
||||
Task<TEntityType?> GetIncluded(TEntityType entity);
|
||||
Task<TEntityType?> GetIncluded(Func<TEntityType, bool> predicate);
|
||||
Task<IEnumerable<TEntityType>> GetWhereIncluded(Func<TEntityType, bool> predicate);
|
||||
Task<IEnumerable<TEntityType>> GetAllIncluded();
|
||||
}
|
||||
@@ -17,16 +17,38 @@ public class NovelRepository : BaseRepository<Novel>, INovelRepository
|
||||
public override async Task<Novel> Upsert(Novel entity)
|
||||
{
|
||||
var dbEntity = await GetIncluded(entity) ?? entity;
|
||||
// Author
|
||||
dbEntity.Author = await _authorRepository.GetIncluded(entity.Author) ?? entity.Author;
|
||||
//Tags
|
||||
List<Tag> newTags = new List<Tag>();
|
||||
foreach (var tag in dbEntity.Tags)
|
||||
foreach (var tag in entity.Tags)
|
||||
{
|
||||
newTags.Add(await _tagRepository.GetIncluded(tag) ?? tag);
|
||||
}
|
||||
dbEntity.Tags.Clear();
|
||||
dbEntity.Tags = newTags;
|
||||
//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;
|
||||
// update in db
|
||||
if (DbContext.Entry(dbEntity).State == EntityState.Detached)
|
||||
{
|
||||
dbEntity.Guid = Guid.NewGuid();
|
||||
DbContext.Add(dbEntity);
|
||||
}
|
||||
|
||||
@@ -42,6 +64,11 @@ public class NovelRepository : BaseRepository<Novel>, INovelRepository
|
||||
.Include(i => i.Tags);
|
||||
}
|
||||
|
||||
public async Task<Novel?> GetNovel(Guid guid)
|
||||
{
|
||||
return await GetIncluded(i => i.Guid == guid);
|
||||
}
|
||||
|
||||
public async Task<Novel?> GetNovel(string url)
|
||||
{
|
||||
return await GetIncluded(i => i.Url == url);
|
||||
|
||||
Reference in New Issue
Block a user