Added components, authentication and authorization
This commit is contained in:
@@ -1,32 +1,38 @@
|
||||
using Immediate.Apis.Shared;
|
||||
using Immediate.Handlers.Shared;
|
||||
|
||||
namespace Htmx.ApiDemo.Templates;
|
||||
|
||||
public sealed class Greeting : GreetingBase
|
||||
{
|
||||
private byte[] _userData = [];
|
||||
private byte[] _countData = [];
|
||||
private byte[] _greetingIdData = [];
|
||||
public required string Username { init => _userData = value.ToUtf8Bytes(); }
|
||||
public required int Count { init => _countData = $"{value}".ToUtf8Bytes(); }
|
||||
public required Guid GreetingId { init => _greetingIdData = $"{value}".ToUtf8Bytes(); }
|
||||
|
||||
protected override void RenderCount(HtmxRenderContext context) => context.Writer.WriteUtf8(_countData);
|
||||
protected override void RenderGreetingId(HtmxRenderContext context) => context.Writer.WriteUtf8(_greetingIdData);
|
||||
protected override void RenderUser(HtmxRenderContext context) => context.Writer.WriteUtf8(_userData);
|
||||
}
|
||||
|
||||
[Handler]
|
||||
[MapGet("/greet/{username}")]
|
||||
[MapGet("/greet/{username}/{count?}/{id?}")]
|
||||
public static partial class GetGreetingHandler
|
||||
{
|
||||
public record Query(string Username);
|
||||
public record Query(string Username, int? Count, Guid? Id);
|
||||
|
||||
private static ValueTask HandleAsync(
|
||||
Query query,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
CancellationToken token)
|
||||
{
|
||||
var context = httpContextAccessor.HttpContext;
|
||||
if(context is null)
|
||||
throw new InvalidOperationException("HttpContext is not available.");
|
||||
|
||||
var template = new Greeting { Username = query.Username };
|
||||
|
||||
context.Response.ContentType = "text/html; charset=utf-8";
|
||||
context.Response.BodyWriter.WriteHtmx(template);
|
||||
var context = httpContextAccessor.HttpContext
|
||||
?? throw new InvalidOperationException("HttpContext is not available.");
|
||||
|
||||
var template = new Greeting { Username = query.Username, Count = query.Count + 1 ?? 0, GreetingId = query.Id ?? Guid.NewGuid() };
|
||||
context.WriteHtmxBody(template);
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user