Initial efcore migration and updates to make sure upserting novels (mostly) works. still need to do chapter handling
This commit is contained in:
17
DBConnection/Repositories/AuthorRepository.cs
Normal file
17
DBConnection/Repositories/AuthorRepository.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using DBConnection.Models;
|
||||
using DBConnection.Repositories.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DBConnection.Repositories;
|
||||
|
||||
public class AuthorRepository : BaseRepository<Author>, IAuthorRepository
|
||||
{
|
||||
public AuthorRepository(AppDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IQueryable<Author> GetAllIncludedQueryable()
|
||||
{
|
||||
return DbContext.Authors.Include(i => i.Novels);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,8 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
||||
private object?[]? GetPrimaryKey(TEntityType entity)
|
||||
{
|
||||
var keyProperties = DbContext.Model.FindEntityType(typeof(TEntityType))?.FindPrimaryKey()?.Properties.Select(p => p.Name);
|
||||
return keyProperties?.Select(p => entity.GetType().GetProperty(p)?.GetValue(entity, null)).ToArray();
|
||||
var ret = keyProperties?.Select(p => entity.GetType().GetProperty(p)?.GetValue(entity, null)).ToArray();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected abstract IQueryable<TEntityType> GetAllIncludedQueryable();
|
||||
@@ -48,7 +49,7 @@ public abstract class BaseRepository<TEntityType> : IRepository<TEntityType> whe
|
||||
|
||||
public virtual async Task<TEntityType?> GetIncluded(TEntityType entity)
|
||||
{
|
||||
return await GetIncluded(dbEntity => GetPrimaryKey(dbEntity) == GetPrimaryKey(entity));
|
||||
return await GetIncluded(dbEntity => GetPrimaryKey(dbEntity).SequenceEqual(GetPrimaryKey(entity)));
|
||||
}
|
||||
|
||||
public virtual async Task<TEntityType?> GetIncluded(Func<TEntityType, bool> predicate)
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
using DBConnection.Models;
|
||||
|
||||
namespace DBConnection.Repositories.Interfaces;
|
||||
|
||||
public interface IAuthorRepository : IRepository<Author>
|
||||
{
|
||||
|
||||
}
|
||||
8
DBConnection/Repositories/Interfaces/ITagRepository.cs
Normal file
8
DBConnection/Repositories/Interfaces/ITagRepository.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using DBConnection.Models;
|
||||
|
||||
namespace DBConnection.Repositories.Interfaces;
|
||||
|
||||
public interface ITagRepository : IRepository<Tag>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
17
DBConnection/Repositories/TagRepository.cs
Normal file
17
DBConnection/Repositories/TagRepository.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using DBConnection.Models;
|
||||
using DBConnection.Repositories.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DBConnection.Repositories;
|
||||
|
||||
public class TagRepository : BaseRepository<Tag>, ITagRepository
|
||||
{
|
||||
public TagRepository(AppDbContext dbContext) : base(dbContext)
|
||||
{
|
||||
}
|
||||
|
||||
protected override IQueryable<Tag> GetAllIncludedQueryable()
|
||||
{
|
||||
return DbContext.Tags.Include(i => i.Novels);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user