From b3670b2046c84abf8ddc97e131ab7ebe04f7e5e5 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Sat, 18 Nov 2023 15:09:53 +0100 Subject: [PATCH] changed: migrated inspect page to razor components --- Components/MessageComponent.razor | 34 ++++++++++++++ Components/Pages/Inspect.razor | 77 +++++++++++++++++++++++++++++++ Pages/Inspect.cshtml | 44 ------------------ Pages/Inspect.cshtml.cs | 46 ------------------ Pages/Urls.cshtml.cs | 5 +- 5 files changed, 113 insertions(+), 93 deletions(-) create mode 100644 Components/MessageComponent.razor create mode 100644 Components/Pages/Inspect.razor delete mode 100644 Pages/Inspect.cshtml delete mode 100644 Pages/Inspect.cshtml.cs diff --git a/Components/MessageComponent.razor b/Components/MessageComponent.razor new file mode 100644 index 0000000..63822ec --- /dev/null +++ b/Components/MessageComponent.razor @@ -0,0 +1,34 @@ +@if (Message is not null) { +
+ + + + + @Message + + +
+} + +@code { + [Parameter] + public string? Message { get; set; } + [Parameter] + public AlertType Type { get; set; } = AlertType.Information; + + private string GetAlertTypeClass() => Type switch{ + AlertType.Information => "alert-info", + AlertType.Success => "alert-success", + AlertType.Warning => "alert-warning", + AlertType.Error => "alert-error", + _ => throw new ArgumentOutOfRangeException() + }; + + public enum AlertType { + Information, Success, Warning, Error + } +} diff --git a/Components/Pages/Inspect.razor b/Components/Pages/Inspect.razor new file mode 100644 index 0000000..9cb766d --- /dev/null +++ b/Components/Pages/Inspect.razor @@ -0,0 +1,77 @@ +@page "/inspect" +@using Microsoft.AspNetCore.Authorization +@using JustShortIt.Model +@using Microsoft.Extensions.Caching.Distributed +@attribute [Authorize] + +@inject IDistributedCache Db + +Inspect - Just Short It + +
+
+ @if (Model is null) { +

+ URL not found +

+

+ The given ID does not exist, it may have expired or been deleted. +

+ + } else { +

Inspect

+ +
+
ID
+
@Model.Id
+
URL-Target
+
@Model.Target
+
+ +
+ + + + + } + + + Back to URLs + +
+
+ +@code { + [SupplyParameterFromQuery] + public string? Id { get; set; } + + private string? Message { get; set; } + private MessageComponent.AlertType Type { get; set; } + + private UrlRedirect? Model { get; set; } + + protected override async Task OnInitializedAsync() { + if (Id is not null) { + string? url = await Db.GetStringAsync(Id); + if (url is not null) + Model = new UrlRedirect(Id, url, string.Empty); + } + } + + private async Task Submit_Delete() { + if (Id is null) { + Message = $"Invalid Request: Deletion without Id"; + return; + } + + await Db.RemoveAsync(Id); + + Type = MessageComponent.AlertType.Success; + Message = $"Id '{Id}' successfully deleted."; + return; + } + +} diff --git a/Pages/Inspect.cshtml b/Pages/Inspect.cshtml deleted file mode 100644 index 9730393..0000000 --- a/Pages/Inspect.cshtml +++ /dev/null @@ -1,44 +0,0 @@ -@page -@using JustShortIt.Model -@model JustShortIt.Pages.InspectModel -@{ - ViewData["Title"] = "Inspect"; -} - -
-
- @if (Model.UrlRedirect is null) { - @if (!string.IsNullOrEmpty(Model.Message)) { -
- - - - - @Html.Raw(Model.Message) - - -
- } else { -

URL not found

-

The given ID does not exist, it may have expired or been deleted.

- } - } else { -
-
ID
-
@Model.UrlRedirect.Id
-
URL-Target
-
@Model.UrlRedirect.Target
-
- -
- - -
- } - Back to URLs -
-
diff --git a/Pages/Inspect.cshtml.cs b/Pages/Inspect.cshtml.cs deleted file mode 100644 index 1c6950c..0000000 --- a/Pages/Inspect.cshtml.cs +++ /dev/null @@ -1,46 +0,0 @@ -using JustShortIt.Model; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.Extensions.Caching.Distributed; - -namespace JustShortIt.Pages; - -[Authorize] -public class InspectModel : PageModel { - [BindProperty(Name = "id", SupportsGet = true)] - public string? Id { get; set; } = string.Empty; - [BindProperty(Name="message")] - public string? Message { get; set; } - - public UrlRedirect? UrlRedirect { get; set; } - - private IDistributedCache Db { get; } - - public InspectModel(IDistributedCache db) { - Db = db; - } - - public async Task OnPostAsync() { - if (Id == null) return await OnGet(null, $"Delete request without ID, aborted."); - - await Db.RemoveAsync(Id); - - return await OnGet(null, $"ID '{Id}' successfully deleted."); - } - - public async Task OnGet(string? id, string? message) { - if (id is null && message is null) return RedirectToPage("Urls"); - - Id = id; - Message = message; - - if (Id is not null) { - string? url = await Db.GetStringAsync(Id); - if (url is not null) - UrlRedirect = new UrlRedirect(Id, url, string.Empty); - } - - return Page(); - } -} \ No newline at end of file diff --git a/Pages/Urls.cshtml.cs b/Pages/Urls.cshtml.cs index 5ad7bb5..05b129a 100644 --- a/Pages/Urls.cshtml.cs +++ b/Pages/Urls.cshtml.cs @@ -1,11 +1,10 @@ -using System.Text.Encodings.Web; using JustShortIt.Model; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using System.Text.RegularExpressions; using System.Web; -using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Caching.Distributed; namespace JustShortIt.Pages; @@ -42,7 +41,7 @@ public class UrlsModel : PageModel { return Page(); } - return RedirectToPage("Inspect", new { Id = id }); + return LocalRedirect(QueryHelpers.AddQueryString("~/inspect", "Id", id)); } public async Task OnPostNewAsync() {