From 96d211634d2ec3bf0ee62dffdc835bd0d093d3d3 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 17 Nov 2022 03:32:03 +0100 Subject: [PATCH] Use AJAX for queries, add anchors --- MediaManager/Pages/EditMovie.cshtml.cs | 30 ++++----- MediaManager/Pages/EditShow.cshtml.cs | 90 +++++++++++++------------- MediaManager/Pages/Movies.cshtml | 21 +++++- MediaManager/Pages/Shows.cshtml | 29 +++++++-- 4 files changed, 97 insertions(+), 73 deletions(-) diff --git a/MediaManager/Pages/EditMovie.cshtml.cs b/MediaManager/Pages/EditMovie.cshtml.cs index 13696fe..9eaf7ae 100644 --- a/MediaManager/Pages/EditMovie.cshtml.cs +++ b/MediaManager/Pages/EditMovie.cshtml.cs @@ -14,24 +14,9 @@ public class EditMovie : PageModel { var movie = db.Movies.First(p => p.MovieId == movieId); AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db))!; - - if (movie.UserId != AuthorizedUser.UserId) { + + if (movie.UserId != AuthorizedUser.UserId) Response.Redirect("/"); - return; - } - - if (Request.Query.ContainsKey("action") && Request.Query["action"] == "autoinc") { - if (movie.UserId != AuthorizedUser.UserId) { - Response.Redirect("/"); - return; - } - - movie.WatchCount++; - movie.LastSeen = DateTime.Now; - - db.Update(movie); - Response.Redirect("/Movies"); - } } public void OnPost() { @@ -52,6 +37,15 @@ public class EditMovie : PageModel { return; } + if (Request.Form["action"] == "autoinc") { + movie.WatchCount++; + movie.LastSeen = DateTime.Now; + + db.Update(movie); + Response.Redirect($"/Movies#movie_{movie.MovieId}"); + return; + } + movie.Title = Request.Form["title"]; movie.Year = int.Parse(Request.Form["year"]); movie.WatchCount = int.Parse(Request.Form["watchcount"]); @@ -62,6 +56,6 @@ public class EditMovie : PageModel { db.Update(movie); - Response.Redirect("/Movies"); + Response.Redirect($"/Movies#movie_{movie.MovieId}"); } } diff --git a/MediaManager/Pages/EditShow.cshtml.cs b/MediaManager/Pages/EditShow.cshtml.cs index adbc1b4..802c352 100644 --- a/MediaManager/Pages/EditShow.cshtml.cs +++ b/MediaManager/Pages/EditShow.cshtml.cs @@ -7,20 +7,37 @@ namespace MediaManager.Pages; public class EditShow : PageModel { public User AuthorizedUser; - + public void OnGet() { using var db = new Database.DbConn(); var showId = int.Parse(RouteData.Values["id"]!.ToString()!); var show = db.Shows.First(p => p.ShowId == showId); - + + AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db))!; + + if (show.UserId != AuthorizedUser.UserId) + Response.Redirect("/"); + } + + public void OnPost() { + using var db = new Database.DbConn(); + var showId = int.Parse(RouteData.Values["id"]!.ToString()!); + var show = db.Shows.First(p => p.ShowId == showId); + AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db))!; if (show.UserId != AuthorizedUser.UserId) { Response.Redirect("/"); return; } - - if (Request.Query.ContainsKey("action") && Request.Query["action"] == "autoinc") { + + if (Request.Form["action"] == "delete") { + db.Delete(show); + Response.Redirect("/Shows"); + return; + } + + if (Request.Form["action"] == "autoinc") { if (show.WatchStatus == WatchStatus.Finished) { Response.Redirect("/Shows"); return; @@ -39,46 +56,31 @@ public class EditShow : PageModel { }; db.Update(show); - Response.Redirect("/Shows"); - } - - if (Request.Query.ContainsKey("action") && Request.Query["action"] == "rewatch") { - show.SeenEpisodes = 0; - show.WatchStatus = WatchStatus.Rewatch; - - db.Update(show); - Response.Redirect("/Shows"); - } - - if (Request.Query.ContainsKey("action") && Request.Query["action"] == "finish") { - show.WatchStatus = WatchStatus.Finished; - show.WatchCount++; - db.Update(show); - Response.Redirect("/Shows"); - } - - if (Request.Query.ContainsKey("action") && Request.Query["action"] == "waiting") { - show.WatchStatus = WatchStatus.Waiting; - db.Update(show); - Response.Redirect("/Shows"); - } - } - - public void OnPost() { - using var db = new Database.DbConn(); - var showId = int.Parse(RouteData.Values["id"]!.ToString()!); - var show = db.Shows.First(p => p.ShowId == showId); - - AuthorizedUser = db.Users.FirstOrDefault(p => p.Username == AuthUtil.GetRemoteUser(HttpContext, db))!; - - if (show.UserId != AuthorizedUser.UserId) { - Response.Redirect("/"); + Response.Redirect($"/Shows#show_{show.ShowId}"); return; } - if (Request.Form["action"] == "delete") { - db.Delete(show); - Response.Redirect("/Shows"); + if (Request.Form["action"] == "rewatch") { + show.SeenEpisodes = 0; + show.WatchStatus = WatchStatus.Rewatch; + + db.Update(show); + Response.Redirect($"/Shows#show_{show.ShowId}"); + return; + } + + if (Request.Form["action"] == "finish") { + show.WatchStatus = WatchStatus.Finished; + show.WatchCount++; + db.Update(show); + Response.Redirect($"/Shows#show_{show.ShowId}"); + return; + } + + if (Request.Form["action"] == "waiting") { + show.WatchStatus = WatchStatus.Waiting; + db.Update(show); + Response.Redirect($"/Shows#show_{show.ShowId}"); return; } @@ -95,13 +97,11 @@ public class EditShow : PageModel { if (show.SeenEpisodes >= show.TotalEpisodes) { show.SeenEpisodes = show.TotalEpisodes; - if (show.WatchStatus != WatchStatus.Waiting) { + if (show.WatchStatus != WatchStatus.Waiting) show.WatchStatus = WatchStatus.Finished; - } } db.Update(show); - - Response.Redirect("/Shows"); + Response.Redirect($"/Shows#show_{show.ShowId}"); } } diff --git a/MediaManager/Pages/Movies.cshtml b/MediaManager/Pages/Movies.cshtml index e77b115..ed3122a 100644 --- a/MediaManager/Pages/Movies.cshtml +++ b/MediaManager/Pages/Movies.cshtml @@ -1,9 +1,12 @@ @page +@inject IAntiforgery _antiforgery +@using Microsoft.AspNetCore.Antiforgery @using MediaManager.database -@model MediaManager.Pages.Movies +@model Movies @{ ViewData["Title"] = "Movies"; Layout = "_LayoutNoContainer"; + var tokenSet = _antiforgery.GetAndStoreTokens(HttpContext); }
@@ -26,7 +29,7 @@ @foreach (var movie in new Database.DbConn().Movies.Where(p => p.UserId == Model.AuthorizedUser.UserId).OrderBy(p => p.Title.ToLower())) { - + @movie.Title
@@ -74,7 +77,7 @@
- W+1 + Edit
@@ -83,3 +86,15 @@
+ + diff --git a/MediaManager/Pages/Shows.cshtml b/MediaManager/Pages/Shows.cshtml index 6e8af7d..76599ca 100644 --- a/MediaManager/Pages/Shows.cshtml +++ b/MediaManager/Pages/Shows.cshtml @@ -1,10 +1,13 @@ @page +@inject IAntiforgery _antiforgery +@using Microsoft.AspNetCore.Antiforgery @using MediaManager.database @using MediaManager.database.Tables -@model MediaManager.Pages.Shows +@model Shows @{ ViewData["Title"] = "Shows"; Layout = "_LayoutNoContainer"; + var tokenSet = _antiforgery.GetAndStoreTokens(HttpContext); }
@@ -29,7 +32,7 @@ @foreach (var show in new Database.DbConn().Shows.Where(p => p.UserId == Model.AuthorizedUser.UserId).OrderBy(p => p.Title.ToLower())) { - + @show.Title
@@ -114,19 +117,19 @@
@if (show.SeenEpisodes == show.TotalEpisodes && show.WatchStatus == WatchStatus.FirstWatch) { - Waiting + } @if (show.SeenEpisodes == show.TotalEpisodes && (show.WatchStatus == WatchStatus.FirstWatch || show.WatchStatus == WatchStatus.Rewatch)) { - Finish + } @if (show.SeenEpisodes < show.TotalEpisodes && (show.WatchStatus == WatchStatus.Unwatched || show.WatchStatus == WatchStatus.FirstWatch || show.WatchStatus == WatchStatus.Rewatch)) { - W+1 + } else if (show.WatchStatus == WatchStatus.Finished) { - Start rewatch + } else if (show.WatchStatus == WatchStatus.Waiting) { - New episode + } Edit
@@ -136,3 +139,15 @@
+ +