Finished adding user support and ability to update specific novels or set your last read chapter

This commit is contained in:
2022-07-17 20:34:06 -04:00
parent e4529e11c0
commit b5c4146d4d
34 changed files with 589 additions and 59 deletions

View File

@@ -0,0 +1,49 @@
using DBConnection.Contexts;
using DBConnection.Repositories.Interfaces;
using Microsoft.EntityFrameworkCore;
using Treestar.Shared.Models.DBDomain;
namespace DBConnection.Repositories;
public class UserRepository : BaseRepository<User>, IUserRepository
{
private readonly INovelRepository _novelRepository;
public UserRepository(AppDbContext dbContext, INovelRepository novelRepository) : base(dbContext)
{
_novelRepository = novelRepository;
}
protected override IQueryable<User> GetAllIncludedQueryable()
{
return DbContext.Users.Include(u => u.WatchedNovels);
}
public async Task<User> AssignNovelsToUser(User user, List<Novel> novels)
{
var dbUser = await GetIncluded(user);
if (dbUser == null)
{
return user;
}
var dbNovels = await _novelRepository.GetWhereIncluded(novels);
var newNovels = dbNovels.Except(dbUser.WatchedNovels.Select(un => un.Novel));
var newUserNovels = newNovels.Select(n => new UserNovel
{
Novel = n,
User = dbUser
});
dbUser.WatchedNovels.AddRange(newUserNovels);
await DbContext.SaveChangesAsync();
return dbUser;
}
public async Task<User> UpdateLastChapterRead(User user, Novel novel, int chapterRead)
{
var dbUser = await GetIncluded(user);
var userNovel = dbUser.WatchedNovels.FirstOrDefault(i => i.NovelUrl == novel.Url);
userNovel.LastChapterRead = chapterRead;
await DbContext.SaveChangesAsync();
return dbUser;
}
}