Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Implemented ability to leave a rating (up to 5 stars) and a text review (not UI supported until v0.2).
Joseph Milazzoac993a59bae2
diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.csindex af2d36bb6..445a28107 100644--- a/API/Controllers/SeriesController.cs+++ b/API/Controllers/SeriesController.cs@@ -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;@@ -26,7 +27,8 @@ namespace API.Controllers[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")]@@ -55,7 +57,29 @@ namespace API.Controllers[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();}}}\ No newline at end of filediff --git a/API/DTOs/SeriesDto.cs b/API/DTOs/SeriesDto.csindex 50160ab5c..bf9fc8a45 100644--- a/API/DTOs/SeriesDto.cs+++ b/API/DTOs/SeriesDto.cs@@ -13,5 +13,13 @@/// 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; }}}\ No newline at end of filediff --git a/API/DTOs/UpdateSeriesRatingDto.cs b/API/DTOs/UpdateSeriesRatingDto.csnew file mode 100644index 000000000..d8b8dac2d--- /dev/null+++ b/API/DTOs/UpdateSeriesRatingDto.cs@@ -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; }+ }+}\ No newline at end of filediff --git a/API/Data/DataContext.cs b/API/Data/DataContext.csindex 2bb9424c0..a8567849a 100644--- a/API/Data/DataContext.cs+++ b/API/Data/DataContext.cs@@ -23,6 +23,7 @@ namespace API.Datapublic 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){diff --git a/API/Data/Migrations/20210119213837_AppUserRatingAndReviews.Designer.cs b/API/Data/Migrations/20210119213837_AppUserRatingAndReviews.Designer.csnew file mode 100644index 000000000..e68e9e11b--- /dev/null+++ b/API/Data/Migrations/20210119213837_AppUserRatingAndReviews.Designer.cs@@ -0,0 +1,603 @@+// <auto-generated />+using System;+using API.Data;+using Microsoft.EntityFrameworkCore;+using Microsoft.EntityFrameworkCore.Infrastructure;+using Microsoft.EntityFrameworkCore.Migrations;+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;++namespace API.Data.Migrations+{+ [DbContext(typeof(DataContext))]+ [Migration("20210119213837_AppUserRatingAndReviews")]+ partial class AppUserRatingAndReviews+ {+ protected override void BuildTargetModel(ModelBuilder modelBuilder)+ {+#pragma warning disable 612, 618+ modelBuilder+ .HasAnnotation("ProductVersion", "5.0.1");++ modelBuilder.Entity("API.Entities.AppRole", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ConcurrencyStamp")+ .IsConcurrencyToken()+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("NormalizedName")+ .IsUnique()+ .HasDatabaseName("RoleNameIndex");++ b.ToTable("AspNetRoles");+ });++ modelBuilder.Entity("API.Entities.AppUser", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AccessFailedCount")+ .HasColumnType("INTEGER");++ b.Property<string>("ConcurrencyStamp")+ .IsConcurrencyToken()+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<string>("Email")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<bool>("EmailConfirmed")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastActive")+ .HasColumnType("TEXT");++ b.Property<bool>("LockoutEnabled")+ .HasColumnType("INTEGER");++ b.Property<DateTimeOffset?>("LockoutEnd")+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedEmail")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedUserName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("PasswordHash")+ .HasColumnType("TEXT");++ b.Property<string>("PhoneNumber")+ .HasColumnType("TEXT");++ b.Property<bool>("PhoneNumberConfirmed")+ .HasColumnType("INTEGER");++ b.Property<uint>("RowVersion")+ .IsConcurrencyToken()+ .HasColumnType("INTEGER");++ b.Property<string>("SecurityStamp")+ .HasColumnType("TEXT");++ b.Property<bool>("TwoFactorEnabled")+ .HasColumnType("INTEGER");++ b.Property<string>("UserName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("NormalizedEmail")+ .HasDatabaseName("EmailIndex");++ b.HasIndex("NormalizedUserName")+ .IsUnique()+ .HasDatabaseName("UserNameIndex");++ b.ToTable("AspNetUsers");+ });++ modelBuilder.Entity("API.Entities.AppUserProgress", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<int>("PagesRead")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("AppUserProgresses");+ });++ modelBuilder.Entity("API.Entities.AppUserRating", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<int>("Rating")+ .HasColumnType("INTEGER");++ b.Property<string>("Review")+ .HasColumnType("TEXT");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("AppUserRating");+ });++ modelBuilder.Entity("API.Entities.AppUserRole", b =>+ {+ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.Property<int>("RoleId")+ .HasColumnType("INTEGER");++ b.HasKey("UserId", "RoleId");++ b.HasIndex("RoleId");++ b.ToTable("AspNetUserRoles");+ });++ modelBuilder.Entity("API.Entities.FolderPath", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("LibraryId")+ .HasColumnType("INTEGER");++ b.Property<string>("Path")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("LibraryId");++ b.ToTable("FolderPath");+ });++ modelBuilder.Entity("API.Entities.Library", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<int>("Type")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.ToTable("Library");+ });++ modelBuilder.Entity("API.Entities.MangaFile", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("Chapter")+ .HasColumnType("INTEGER");++ b.Property<string>("FilePath")+ .HasColumnType("TEXT");++ b.Property<int>("Format")+ .HasColumnType("INTEGER");++ b.Property<int>("NumberOfPages")+ .HasColumnType("INTEGER");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("VolumeId");++ b.ToTable("MangaFile");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<byte[]>("CoverImage")+ .HasColumnType("BLOB");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<int>("LibraryId")+ .HasColumnType("INTEGER");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<string>("OriginalName")+ .HasColumnType("TEXT");++ b.Property<int>("Pages")+ .HasColumnType("INTEGER");++ b.Property<string>("SortName")+ .HasColumnType("TEXT");++ b.Property<string>("Summary")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("LibraryId");++ b.ToTable("Series");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<byte[]>("CoverImage")+ .HasColumnType("BLOB");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<int>("Number")+ .HasColumnType("INTEGER");++ b.Property<int>("Pages")+ .HasColumnType("INTEGER");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("SeriesId");++ b.ToTable("Volume");+ });++ modelBuilder.Entity("AppUserLibrary", b =>+ {+ b.Property<int>("AppUsersId")+ .HasColumnType("INTEGER");++ b.Property<int>("LibrariesId")+ .HasColumnType("INTEGER");++ b.HasKey("AppUsersId", "LibrariesId");++ b.HasIndex("LibrariesId");++ b.ToTable("AppUserLibrary");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ClaimType")+ .HasColumnType("TEXT");++ b.Property<string>("ClaimValue")+ .HasColumnType("TEXT");++ b.Property<int>("RoleId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("RoleId");++ b.ToTable("AspNetRoleClaims");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ClaimType")+ .HasColumnType("TEXT");++ b.Property<string>("ClaimValue")+ .HasColumnType("TEXT");++ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("UserId");++ b.ToTable("AspNetUserClaims");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>+ {+ b.Property<string>("LoginProvider")+ .HasColumnType("TEXT");++ b.Property<string>("ProviderKey")+ .HasColumnType("TEXT");++ b.Property<string>("ProviderDisplayName")+ .HasColumnType("TEXT");++ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.HasKey("LoginProvider", "ProviderKey");++ b.HasIndex("UserId");++ b.ToTable("AspNetUserLogins");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>+ {+ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.Property<string>("LoginProvider")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<string>("Value")+ .HasColumnType("TEXT");++ b.HasKey("UserId", "LoginProvider", "Name");++ b.ToTable("AspNetUserTokens");+ });++ modelBuilder.Entity("API.Entities.AppUserProgress", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("Progresses")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.AppUserRating", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("Ratings")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });++ modelBuilder.Entity("API.Entities.AppUserRole", b =>+ {+ b.HasOne("API.Entities.AppRole", "Role")+ .WithMany("UserRoles")+ .HasForeignKey("RoleId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.AppUser", "User")+ .WithMany("UserRoles")+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Role");++ b.Navigation("User");+ });++ modelBuilder.Entity("API.Entities.FolderPath", b =>+ {+ b.HasOne("API.Entities.Library", "Library")+ .WithMany("Folders")+ .HasForeignKey("LibraryId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Library");+ });++ modelBuilder.Entity("API.Entities.MangaFile", b =>+ {+ b.HasOne("API.Entities.Volume", "Volume")+ .WithMany("Files")+ .HasForeignKey("VolumeId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Volume");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.HasOne("API.Entities.Library", "Library")+ .WithMany("Series")+ .HasForeignKey("LibraryId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Library");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.HasOne("API.Entities.Series", "Series")+ .WithMany("Volumes")+ .HasForeignKey("SeriesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Series");+ });++ modelBuilder.Entity("AppUserLibrary", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("AppUsersId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.Library", null)+ .WithMany()+ .HasForeignKey("LibrariesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>+ {+ b.HasOne("API.Entities.AppRole", null)+ .WithMany()+ .HasForeignKey("RoleId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("API.Entities.AppRole", b =>+ {+ b.Navigation("UserRoles");+ });++ modelBuilder.Entity("API.Entities.AppUser", b =>+ {+ b.Navigation("Progresses");++ b.Navigation("Ratings");++ b.Navigation("UserRoles");+ });++ modelBuilder.Entity("API.Entities.Library", b =>+ {+ b.Navigation("Folders");++ b.Navigation("Series");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.Navigation("Volumes");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.Navigation("Files");+ });+#pragma warning restore 612, 618+ }+ }+}diff --git a/API/Data/Migrations/20210119213837_AppUserRatingAndReviews.cs b/API/Data/Migrations/20210119213837_AppUserRatingAndReviews.csnew file mode 100644index 000000000..98db3af2b--- /dev/null+++ b/API/Data/Migrations/20210119213837_AppUserRatingAndReviews.cs@@ -0,0 +1,43 @@+using Microsoft.EntityFrameworkCore.Migrations;++namespace API.Data.Migrations+{+ public partial class AppUserRatingAndReviews : Migration+ {+ protected override void Up(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.CreateTable(+ name: "AppUserRating",+ columns: table => new+ {+ Id = table.Column<int>(type: "INTEGER", nullable: false)+ .Annotation("Sqlite:Autoincrement", true),+ Rating = table.Column<int>(type: "INTEGER", nullable: false),+ Review = table.Column<string>(type: "TEXT", nullable: true),+ SeriesId = table.Column<int>(type: "INTEGER", nullable: false),+ AppUserId = table.Column<int>(type: "INTEGER", nullable: false)+ },+ constraints: table =>+ {+ table.PrimaryKey("PK_AppUserRating", x => x.Id);+ table.ForeignKey(+ name: "FK_AppUserRating_AspNetUsers_AppUserId",+ column: x => x.AppUserId,+ principalTable: "AspNetUsers",+ principalColumn: "Id",+ onDelete: ReferentialAction.Cascade);+ });++ migrationBuilder.CreateIndex(+ name: "IX_AppUserRating_AppUserId",+ table: "AppUserRating",+ column: "AppUserId");+ }++ protected override void Down(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.DropTable(+ name: "AppUserRating");+ }+ }+}diff --git a/API/Data/Migrations/DataContextModelSnapshot.cs b/API/Data/Migrations/DataContextModelSnapshot.csindex 7d345a870..cbb687e1d 100644--- a/API/Data/Migrations/DataContextModelSnapshot.cs+++ b/API/Data/Migrations/DataContextModelSnapshot.cs@@ -143,6 +143,31 @@ namespace API.Data.Migrationsb.ToTable("AppUserProgresses");});+ modelBuilder.Entity("API.Entities.AppUserRating", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AppUserId")+ .HasColumnType("INTEGER");++ b.Property<int>("Rating")+ .HasColumnType("INTEGER");++ b.Property<string>("Review")+ .HasColumnType("TEXT");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("AppUserId");++ b.ToTable("AppUserRating");+ });+modelBuilder.Entity("API.Entities.AppUserRole", b =>{b.Property<int>("UserId")@@ -415,6 +440,17 @@ namespace API.Data.Migrationsb.Navigation("AppUser");});+ modelBuilder.Entity("API.Entities.AppUserRating", b =>+ {+ b.HasOne("API.Entities.AppUser", "AppUser")+ .WithMany("Ratings")+ .HasForeignKey("AppUserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("AppUser");+ });+modelBuilder.Entity("API.Entities.AppUserRole", b =>{b.HasOne("API.Entities.AppRole", "Role")@@ -538,6 +574,8 @@ namespace API.Data.Migrations{b.Navigation("Progresses");+ b.Navigation("Ratings");+b.Navigation("UserRoles");});diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.csindex 510a21388..a8a5c1f98 100644--- a/API/Data/SeriesRepository.cs+++ b/API/Data/SeriesRepository.cs@@ -70,17 +70,9 @@ namespace API.Data.ProjectTo<SeriesDto>(_mapper.ConfigurationProvider).ToListAsync();- if (userId > 0)- {- var userProgress = await _context.AppUserProgresses- .Where(p => p.AppUserId == userId && series.Select(s => s.Id).Contains(p.SeriesId))- .ToListAsync();-- foreach (var s in series)- {- s.PagesRead = userProgress.Where(p => p.SeriesId == s.Id).Sum(p => p.PagesRead);- }- }++ await AddSeriesModifiers(userId, series);+Console.WriteLine("Processed GetSeriesDtoForLibraryIdAsync in {0} milliseconds", sw.ElapsedMilliseconds);return series;@@ -94,20 +86,14 @@ namespace API.Data.ProjectTo<VolumeDto>(_mapper.ConfigurationProvider).AsNoTracking().ToListAsync();- var userProgress = await _context.AppUserProgresses- .Where(p => p.AppUserId == userId && volumes.Select(s => s.Id).Contains(p.VolumeId))- .AsNoTracking()- .ToListAsync();-- foreach (var v in volumes)- {- v.PagesRead = userProgress.Where(p => p.VolumeId == v.Id).Sum(p => p.PagesRead);- }++ await AddVolumeModifiers(userId, volumes);return volumes;}+public IEnumerable<Volume> GetVolumes(int seriesId){return _context.Volume@@ -117,10 +103,16 @@ namespace API.Data.ToList();}- public async Task<SeriesDto> GetSeriesDtoByIdAsync(int seriesId)+ public async Task<SeriesDto> GetSeriesDtoByIdAsync(int seriesId, int userId){- return await _context.Series.Where(x => x.Id == seriesId)- .ProjectTo<SeriesDto>(_mapper.ConfigurationProvider).SingleAsync();+ var series = await _context.Series.Where(x => x.Id == seriesId)+ .ProjectTo<SeriesDto>(_mapper.ConfigurationProvider)+ .SingleAsync();++ var seriesList = new List<SeriesDto>() {series};+ await AddSeriesModifiers(userId, seriesList);++ return seriesList[0];}public async Task<Volume> GetVolumeAsync(int volumeId)@@ -130,13 +122,18 @@ namespace API.Data.SingleOrDefaultAsync(vol => vol.Id == volumeId);}- public async Task<VolumeDto> GetVolumeDtoAsync(int volumeId)+ public async Task<VolumeDto> GetVolumeDtoAsync(int volumeId, int userId){- return await _context.Volume+ var volume = await _context.Volume.Where(vol => vol.Id == volumeId).Include(vol => vol.Files).ProjectTo<VolumeDto>(_mapper.ConfigurationProvider).SingleAsync(vol => vol.Id == volumeId);++ var volumeList = new List<VolumeDto>() {volume};+ await AddVolumeModifiers(userId, volumeList);++ return volumeList[0];}/// <summary>@@ -163,5 +160,37 @@ namespace API.Data{return await _context.Volume.SingleOrDefaultAsync(x => x.Id == volumeId);}++ private async Task AddSeriesModifiers(int userId, List<SeriesDto> series)+ {+ var userProgress = await _context.AppUserProgresses+ .Where(p => p.AppUserId == userId && series.Select(s => s.Id).Contains(p.SeriesId))+ .ToListAsync();++ var userRatings = await _context.AppUserRating+ .Where(r => r.AppUserId == userId && series.Select(s => s.Id).Contains(r.SeriesId))+ .ToListAsync();++ foreach (var s in series)+ {+ s.PagesRead = userProgress.Where(p => p.SeriesId == s.Id).Sum(p => p.PagesRead);+ var rating = userRatings.SingleOrDefault(r => r.SeriesId == s.Id);+ if (rating == null) continue;+ s.UserRating = rating.Rating;+ s.UserReview = rating.Review;+ }+ }+ private async Task AddVolumeModifiers(int userId, List<VolumeDto> volumes)+ {+ var userProgress = await _context.AppUserProgresses+ .Where(p => p.AppUserId == userId && volumes.Select(s => s.Id).Contains(p.VolumeId))+ .AsNoTracking()+ .ToListAsync();++ foreach (var v in volumes)+ {+ v.PagesRead = userProgress.Where(p => p.VolumeId == v.Id).Sum(p => p.PagesRead);+ }+ }}}\ No newline at end of filediff --git a/API/Data/UserRepository.cs b/API/Data/UserRepository.csindex 2569e1604..a06be47ee 100644--- a/API/Data/UserRepository.cs+++ b/API/Data/UserRepository.cs@@ -48,6 +48,17 @@ namespace API.Datareturn await _userManager.GetUsersInRoleAsync(PolicyConstants.AdminRole);}+ public async Task<AppUserRating> GetUserRating(int seriesId, int userId)+ {+ return await _context.AppUserRating.Where(r => r.SeriesId == seriesId && r.AppUserId == userId)+ .SingleOrDefaultAsync();+ }++ public void AddRatingTracking(AppUserRating userRating)+ {+ _context.AppUserRating.Add(userRating);+ }+public async Task<IEnumerable<MemberDto>> GetMembersAsync(){return await _context.Usersdiff --git a/API/Entities/AppUser.cs b/API/Entities/AppUser.csindex 10bcec503..3384df9ee 100644--- a/API/Entities/AppUser.cs+++ b/API/Entities/AppUser.cs@@ -14,6 +14,7 @@ namespace API.Entitiespublic ICollection<Library> Libraries { get; set; }public ICollection<AppUserRole> UserRoles { get; set; }public ICollection<AppUserProgress> Progresses { get; set; }+ public ICollection<AppUserRating> Ratings { get; set; }[ConcurrencyCheck]public uint RowVersion { get; set; }diff --git a/API/Entities/AppUserRating.cs b/API/Entities/AppUserRating.csnew file mode 100644index 000000000..ca176e7ae--- /dev/null+++ b/API/Entities/AppUserRating.cs@@ -0,0 +1,22 @@++namespace API.Entities+{+ public class AppUserRating+ {+ public int Id { get; set; }+ /// <summary>+ /// A number between 0-5 that represents how good a series is.+ /// </summary>+ public int Rating { get; set; }+ /// <summary>+ /// A short summary the user can write when giving their review.+ /// </summary>+ public string Review { get; set; }+ public int SeriesId { get; set; }+++ // Relationships+ public int AppUserId { get; set; }+ public AppUser AppUser { get; set; }+ }+}\ No newline at end of filediff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.csindex a33fc18aa..d3be26a60 100644--- a/API/Interfaces/ISeriesRepository.cs+++ b/API/Interfaces/ISeriesRepository.cs@@ -15,10 +15,10 @@ namespace API.InterfacesTask<IEnumerable<Series>> GetSeriesForLibraryIdAsync(int libraryId);Task<IEnumerable<VolumeDto>> GetVolumesDtoAsync(int seriesId, int userId);IEnumerable<Volume> GetVolumes(int seriesId);- Task<SeriesDto> GetSeriesDtoByIdAsync(int seriesId);+ Task<SeriesDto> GetSeriesDtoByIdAsync(int seriesId, int userId);Task<Volume> GetVolumeAsync(int volumeId);- Task<VolumeDto> GetVolumeDtoAsync(int volumeId);+ Task<VolumeDto> GetVolumeDtoAsync(int volumeId, int userId);Task<IEnumerable<Volume>> GetVolumesForSeriesAsync(int[] seriesIds);Task<bool> DeleteSeriesAsync(int seriesId);diff --git a/API/Interfaces/IUserRepository.cs b/API/Interfaces/IUserRepository.csindex 01127050a..20f515350 100644--- a/API/Interfaces/IUserRepository.cs+++ b/API/Interfaces/IUserRepository.cs@@ -12,5 +12,7 @@ namespace API.InterfacesTask<AppUser> GetUserByUsernameAsync(string username);Task<IEnumerable<MemberDto>> GetMembersAsync();Task<IEnumerable<AppUser>> GetAdminUsersAsync();+ Task<AppUserRating> GetUserRating(int seriesId, int userId);+ void AddRatingTracking(AppUserRating userRating);}}\ No newline at end of file