Skip to content

Commit

Permalink
Merge pull request #29 from Kareadita/feature/ratings
Browse files Browse the repository at this point in the history
Implemented ability to leave a rating (up to 5 stars) and a text revi…
  • Loading branch information
majora2007 authored Jan 20, 2021
2 parents 767f835 + 925a009 commit 9fb16fd
Show file tree
Hide file tree
Showing 17 changed files with 882 additions and 31 deletions.
36 changes: 35 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,38 @@ appsettings.json
/API/kavita.db-wal
/API/Hangfire.db
/API/Hangfire-log.db
cache/
cache/
/API/wwwroot/assets/images/image-placeholder.jpg
/API/wwwroot/assets/images/mock-cover.jpg
/API/wwwroot/assets/images/preset-light.png
/API/wwwroot/assets/themes/plex/_bootswatch.scss
/API/wwwroot/assets/themes/plex/_variables.scss
/API/wwwroot/admin-admin-module.js
/API/wwwroot/admin-admin-module.js.map
/API/wwwroot/fa-brands-400.eot
/API/wwwroot/fa-brands-400.svg
/API/wwwroot/fa-brands-400.ttf
/API/wwwroot/fa-brands-400.woff
/API/wwwroot/fa-brands-400.woff2
/API/wwwroot/fa-regular-400.eot
/API/wwwroot/fa-regular-400.svg
/API/wwwroot/fa-regular-400.ttf
/API/wwwroot/fa-regular-400.woff
/API/wwwroot/fa-regular-400.woff2
/API/wwwroot/fa-solid-900.eot
/API/wwwroot/fa-solid-900.svg
/API/wwwroot/fa-solid-900.ttf
/API/wwwroot/fa-solid-900.woff
/API/wwwroot/fa-solid-900.woff2
/API/wwwroot/favicon.ico
/API/wwwroot/index.html
/API/wwwroot/main.js
/API/wwwroot/main.js.map
/API/wwwroot/polyfills.js
/API/wwwroot/polyfills.js.map
/API/wwwroot/runtime.js
/API/wwwroot/runtime.js.map
/API/wwwroot/styles.css
/API/wwwroot/styles.css.map
/API/wwwroot/vendor.js
/API/wwwroot/vendor.js.map
14 changes: 14 additions & 0 deletions API/Controllers/FallbackController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.IO;
using API.Services;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
public class FallbackController : Controller
{
public ActionResult Index()
{
return PhysicalFile(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "index.html"), "text/HTML");
}
}
}
28 changes: 26 additions & 2 deletions API/Controllers/SeriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using System.Threading.Tasks;
using API.DTOs;
using API.Entities;
using API.Extensions;
using API.Interfaces;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -26,7 +27,8 @@ public SeriesController(ILogger<SeriesController> logger, ITaskScheduler taskSch
[HttpGet("{seriesId}")]
public async Task<ActionResult<SeriesDto>> GetSeries(int seriesId)
{
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId));
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId, user.Id));
}

[Authorize(Policy = "RequireAdminRole")]
Expand Down Expand Up @@ -55,7 +57,29 @@ public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId)
[HttpGet("volume")]
public async Task<ActionResult<VolumeDto>> GetVolume(int volumeId)
{
return Ok(await _unitOfWork.SeriesRepository.GetVolumeDtoAsync(volumeId));
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _unitOfWork.SeriesRepository.GetVolumeDtoAsync(volumeId, user.Id));
}

[HttpPost("update-rating")]
public async Task<ActionResult> UpdateSeriesRating(UpdateSeriesRatingDto updateSeriesRatingDto)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
var userRating = await _unitOfWork.UserRepository.GetUserRating(updateSeriesRatingDto.SeriesId, user.Id) ??
new AppUserRating();

userRating.Rating = updateSeriesRatingDto.UserRating;
userRating.Review = updateSeriesRatingDto.UserReview;
userRating.SeriesId = updateSeriesRatingDto.SeriesId;

_unitOfWork.UserRepository.AddRatingTracking(userRating);
user.Ratings ??= new List<AppUserRating>();
user.Ratings.Add(userRating);


if (!await _unitOfWork.Complete()) return BadRequest("There was a critical error.");

return Ok();
}
}
}
8 changes: 8 additions & 0 deletions API/DTOs/SeriesDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ public class SeriesDto
/// Sum of pages read from linked Volumes. Calculated at API-time.
/// </summary>
public int PagesRead { get; set; }
/// <summary>
/// Rating from logged in user. Calculated at API-time.
/// </summary>
public int UserRating { get; set; }
/// <summary>
/// Review from logged in user. Calculated at API-time.
/// </summary>
public string UserReview { get; set; }
}
}
12 changes: 12 additions & 0 deletions API/DTOs/UpdateSeriesRatingDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace API.DTOs
{
public class UpdateSeriesRatingDto
{
public int SeriesId { get; init; }
public int UserRating { get; init; }
[MaxLength(1000)]
public string UserReview { get; init; }
}
}
1 change: 1 addition & 0 deletions API/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public DataContext(DbContextOptions options) : base(options)
public DbSet<Volume> Volume { get; set; }
public DbSet<AppUser> AppUser { get; set; }
public DbSet<AppUserProgress> AppUserProgresses { get; set; }
public DbSet<AppUserRating> AppUserRating { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
Expand Down
Loading

0 comments on commit 9fb16fd

Please sign in to comment.