60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using DBConnection.Models;
|
|
using DBConnection.Repositories.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DBConnection.Repositories;
|
|
|
|
public class NovelRepository : BaseRepository<Novel>, INovelRepository
|
|
{
|
|
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()
|
|
{
|
|
return DbContext.Novels
|
|
.Include(i => i.Author)
|
|
.Include(i => i.Chapters)
|
|
.Include(i => i.Tags);
|
|
}
|
|
|
|
public async Task<Novel?> GetNovel(string url)
|
|
{
|
|
return await GetIncluded(i => i.Url == url);
|
|
}
|
|
|
|
public async Task<IEnumerable<Novel>> LookupNovelsByName(string name)
|
|
{
|
|
return await GetWhereIncluded(n => n.Title.Contains(name));
|
|
}
|
|
|
|
public async Task<IEnumerable<Novel>> LookupNovelsByTag(string tag)
|
|
{
|
|
return await GetWhereIncluded(n => n.Tags.Any(nt => nt.TagValue.Contains(tag)));
|
|
}
|
|
|
|
} |