From 70dad9ccdbc2f65102e61adb29de024f6d88ec38 Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Sat, 18 Nov 2023 14:35:05 +0100 Subject: [PATCH] changed: migrated index page to razor component --- Components/Index.razor | 37 +++++++++++++++++++++++++++++++++++++ Pages/Index.cshtml | 15 --------------- Pages/Index.cshtml.cs | 33 --------------------------------- 3 files changed, 37 insertions(+), 48 deletions(-) create mode 100644 Components/Index.razor delete mode 100644 Pages/Index.cshtml delete mode 100644 Pages/Index.cshtml.cs diff --git a/Components/Index.razor b/Components/Index.razor new file mode 100644 index 0000000..56e6c47 --- /dev/null +++ b/Components/Index.razor @@ -0,0 +1,37 @@ +@page "/" +@page "/{Id}" +@using Microsoft.Extensions.Caching.Distributed + +@inject IDistributedCache Db +@inject NavigationManager Navigation + +Startpage - Just Short It + +
+ @if (Id is null) { +

Welcome to Just Short It!

+

The KISS single-user URL shortener!

+ Start shorting URLs + } else { +

@ErrorMessage

+ } +
+ +@code { + [Parameter] + public string? Id { get; set; } + + private string? ErrorMessage { get; set; } + + protected override async Task OnInitializedAsync() { + if (Id is not null) { + string? data = await Db.GetStringAsync(Id); + if (data is not null) { + Navigation.NavigateTo(data, true); + } else { + ErrorMessage = "Redirect ID not found, it may have been deleted or expired"; + } + } + } + +} diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml deleted file mode 100644 index 5ce9393..0000000 --- a/Pages/Index.cshtml +++ /dev/null @@ -1,15 +0,0 @@ -@page "{Id?}" -@model IndexModel -@{ - ViewData["Title"] = "Startpage"; -} - -
- @if (Model.Id is null) { -

Welcome to Just Short It!

-

The KISS single-user URL shortener!

- Start shorting URLs - } else { -

@Model.ErrorMessage

- } -
diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs deleted file mode 100644 index adca7c5..0000000 --- a/Pages/Index.cshtml.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Text; -using Microsoft.AspNetCore.Mvc; -using Microsoft.AspNetCore.Mvc.RazorPages; -using Microsoft.Extensions.Caching.Distributed; - -namespace JustShortIt.Pages; - -public class IndexModel : PageModel { - private ILogger Logger { get; } - private IDistributedCache Db { get; set; } - - // Bound property - public string? Id { get; set; } - public string? ErrorMessage { get; set; } - - public IndexModel(ILogger logger, IDistributedCache distributedCache) { - Logger = logger; - Db = distributedCache; - } - - public async Task OnGetAsync(string? id) { - Id = id; - - if (Id is not null) { - string? data = await Db.GetStringAsync(Id); - if (data is not null) return Redirect(data); - - ErrorMessage = "Redirect ID not found, it may have been deleted or expired"; - } - - return Page(); - } -} \ No newline at end of file