|
@@ -1,116 +1,85 @@
|
|
-using System.Collections.Generic;
|
|
|
|
-using System.Threading.Tasks;
|
|
|
|
-using System.Linq;
|
|
|
|
-using System.Web;
|
|
|
|
-using System.IO;
|
|
|
|
-
|
|
|
|
-using Microsoft.EntityFrameworkCore;
|
|
|
|
-
|
|
|
|
|
|
+using System.Web;
|
|
|
|
+using Benchmarks.Model;
|
|
|
|
+using Cottle;
|
|
using GenHTTP.Api.Content;
|
|
using GenHTTP.Api.Content;
|
|
-using GenHTTP.Api.Content.Templating;
|
|
|
|
using GenHTTP.Api.Protocol;
|
|
using GenHTTP.Api.Protocol;
|
|
|
|
|
|
using GenHTTP.Modules.IO;
|
|
using GenHTTP.Modules.IO;
|
|
-using GenHTTP.Modules.Razor;
|
|
|
|
-
|
|
|
|
-using Benchmarks.Model;
|
|
|
|
|
|
+using GenHTTP.Modules.Pages;
|
|
|
|
+using GenHTTP.Modules.Pages.Rendering;
|
|
|
|
|
|
-namespace Benchmarks.Tests
|
|
|
|
-{
|
|
|
|
|
|
+using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
- #region Factory
|
|
|
|
|
|
+namespace Benchmarks.Tests;
|
|
|
|
|
|
- public class FortuneHandlerBuilder : IHandlerBuilder
|
|
|
|
- {
|
|
|
|
|
|
+public class FortuneHandler : IHandler
|
|
|
|
+{
|
|
|
|
|
|
- public IHandler Build(IHandler parent)
|
|
|
|
- {
|
|
|
|
- return new FortuneHandler(parent);
|
|
|
|
- }
|
|
|
|
|
|
+ #region Get-/Setters
|
|
|
|
|
|
- }
|
|
|
|
|
|
+ private TemplateRenderer Template { get; }
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
- #region Supporting data structures
|
|
|
|
|
|
+ #region Initialization
|
|
|
|
|
|
- public sealed class FortuneModel : BasicModel
|
|
|
|
|
|
+ public FortuneHandler()
|
|
{
|
|
{
|
|
|
|
+ var resource = Resource.FromAssembly("Template.html").Build();
|
|
|
|
|
|
- public List<Fortune> Cookies { get; }
|
|
|
|
-
|
|
|
|
- public FortuneModel(IRequest request, IHandler handler, List<Fortune> cookies) : base(request, handler)
|
|
|
|
- {
|
|
|
|
- Cookies = cookies;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ Template = Renderer.From(resource);
|
|
}
|
|
}
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
- public class FortuneHandler : IHandler, IPageRenderer
|
|
|
|
- {
|
|
|
|
-
|
|
|
|
- #region Get-/Setters
|
|
|
|
-
|
|
|
|
- public IHandler Parent { get; }
|
|
|
|
|
|
+ #region Functionality
|
|
|
|
|
|
- private IHandler Page { get; }
|
|
|
|
|
|
+ public ValueTask PrepareAsync() => new();
|
|
|
|
|
|
- private IRenderer<TemplateModel> Template { get; }
|
|
|
|
-
|
|
|
|
- #endregion
|
|
|
|
-
|
|
|
|
- #region Initialization
|
|
|
|
-
|
|
|
|
- public FortuneHandler(IHandler parent)
|
|
|
|
|
|
+ public async ValueTask<IResponse> HandleAsync(IRequest request)
|
|
|
|
+ {
|
|
|
|
+ var data = new Dictionary<Value, Value>
|
|
{
|
|
{
|
|
- Parent = parent;
|
|
|
|
|
|
+ ["cookies"] = Value.FromEnumerable(await GetFortunes())
|
|
|
|
+ };
|
|
|
|
|
|
- Page = ModRazor.Page(Resource.FromAssembly("Fortunes.html"), (r, h) => GetFortunes(r, h))
|
|
|
|
- .Title("Fortunes")
|
|
|
|
- .AddAssemblyReference<HttpUtility>()
|
|
|
|
- .AddUsing("System.Web")
|
|
|
|
- .Build(this);
|
|
|
|
|
|
+ return request.GetPage(await Template.RenderAsync(data)).Build();
|
|
|
|
+ }
|
|
|
|
|
|
- Template = ModRazor.Template<TemplateModel>(Resource.FromAssembly("Template.html")).Build();
|
|
|
|
- }
|
|
|
|
|
|
+ private static async ValueTask<List<Value>> GetFortunes()
|
|
|
|
+ {
|
|
|
|
+ await using var context = DatabaseContext.CreateNoTracking();
|
|
|
|
|
|
- #endregion
|
|
|
|
|
|
+ var fortunes = await context.Fortune.ToListAsync().ConfigureAwait(false);
|
|
|
|
|
|
- #region Functionality
|
|
|
|
|
|
+ var result = new List<Value>(fortunes.Count + 1);
|
|
|
|
|
|
- public async ValueTask PrepareAsync()
|
|
|
|
|
|
+ foreach (var fortune in fortunes)
|
|
{
|
|
{
|
|
- await Page.PrepareAsync();
|
|
|
|
- await Template.PrepareAsync();
|
|
|
|
|
|
+ result.Add(Value.FromDictionary(new Dictionary<Value, Value>()
|
|
|
|
+ {
|
|
|
|
+ ["id"] = fortune.Id,
|
|
|
|
+ ["message"] = HttpUtility.HtmlEncode(fortune.Message)
|
|
|
|
+ }));
|
|
}
|
|
}
|
|
|
|
|
|
- public ValueTask<ulong> CalculateChecksumAsync() => new(17);
|
|
|
|
-
|
|
|
|
- public IAsyncEnumerable<ContentElement> GetContentAsync(IRequest request) => AsyncEnumerable.Empty<ContentElement>();
|
|
|
|
-
|
|
|
|
- public ValueTask<string> RenderAsync(TemplateModel model) => Template.RenderAsync(model);
|
|
|
|
-
|
|
|
|
- public ValueTask RenderAsync(TemplateModel model, Stream target) => Template.RenderAsync(model, target);
|
|
|
|
-
|
|
|
|
- public ValueTask<IResponse> HandleAsync(IRequest request) => Page.HandleAsync(request);
|
|
|
|
-
|
|
|
|
- private static async ValueTask<FortuneModel> GetFortunes(IRequest request, IHandler handler)
|
|
|
|
|
|
+ result.Add(Value.FromDictionary(new Dictionary<Value, Value>()
|
|
{
|
|
{
|
|
- using var context = DatabaseContext.CreateNoTracking();
|
|
|
|
-
|
|
|
|
- var fortunes = await context.Fortune.ToListAsync().ConfigureAwait(false);
|
|
|
|
-
|
|
|
|
- fortunes.Add(new Fortune() { Message = "Additional fortune added at request time." });
|
|
|
|
-
|
|
|
|
- fortunes.Sort();
|
|
|
|
|
|
+ ["id"] = 0,
|
|
|
|
+ ["message"] = "Additional fortune added at request time."
|
|
|
|
+ }));
|
|
|
|
|
|
- return new FortuneModel(request, handler, fortunes);
|
|
|
|
- }
|
|
|
|
|
|
+ result.Sort((one, two) =>
|
|
|
|
+ {
|
|
|
|
+ var firstMessage = one.Fields["message"].AsString;
|
|
|
|
+ var secondMessage = two.Fields["message"].AsString;
|
|
|
|
|
|
- #endregion
|
|
|
|
|
|
+ return string.Compare(firstMessage, secondMessage, StringComparison.Ordinal);
|
|
|
|
+ });
|
|
|
|
|
|
|
|
+ return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
}
|
|
}
|