Very messy code that implements read status tracking. Needs major cleanup.

This commit is contained in:
Joseph Milazzo 2021-01-17 15:05:27 -06:00
parent e0d70d16f9
commit effdf07cef
24 changed files with 2179 additions and 24 deletions

View file

@ -38,7 +38,8 @@ namespace API.Controllers
[HttpPost("register")]
public async Task<ActionResult<UserDto>> Register(RegisterDto registerDto)
{
if (await UserExists(registerDto.Username))
if (await _userManager.Users.AnyAsync(x => x.UserName == registerDto.Username))
{
return BadRequest("Username is taken.");
}
@ -88,9 +89,9 @@ namespace API.Controllers
};
}
private async Task<bool> UserExists(string username)
{
return await _userManager.Users.AnyAsync(user => user.UserName == username.ToLower());
}
// private async Task<bool> UserExists(string username)
// {
// return await _userManager.Users.AnyAsync(user => user.UserName == username.ToLower());
// }
}
}

View file

@ -147,8 +147,13 @@ namespace API.Controllers
}
[HttpGet("series")]
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId)
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, bool forUser = false)
{
if (forUser)
{
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _seriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id));
}
return Ok(await _seriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId));
}

View file

@ -1,7 +1,15 @@
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using API.Data;
using API.DTOs;
using API.Entities;
using API.Extensions;
using API.Interfaces;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace API.Controllers
{
@ -9,11 +17,23 @@ namespace API.Controllers
{
private readonly IDirectoryService _directoryService;
private readonly ICacheService _cacheService;
private readonly ILogger<ReaderController> _logger;
private readonly UserManager<AppUser> _userManager;
private readonly DataContext _dataContext; // TODO: Refactor code into repo
private readonly IUserRepository _userRepository;
private readonly ISeriesRepository _seriesRepository;
public ReaderController(IDirectoryService directoryService, ICacheService cacheService)
public ReaderController(IDirectoryService directoryService, ICacheService cacheService,
ILogger<ReaderController> logger, UserManager<AppUser> userManager, DataContext dataContext,
IUserRepository userRepository, ISeriesRepository seriesRepository)
{
_directoryService = directoryService;
_cacheService = cacheService;
_logger = logger;
_userManager = userManager;
_dataContext = dataContext;
_userRepository = userRepository;
_seriesRepository = seriesRepository;
}
[HttpGet("image")]
@ -28,5 +48,54 @@ namespace API.Controllers
return Ok(file);
}
[HttpGet("get-bookmark")]
public async Task<ActionResult<int>> GetBookmark(int volumeId)
{
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
if (user.Progresses == null) return Ok(0);
var progress = user.Progresses.SingleOrDefault(x => x.AppUserId == user.Id && x.VolumeId == volumeId);
if (progress != null) return Ok(progress.PagesRead);
return Ok(0);
}
[HttpPost("bookmark")]
public async Task<ActionResult> Bookmark(BookmarkDto bookmarkDto)
{
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
_logger.LogInformation($"Saving {user.UserName} progress for {bookmarkDto.VolumeId} to page {bookmarkDto.PageNum}");
user.Progresses ??= new List<AppUserProgress>();
var userProgress = user.Progresses.SingleOrDefault(x => x.VolumeId == bookmarkDto.VolumeId && x.AppUserId == user.Id);
if (userProgress == null)
{
user.Progresses.Add(new AppUserProgress
{
PagesRead = bookmarkDto.PageNum, // TODO: PagesRead is misleading. Should it be PageNumber or PagesRead (+1)?
VolumeId = bookmarkDto.VolumeId,
SeriesId = bookmarkDto.SeriesId,
});
}
else
{
userProgress.PagesRead = bookmarkDto.PageNum;
userProgress.SeriesId = bookmarkDto.SeriesId;
}
_userRepository.Update(user);
if (await _userRepository.SaveAllAsync())
{
return Ok();
}
return BadRequest("Could not save progress");
}
}
}

View file

@ -19,16 +19,18 @@ namespace API.Controllers
private readonly ITaskScheduler _taskScheduler;
private readonly ISeriesRepository _seriesRepository;
private readonly ICacheService _cacheService;
private readonly IUserRepository _userRepository;
public SeriesController(ILogger<SeriesController> logger, IMapper mapper,
ITaskScheduler taskScheduler, ISeriesRepository seriesRepository,
ICacheService cacheService)
ICacheService cacheService, IUserRepository userRepository)
{
_logger = logger;
_mapper = mapper;
_taskScheduler = taskScheduler;
_seriesRepository = seriesRepository;
_cacheService = cacheService;
_userRepository = userRepository;
}
[HttpGet("{seriesId}")]

View file

@ -46,6 +46,8 @@ namespace API.Controllers
[HttpGet("has-library-access")]
public async Task<ActionResult<bool>> HasLibraryAccess(int libraryId)
{
// TODO: refactor this to use either userexists or usermanager
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
if (user == null) return BadRequest("Could not validate user");