From 89cce362f68e33d49e44dcffb7502b70e9e9ddf5 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Wed, 19 Apr 2023 16:10:49 +0200 Subject: [PATCH] Added Inspect Page --- CHANGELOG.md | 1 + Pages/Inspect.cshtml | 38 ++++++++++++++++++++++++++++++++++ Pages/Inspect.cshtml.cs | 46 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) create mode 100644 Pages/Inspect.cshtml create mode 100644 Pages/Inspect.cshtml.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index ce438bc..d5a202f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Added - Page Titles to Login, Logout and Urls Page +- Added Inspect Page, which displays information about a URL and allows to delete it ## [1.1.0] - 2023-04-17 diff --git a/Pages/Inspect.cshtml b/Pages/Inspect.cshtml new file mode 100644 index 0000000..b886b6e --- /dev/null +++ b/Pages/Inspect.cshtml @@ -0,0 +1,38 @@ +@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.

+ Back to URLs +
+ } +} else { +
+
+
ID
+
@Model.UrlRedirect.Id
+
URL-Target
+
@Model.UrlRedirect.Target
+
+ +
+ + +
+
+ +} +
\ No newline at end of file diff --git a/Pages/Inspect.cshtml.cs b/Pages/Inspect.cshtml.cs new file mode 100644 index 0000000..1c6950c --- /dev/null +++ b/Pages/Inspect.cshtml.cs @@ -0,0 +1,46 @@ +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