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

@ -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");
}
}
}