Use AJAX for queries, add anchors

This commit is contained in:
Laura Hausmann 2022-11-17 03:32:03 +01:00
parent fbed02c12a
commit 96d211634d
Signed by: zotan
GPG key ID: D044E84C5BE01605
4 changed files with 97 additions and 73 deletions

View file

@ -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}");
}
}

View file

@ -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}");
}
}

View file

@ -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);
}
<div class="text-center">
@ -26,7 +29,7 @@
</thead>
<tbody>
@foreach (var movie in new Database.DbConn().Movies.Where(p => p.UserId == Model.AuthorizedUser.UserId).OrderBy(p => p.Title.ToLower())) {
<tr>
<tr id="movie_@movie.MovieId">
<td>
<b>@movie.Title</b>
<br/>
@ -74,7 +77,7 @@
</td>
<td>
<div class="btn-group" role="group">
<a class="btn btn-sm btn-secondary" href="/EditMovie/@movie.MovieId?action=autoinc">W+1</a>
<button class="btn btn-sm btn-secondary" onclick="ajax_and_reload('/EditMovie/@movie.MovieId', 'autoinc')">W+1</button>
<a class="btn btn-sm btn-primary" href="/EditMovie/@movie.MovieId">Edit</a>
</div>
</td>
@ -83,3 +86,15 @@
</tbody>
</table>
</div>
<script>
function ajax_and_reload(url, action, target, value) {
if (window.location.hash.length > 0){
window.location.replace("#");
if (typeof window.history.replaceState == 'function') {
history.replaceState({}, '', window.location.href.slice(0, -1));
}
}
$.ajax(url, {method: 'POST', data: {action: action, target: target, value: value, '__RequestVerificationToken' : '@tokenSet.RequestToken'}, success: function () { location.reload() }})
}
</script>

View file

@ -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);
}
<div class="text-center">
@ -29,7 +32,7 @@
</thead>
<tbody>
@foreach (var show in new Database.DbConn().Shows.Where(p => p.UserId == Model.AuthorizedUser.UserId).OrderBy(p => p.Title.ToLower())) {
<tr>
<tr id="show_@show.ShowId">
<td>
<b>@show.Title</b>
<br/>
@ -114,19 +117,19 @@
<td>
<div class="btn-group" role="group">
@if (show.SeenEpisodes == show.TotalEpisodes && show.WatchStatus == WatchStatus.FirstWatch) {
<a class="btn btn-sm btn-secondary" href="/EditShow/@show.ShowId?action=waiting">Waiting</a>
<button class="btn btn-sm btn-secondary" onclick="ajax_and_reload('/EditShow/@show.ShowId', 'waiting')">Waiting</button>
}
@if (show.SeenEpisodes == show.TotalEpisodes && (show.WatchStatus == WatchStatus.FirstWatch || show.WatchStatus == WatchStatus.Rewatch)) {
<a class="btn btn-sm btn-success" href="/EditShow/@show.ShowId?action=finish">Finish</a>
<button class="btn btn-sm btn-success" onclick="ajax_and_reload('/EditShow/@show.ShowId', 'finish')">Finish</button>
}
@if (show.SeenEpisodes < show.TotalEpisodes && (show.WatchStatus == WatchStatus.Unwatched || show.WatchStatus == WatchStatus.FirstWatch || show.WatchStatus == WatchStatus.Rewatch)) {
<a class="btn btn-sm btn-secondary" href="/EditShow/@show.ShowId?action=autoinc">W+1</a>
<button class="btn btn-sm btn-secondary" onclick="ajax_and_reload('/EditShow/@show.ShowId', 'autoinc')">W+1</button>
}
else if (show.WatchStatus == WatchStatus.Finished) {
<a class="btn btn-sm btn-success" href="/EditShow/@show.ShowId?action=rewatch">Start rewatch</a>
<button class="btn btn-sm btn-success" onclick="ajax_and_reload('/EditShow/@show.ShowId', 'rewatch')">Start rewatch</button>
}
else if (show.WatchStatus == WatchStatus.Waiting) {
<a class="btn btn-sm btn-secondary" href="/EditShow/@show.ShowId?action=autoinc">New episode</a>
<button class="btn btn-sm btn-secondary" onclick="ajax_and_reload('/EditShow/@show.ShowId', 'autoinc')">New episode</button>
}
<a class="btn btn-sm btn-primary" href="/EditShow/@show.ShowId">Edit</a>
</div>
@ -136,3 +139,15 @@
</tbody>
</table>
</div>
<script>
function ajax_and_reload(url, action, target, value) {
if (window.location.hash.length > 0){
window.location.replace("#");
if (typeof window.history.replaceState == 'function') {
history.replaceState({}, '', window.location.href.slice(0, -1));
}
}
$.ajax(url, {method: 'POST', data: {action: action, target: target, value: value, '__RequestVerificationToken' : '@tokenSet.RequestToken'}, success: function () { location.reload() }})
}
</script>