Skip to content

Commit

Permalink
♻️
Browse files Browse the repository at this point in the history
  • Loading branch information
hlaueriksson committed May 14, 2024
1 parent 3997168 commit 6335f08
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 220 deletions.
87 changes: 47 additions & 40 deletions samples/Markupolation.Sample.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options => options.AddDefaultPolicy(policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
builder.Services.AddProblemDetails();
Expand All @@ -11,76 +9,85 @@
app.UseHttpsRedirection();
app.MapDefaultEndpoints();

app.MapGet("/", () => Results.Extensions.Html(
app.MapGet("/", () => Results.Text(contentType: "text/html; charset=utf-8", content:
DOCTYPE() +
html(lang("en"),
head(
meta(charset("utf-8")),
e.title("Markupolation.Sample.Api"),
meta(name("description"), content("Sample of how to use Markupolation in a net6.0 Web Api")),
meta(name("description"), content("Sample of how to use Markupolation in a Minimal API")),
meta(name("viewport"), content("width=device-width, initial-scale=1"))
),
body(onload("console.log('Markupolation in a net6.0 Web Api');"),
body(onload("console.log('Markupolation in a Minimal API');"),
h1("Hello, World!"),
p("This is ", mark(a.title("Markup with string interpolation"), "Markupolation"), " in action.")
)
)
));

app.MapGet("/hello", () => Results.Extensions.Html(
app.MapGet("/hello", () => Results.Text(contentType: "text/html; charset=utf-8", content:
h1("Hello, World!") + p("This is ", mark(title("Markup with string interpolation"), "Markupolation"), " in action.")
));

app.MapGet("/counter/{count:int?}", ([FromRoute(Name = "count")] int? routeCount, [FromQuery(Name = "count")] int? queryCount) =>
app.MapGet("/counter/{count}", (HttpRequest request, int count) =>
{
int count = routeCount ?? queryCount ?? 0;
var result = mark(a.title(count), Humanizer.NumberToWordsExtension.ToWords(count));
return Results.Extensions.Html(
mark(a.title(count), Humanizer.NumberToWordsExtension.ToWords(count))
return Results.Text(contentType: "text/html; charset=utf-8", content: request.Headers.ContainsKey("HX-Request") ?
h1("Counter") +
p(new A("role", "status"), $"Current count: {result}") +
button(
class_("btn btn-primary"),
new A("hx-get", $"/api/counter/{count + 1}"),
new A("hx-target", "#result"),
"Click me"
) :
result
);
});

var summaries = new[]
{
string[] summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
];

app.MapGet("/weather", () =>
{
var forecasts = Enumerable.Range(1, 5).Select(index =>
Func<WeatherForecast[]> forecasts = () =>
Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
)
)
.ToArray();

return Results.Extensions.Html($$"""
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
{{forecasts.Each(x => tr(
td(x.Date.ToShortDateString()),
td(x.TemperatureC),
td(x.TemperatureF),
td(x.Summary)
))}}
</tbody>
</table>
""");
});
app.MapGet("/weather", () => Results.Text(contentType: "text/html; charset=utf-8", content:
$$"""
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
{{forecasts().Each(x => tr(
td(x.Date.ToShortDateString()),
td(x.TemperatureC),
td(x.TemperatureF),
td(x.Summary)
))}}
</tbody>
</table>
"""
));

app.Run();

internal record WeatherForecast(DateOnly Date, int TemperatureC, string Summary)
record WeatherForecast(DateOnly Date, int TemperatureC, string Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
24 changes: 0 additions & 24 deletions samples/Markupolation.Sample.Api/ResultsExtensions.cs

This file was deleted.

21 changes: 0 additions & 21 deletions samples/Markupolation.Sample.Functions/Counter.cs

This file was deleted.

105 changes: 105 additions & 0 deletions samples/Markupolation.Sample.Functions/Functions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

namespace Markupolation.Sample.Functions
{
public class Functions
{
[Function(nameof(Html))]
public IActionResult Html([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) => new ContentResult
{
ContentType = "text/html; charset=utf-8",
Content =
DOCTYPE() +
html(lang("en"),
head(
meta(charset("utf-8")),
e.title("Markupolation.Sample.Functions"),
meta(name("description"), content("Sample of how to use Markupolation in an Azure Function")),
meta(name("viewport"), content("width=device-width, initial-scale=1"))
),
body(onload("console.log('Markupolation in an Azure Function');"),
h1("Hello, World!"),
p("This is ", mark(a.title("Markup with string interpolation"), "Markupolation"), " in action.")
)
),
};

[Function(nameof(Hello))]
public IActionResult Hello([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) => new ContentResult
{
ContentType = "text/html; charset=utf-8",
Content = h1("Hello, World!") + p("This is ", mark(title("Markup with string interpolation"), "Markupolation"), " in action."),
};

[Function(nameof(Counter))]
public IActionResult Counter([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "Counter/{count:int}")] HttpRequestData req, int count)
{
var result = mark(a.title(count), Humanizer.NumberToWordsExtension.ToWords(count));

return new ContentResult
{
ContentType = "text/html; charset=utf-8",
Content = req.Headers.Contains("HX-Request") ?
h1("Counter") +
p(new A("role", "status"), $"Current count: {result}") +
button(
class_("btn btn-primary"),
new A("hx-get", $"/api/counter/{count + 1}"),
new A("hx-target", "#result"),
"Click me"
) :
result,
};
}

static readonly string[] summaries =
[
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
];

static readonly Func<WeatherForecast[]> forecasts = () =>
Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
)
)
.ToArray();

[Function(nameof(Weather))]
public IActionResult Weather([HttpTrigger(AuthorizationLevel.Anonymous, "get")] HttpRequestData req) => new ContentResult
{
ContentType = "text/html; charset=utf-8",
Content =
$$"""
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
{{forecasts().Each(x => tr(
td(x.Date.ToShortDateString()),
td(x.TemperatureC),
td(x.TemperatureF),
td(x.Summary)
))}}
</tbody>
</table>
""",
};

record WeatherForecast(DateOnly Date, int TemperatureC, string Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
}
21 changes: 0 additions & 21 deletions samples/Markupolation.Sample.Functions/Hello.cs

This file was deleted.

33 changes: 0 additions & 33 deletions samples/Markupolation.Sample.Functions/Html.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Humanizer" Version="2.14.1" />
<PackageReference Include="Markupolation" Version="2.0.0" />
<PackageReference Include="Markupolation.Extensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.2.1" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.2" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
Expand Down
2 changes: 1 addition & 1 deletion samples/Markupolation.Sample.Functions/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
.ConfigureFunctionsWorkerDefaults()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
Expand Down
Loading

0 comments on commit 6335f08

Please sign in to comment.