From e570e91fb66a8d35a2c021d179192811a731bed9 Mon Sep 17 00:00:00 2001 From: Masterjun Date: Fri, 23 Aug 2024 02:09:49 +0200 Subject: [PATCH] Make Catalogs keep selections when creating Games or Versions or Goals (#1964) * make catalogs keep selections when creating games or versions or goals * simplify the js by sending everything always This also means when you e.g. create a new game, the url will contain the version and goal ids of the old game. But we properly validate this in the OnGet anyway, as we should. --- TASVideos/Pages/Games/Goals/List.cshtml.cs | 10 +++++-- TASVideos/Pages/Games/Versions/Edit.cshtml.cs | 2 +- .../Pages/Publications/Catalog.cshtml.cs | 29 ++++++++++++++++-- TASVideos/Pages/Submissions/Catalog.cshtml | 6 +++- TASVideos/Pages/Submissions/Catalog.cshtml.cs | 29 ++++++++++++++++-- TASVideos/wwwroot/js/catalog.js | 30 ++++++++++++++++--- 6 files changed, 93 insertions(+), 13 deletions(-) diff --git a/TASVideos/Pages/Games/Goals/List.cshtml.cs b/TASVideos/Pages/Games/Goals/List.cshtml.cs index 4eb6c7c68..e544f6cd7 100644 --- a/TASVideos/Pages/Games/Goals/List.cshtml.cs +++ b/TASVideos/Pages/Games/Goals/List.cshtml.cs @@ -54,14 +54,18 @@ public async Task OnPost(string? goalToCreate) return BackToList(); } - db.GameGoals.Add(new GameGoal + var gameGoal = new GameGoal { GameId = GameId, DisplayName = goalToCreate - }); + }; + + db.GameGoals.Add(gameGoal); SetMessage(await db.TrySaveChanges(), $"Goal {goalToCreate} created successfully", $"Unable to create goal {goalToCreate}"); - return BackToList(); + return string.IsNullOrWhiteSpace(Request.ReturnUrl()) + ? RedirectToPage("List", new { GameId }) + : BaseReturnUrlRedirect(new() { ["GameGoalId"] = gameGoal.Id.ToString() }); } public async Task OnPostEdit(int gameGoalId, string? newGoalName) diff --git a/TASVideos/Pages/Games/Versions/Edit.cshtml.cs b/TASVideos/Pages/Games/Versions/Edit.cshtml.cs index bd556abc6..b1670add8 100644 --- a/TASVideos/Pages/Games/Versions/Edit.cshtml.cs +++ b/TASVideos/Pages/Games/Versions/Edit.cshtml.cs @@ -154,7 +154,7 @@ public async Task OnPost() SuccessStatusMessage($"Game Version {Id} updated"); return string.IsNullOrWhiteSpace(HttpContext.Request.ReturnUrl()) ? RedirectToPage("List", new { gameId = GameId }) - : BaseReturnUrlRedirect(new() { ["GameId"] = GameId.ToString(), ["GameVersionId"] = version.Id.ToString() }); + : BaseReturnUrlRedirect(new() { ["SystemId"] = system.Id.ToString(), ["GameId"] = GameId.ToString(), ["GameVersionId"] = version.Id.ToString() }); } public async Task OnPostDelete() diff --git a/TASVideos/Pages/Publications/Catalog.cshtml.cs b/TASVideos/Pages/Publications/Catalog.cshtml.cs index 55a79f0c1..6c6bbce39 100644 --- a/TASVideos/Pages/Publications/Catalog.cshtml.cs +++ b/TASVideos/Pages/Publications/Catalog.cshtml.cs @@ -8,12 +8,18 @@ public class CatalogModel(ApplicationDbContext db, ExternalMediaPublisher publis [FromRoute] public int Id { get; set; } + [FromQuery] + public int? SystemId { get; set; } + [FromQuery] public int? GameId { get; set; } [FromQuery] public int? GameVersionId { get; set; } + [FromQuery] + public int? GameGoalId { get; set; } + [BindProperty] public PublicationCatalog Catalog { get; set; } = new(); @@ -45,6 +51,16 @@ public async Task OnGet() } Catalog = catalog; + + if (SystemId.HasValue) + { + var system = await db.GameSystems.FindAsync(SystemId); + if (system is not null) + { + Catalog.System = system.Id; + } + } + if (GameId.HasValue) { var game = await db.Games.FindAsync(GameId); @@ -61,6 +77,15 @@ public async Task OnGet() Catalog.GameVersion = gameVersion.Id; } } + + if (GameGoalId.HasValue) + { + var gameGoal = await db.GameGoals.SingleOrDefaultAsync(gg => gg.GameId == game.Id && gg.Id == GameGoalId); + if (gameGoal is not null) + { + Catalog.Goal = gameGoal.Id; + } + } } } @@ -202,9 +227,9 @@ public class PublicationCatalog { public string Title { get; init; } = ""; public int GameVersion { get; set; } - public int Goal { get; init; } + public int Goal { get; set; } public int Game { get; set; } - public int System { get; init; } + public int System { get; set; } public int SystemFramerate { get; init; } [StringLength(50)] diff --git a/TASVideos/Pages/Submissions/Catalog.cshtml b/TASVideos/Pages/Submissions/Catalog.cshtml index aad0f9b90..b24f45078 100644 --- a/TASVideos/Pages/Submissions/Catalog.cshtml +++ b/TASVideos/Pages/Submissions/Catalog.cshtml @@ -62,7 +62,11 @@
- +
diff --git a/TASVideos/Pages/Submissions/Catalog.cshtml.cs b/TASVideos/Pages/Submissions/Catalog.cshtml.cs index ddb01ade8..2b4cb6ef2 100644 --- a/TASVideos/Pages/Submissions/Catalog.cshtml.cs +++ b/TASVideos/Pages/Submissions/Catalog.cshtml.cs @@ -8,12 +8,18 @@ public class CatalogModel(ApplicationDbContext db, ExternalMediaPublisher publis [FromRoute] public int Id { get; set; } + [FromQuery] + public int? SystemId { get; set; } + [FromQuery] public int? GameId { get; set; } [FromQuery] public int? GameVersionId { get; set; } + [FromQuery] + public int? GameGoalId { get; set; } + [BindProperty] public SubmissionCatalog Catalog { get; set; } = new(); @@ -45,6 +51,16 @@ public async Task OnGet() } Catalog = catalog; + + if (SystemId.HasValue) + { + var system = await db.GameSystems.FindAsync(SystemId); + if (system is not null) + { + Catalog.System = system.Id; + } + } + if (GameId.HasValue) { var game = await db.Games.FindAsync(GameId); @@ -61,6 +77,15 @@ public async Task OnGet() Catalog.GameVersion = version.Id; } } + + if (GameGoalId.HasValue) + { + var gameGoal = await db.GameGoals.SingleOrDefaultAsync(gg => gg.GameId == game.Id && gg.Id == GameGoalId); + if (gameGoal is not null) + { + Catalog.Goal = gameGoal.Id; + } + } } } @@ -238,12 +263,12 @@ public class SubmissionCatalog public int? Game { get; set; } [Required] - public int? System { get; init; } + public int? System { get; set; } [Required] public int? SystemFramerate { get; init; } - public int? Goal { get; init; } + public int? Goal { get; set; } [StringLength(50)] public string? Emulator { get; init; } diff --git a/TASVideos/wwwroot/js/catalog.js b/TASVideos/wwwroot/js/catalog.js index e5ccedae6..2659a7412 100644 --- a/TASVideos/wwwroot/js/catalog.js +++ b/TASVideos/wwwroot/js/catalog.js @@ -10,7 +10,9 @@ function enableCataloging() { const createVersionBtn = document.getElementById('create-version'); const gameGoalModel = document.querySelector('[data-id="goal"]'); const gameGoalBtn = document.getElementById('create-goal'); - const returnUrl = encodeURIComponent(systemModel.dataset.returnUrl); + const returnUrlPathAndQuery = systemModel.dataset.returnUrl.split(/\?(.*)/s) // splits string at the first question mark (?) + const returnUrlPath = returnUrlPathAndQuery[0]; + const returnUrlQuery = returnUrlPathAndQuery[1] ?? ""; systemModel.onchange = function () { if (this.value) { @@ -58,20 +60,40 @@ function enableCataloging() { } else { createVersionBtn.classList.add('disabled'); createVersionBtn.setAttribute('disabled', 'disabled'); + gameGoalBtn.classList.add('disabled'); + gameGoalBtn.setAttribute('disabled', 'disabled'); clearDropdown(versionElemId); clearDropdown(gameGoalElemId); } } document.getElementById('create-version')?.addEventListener('click', function () { - document.location = `/Games/${gameModel.value}/Versions/Edit?returnUrl=${returnUrl}&systemId=${systemModel.value}`; + document.location = `/Games/${gameModel.value}/Versions/Edit?SystemId=${systemModel.value}&returnUrl=${generateCurrentReturnUrl() }`; }); document.getElementById('create-game')?.addEventListener('click', function () { - document.location = `/Games/Edit?returnUrl=${returnUrl}`; + document.location = `/Games/Edit?returnUrl=${generateCurrentReturnUrl() }`; }); gameGoalBtn?.addEventListener('click', function () { - document.location = `/Games/${gameModel.value}/Goals/List?returnUrl=${returnUrl}`; + document.location = `/Games/${gameModel.value}/Goals/List?returnUrl=${generateCurrentReturnUrl()}`; }); + + function generateCurrentReturnUrl() { + const returnUrlQueryModified = new URLSearchParams(); + if (systemModel.value) { + returnUrlQueryModified.set('SystemId', systemModel.value); + } + if (gameModel.value) { + returnUrlQueryModified.set('GameId', gameModel.value); + } + if (versionModel.value) { + returnUrlQueryModified.set('GameVersionId', versionModel.value); + } + if (gameGoalModel.value) { + returnUrlQueryModified.set('GameGoalId', gameGoalModel.value); + } + + return encodeURIComponent(returnUrlPath + '?' + returnUrlQueryModified.toString()); + } } \ No newline at end of file