diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.cs index 7df62afc2..c8eee5db5 100644 --- a/API/Controllers/SeriesController.cs +++ b/API/Controllers/SeriesController.cs @@ -56,9 +56,14 @@ namespace API.Controllers } [HttpGet("volumes")] - public async Task>> GetVolumes(int seriesId) + public async Task>> GetVolumes(int seriesId, bool forUser = true) { - return Ok(await _seriesRepository.GetVolumesDtoAsync(seriesId)); + if (forUser) + { + var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername()); + return Ok(await _seriesRepository.GetVolumesDtoAsync(seriesId, user.Id)); + } + return Ok(await _seriesRepository.GetVolumesDtoAsync(seriesId)); // TODO: Refactor out forUser = false since everything is user based } [HttpGet("volume")] diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.cs index ff48c31af..2be26063d 100644 --- a/API/Data/SeriesRepository.cs +++ b/API/Data/SeriesRepository.cs @@ -68,12 +68,26 @@ namespace API.Data return series; } - public async Task> GetVolumesDtoAsync(int seriesId) + public async Task> GetVolumesDtoAsync(int seriesId, int userId = 0) { - return await _context.Volume + var volumes = await _context.Volume .Where(vol => vol.SeriesId == seriesId) .OrderBy(volume => volume.Number) .ProjectTo(_mapper.ConfigurationProvider).ToListAsync(); + if (userId > 0) + { + var userProgress = await _context.AppUserProgresses + .Where(p => p.AppUserId == userId && volumes.Select(s => s.Id).Contains(p.VolumeId)) + .ToListAsync(); + + foreach (var v in volumes) + { + v.PagesRead = userProgress.Where(p => p.VolumeId == v.Id).Sum(p => p.PagesRead); + } + } + + return volumes; + } public IEnumerable GetVolumes(int seriesId) diff --git a/API/Entities/AppUserProgress.cs b/API/Entities/AppUserProgress.cs index 2f9274e26..e9d6a2279 100644 --- a/API/Entities/AppUserProgress.cs +++ b/API/Entities/AppUserProgress.cs @@ -11,7 +11,6 @@ public AppUser AppUser { get; set; } public int AppUserId { get; set; } public int VolumeId { get; set; } - public int SeriesId { get; set; } // shortcut - //public bool VolumeCompleted { get; set; } // This will be set true if PagesRead == Sum of MangaFiles on volume + public int SeriesId { get; set; } } } \ No newline at end of file diff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.cs index 1400431d7..3657a2a6e 100644 --- a/API/Interfaces/ISeriesRepository.cs +++ b/API/Interfaces/ISeriesRepository.cs @@ -13,12 +13,12 @@ namespace API.Interfaces Series GetSeriesByName(string name); bool SaveAll(); Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId = 0); - Task> GetVolumesDtoAsync(int seriesId); + Task> GetVolumesDtoAsync(int seriesId, int userId = 0); IEnumerable GetVolumes(int seriesId); Task GetSeriesDtoByIdAsync(int seriesId); Task GetVolumeAsync(int volumeId); - Task GetVolumeDtoAsync(int volumeId); + Task GetVolumeDtoAsync(int volumeId); // TODO: Likely need to update here Task> GetVolumesForSeriesAsync(int[] seriesIds); Task DeleteSeriesAsync(int seriesId);