#region ========================================================================= USING ===================================================================================== using FastEndpoints; using Lumina.Presentation.Web.Common.Http; using Microsoft.AspNetCore.Http; using System.Collections.Generic; #endregion namespace Lumina.Presentation.Web.Core.Endpoints.Common; /// /// Base class for Web endpoints. /// /// The type of the request object. /// The type of the response object. public abstract class BaseEndpoint : Endpoint where TRequest : notnull where TResponse : notnull { /// /// Gets the current culture taken from the culture route value. /// protected string Culture => HttpContext.Request.RouteValues["culture"]?.ToString() ?? "en-US"; /// /// Configures the endpoint. /// /// /// All exceptions are propagated to the application's exception handling middleware instead of being converted to error responses by the library. /// public override void Configure() { DontCatchExceptions(); } /// /// Renders a Razor view to the response. /// /// The name or path of the view to render. /// The model to pass to the view. /// Optional additional view data entries made available to the view via ViewData. /// An that renders the view. protected IResult View(string viewName, object? model = null, IReadOnlyDictionary? viewData = null) { // store the path of the displayed view in the session, so that the login redirect after the JWT token expires points to it; ignore the login endpoint, // because it is incorrect to be redirected to the login URL, with the login URL as the return URL if (HttpContext.Request.Path != "/en-us/auth/login") HttpContext.Session.SetString(HttpContextItemKeys.LAST_DISPLAYED_VIEW, $"{HttpContext.Request.PathBase}{HttpContext.Request.Path}{HttpContext.Request.QueryString}"); return new RazorViewResult(viewName, model, viewData); } /// /// Creates a JSON response indicating a successful operation, optionally wrapping . /// /// The payload of the successful response. /// An containing the success JSON payload. protected IResult JsonSuccess(object? data) { return Results.Json(new { success = true, data }); } /// /// Creates a JSON response indicating a successful operation without a payload. /// /// An containing the success JSON payload. protected IResult JsonSuccess() { return Results.Json(new { success = true }); } }