diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index eda23081d..e745936a3 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -73,7 +73,7 @@ namespace API.Controllers if (!await _unitOfWork.Complete()) return BadRequest("There was a critical issue. Please try again."); - _logger.LogInformation($"Created a new library: {library.Name}"); + _logger.LogInformation("Created a new library: {LibraryName}", library.Name); _taskScheduler.ScanLibrary(library.Id); return Ok(); } @@ -111,7 +111,7 @@ namespace API.Controllers if (user == null) return BadRequest("Could not validate user"); var libraryString = String.Join(",", updateLibraryForUserDto.SelectedLibraries.Select(x => x.Name)); - _logger.LogInformation($"Granting user {updateLibraryForUserDto.Username} access to: {libraryString}"); + _logger.LogInformation("Granting user {UserName} access to: {Libraries}", updateLibraryForUserDto.Username, libraryString); var allLibraries = await _unitOfWork.LibraryRepository.GetLibrariesAsync(); foreach (var library in allLibraries) @@ -133,13 +133,13 @@ namespace API.Controllers if (!_unitOfWork.HasChanges()) { - _logger.LogInformation($"Added: {updateLibraryForUserDto.SelectedLibraries} to {updateLibraryForUserDto.Username}"); + _logger.LogInformation("Added: {SelectedLibraries} to {Username}",libraryString, updateLibraryForUserDto.Username); return Ok(_mapper.Map(user)); } if (await _unitOfWork.Complete()) { - _logger.LogInformation($"Added: {updateLibraryForUserDto.SelectedLibraries} to {updateLibraryForUserDto.Username}"); + _logger.LogInformation("Added: {SelectedLibraries} to {Username}",libraryString, updateLibraryForUserDto.Username); return Ok(_mapper.Map(user)); } diff --git a/API/Controllers/ReaderController.cs b/API/Controllers/ReaderController.cs index 4ead36701..f6f88bc2a 100644 --- a/API/Controllers/ReaderController.cs +++ b/API/Controllers/ReaderController.cs @@ -64,6 +64,88 @@ namespace API.Controllers return Ok(progress?.PagesRead ?? 0); } + [HttpPost("mark-read")] + public async Task MarkRead(MarkReadDto markReadDto) + { + var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); + var volumes = await _unitOfWork.SeriesRepository.GetVolumes(markReadDto.SeriesId); // TODO: Make this async + user.Progresses ??= new List(); + foreach (var volume in volumes) + { + foreach (var chapter in volume.Chapters) + { + var userProgress = user.Progresses.SingleOrDefault(x => x.ChapterId == chapter.Id && x.AppUserId == user.Id); + if (userProgress == null) // I need to get all chapters and generate new user progresses for them? + { + user.Progresses.Add(new AppUserProgress + { + PagesRead = chapter.Pages, + VolumeId = volume.Id, + SeriesId = markReadDto.SeriesId, + ChapterId = chapter.Id + }); + } + else + { + userProgress.PagesRead = chapter.Pages; + userProgress.SeriesId = markReadDto.SeriesId; + userProgress.VolumeId = volume.Id; + } + } + } + + _unitOfWork.UserRepository.Update(user); + + if (await _unitOfWork.Complete()) + { + return Ok(); + } + + + return BadRequest("There was an issue saving progress"); + } + + [HttpPost("mark-unread")] + public async Task MarkUnread(MarkReadDto markReadDto) + { + var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); + var volumes = await _unitOfWork.SeriesRepository.GetVolumes(markReadDto.SeriesId); + user.Progresses ??= new List(); + foreach (var volume in volumes) + { + foreach (var chapter in volume.Chapters) + { + var userProgress = user.Progresses.SingleOrDefault(x => x.ChapterId == chapter.Id && x.AppUserId == user.Id); + if (userProgress == null) + { + user.Progresses.Add(new AppUserProgress + { + PagesRead = 0, + VolumeId = volume.Id, + SeriesId = markReadDto.SeriesId, + ChapterId = chapter.Id + }); + } + else + { + userProgress.PagesRead = 0; + userProgress.SeriesId = markReadDto.SeriesId; + userProgress.VolumeId = volume.Id; + } + } + } + + _unitOfWork.UserRepository.Update(user); + + if (await _unitOfWork.Complete()) + { + return Ok(); + } + + + return BadRequest("There was an issue saving progress"); + } + [HttpPost("bookmark")] public async Task Bookmark(BookmarkDto bookmarkDto) { diff --git a/API/DTOs/MarkReadDto.cs b/API/DTOs/MarkReadDto.cs new file mode 100644 index 000000000..01f03bb83 --- /dev/null +++ b/API/DTOs/MarkReadDto.cs @@ -0,0 +1,7 @@ +namespace API.DTOs +{ + public class MarkReadDto + { + public int SeriesId { get; set; } + } +} \ No newline at end of file diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.cs index 8c41adfd2..8e916b3b7 100644 --- a/API/Data/SeriesRepository.cs +++ b/API/Data/SeriesRepository.cs @@ -116,14 +116,14 @@ namespace API.Data } - public IEnumerable GetVolumes(int seriesId) + public async Task> GetVolumes(int seriesId) { - return _context.Volume + return await _context.Volume .Where(vol => vol.SeriesId == seriesId) .Include(vol => vol.Chapters) .ThenInclude(c => c.Files) .OrderBy(vol => vol.Number) - .ToList(); + .ToListAsync(); } public async Task GetSeriesDtoByIdAsync(int seriesId, int userId) diff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.cs index 92c4d2431..9f77e08e0 100644 --- a/API/Interfaces/ISeriesRepository.cs +++ b/API/Interfaces/ISeriesRepository.cs @@ -26,7 +26,7 @@ namespace API.Interfaces Task> SearchSeries(int[] libraryIds, string searchQuery); Task> GetSeriesForLibraryIdAsync(int libraryId); Task> GetVolumesDtoAsync(int seriesId, int userId); - IEnumerable GetVolumes(int seriesId); + Task> GetVolumes(int seriesId); Task GetSeriesDtoByIdAsync(int seriesId, int userId); Task GetVolumeAsync(int volumeId); Task GetVolumeDtoAsync(int volumeId, int userId); diff --git a/API/Services/MetadataService.cs b/API/Services/MetadataService.cs index 4ae9f5723..e8411e212 100644 --- a/API/Services/MetadataService.cs +++ b/API/Services/MetadataService.cs @@ -78,12 +78,12 @@ namespace API.Services var library = Task.Run(() => _unitOfWork.LibraryRepository.GetLibraryForIdAsync(libraryId)).Result; var allSeries = Task.Run(() => _unitOfWork.SeriesRepository.GetSeriesForLibraryIdAsync(libraryId)).Result.ToList(); - _logger.LogInformation($"Beginning metadata refresh of {library.Name}"); + _logger.LogInformation("Beginning metadata refresh of {LibraryName}", library.Name); foreach (var series in allSeries) { series.NormalizedName = Parser.Parser.Normalize(series.Name); - var volumes = _unitOfWork.SeriesRepository.GetVolumes(series.Id).ToList(); + var volumes = Task.Run(() => _unitOfWork.SeriesRepository.GetVolumes(series.Id)).Result.ToList(); foreach (var volume in volumes) { foreach (var chapter in volume.Chapters) @@ -101,7 +101,7 @@ namespace API.Services if (_unitOfWork.HasChanges() && Task.Run(() => _unitOfWork.Complete()).Result) { - _logger.LogInformation($"Updated metadata for {library.Name} in {sw.ElapsedMilliseconds} ms."); + _logger.LogInformation("Updated metadata for {LibraryName} in {ElapsedMilliseconds} milliseconds", library.Name, sw.ElapsedMilliseconds); } } }