53 lines
1.9 KiB
C#
53 lines
1.9 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Htmx.ApiDemo.Templates;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Net.Http.Headers;
|
|
|
|
namespace Htmx.ApiDemo;
|
|
|
|
public static partial class RouteMap
|
|
{
|
|
public static void MapGetIndex(WebApplication app)
|
|
=> app.MapGet("/", (IHttpContextAccessor contextAccessor) =>
|
|
{
|
|
var context = contextAccessor.HttpContext
|
|
?? throw new InvalidOperationException("HttpContext is not available.");
|
|
|
|
var isAuthenticate = context.User.Identity?.IsAuthenticated ?? false;
|
|
var JohnDoe = "John Doe";
|
|
var claimedName = context.User.Claims.FirstOrDefault(c => c.Type == "DisplayName")?.Value ?? JohnDoe;
|
|
string name =
|
|
isAuthenticate && claimedName is not null ? claimedName : JohnDoe;
|
|
|
|
var greet = new Greeting { Username = name, Count = 0, GreetingId = Guid.NewGuid() };
|
|
greet.HtmxAwareWriteToBody(
|
|
context: context,
|
|
title: "Home",
|
|
appName: "HtmxApp",
|
|
pageTitle: "Home"
|
|
);
|
|
});
|
|
|
|
private static void MapGetGreet(WebApplication app)
|
|
=> app.MapGet("/greet/{name}/{count}/{greetid}",
|
|
(
|
|
[FromRoute] string name,
|
|
[FromRoute] int count,
|
|
[FromRoute] string greetid,
|
|
[FromServices] IHttpContextAccessor contextAccessor
|
|
) =>
|
|
{
|
|
var context = contextAccessor.HttpContext
|
|
?? throw new InvalidOperationException("HttpContext is not available.");
|
|
|
|
var id = Guid.TryParse(greetid, out var parsedId) ? parsedId : Guid.NewGuid();
|
|
var greet = new Greeting { Username = name, Count = ++count, GreetingId = id };
|
|
greet.HtmxAwareWriteToBody(
|
|
context: context,
|
|
title: "Greet",
|
|
appName: "HtmxApp",
|
|
pageTitle: "Greet"
|
|
);
|
|
});
|
|
} |