forked from markjprice/cs12dotnet8
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aec9d2
commit af60d21
Showing
31 changed files
with
1,046 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
code/PracticalApps/Northwind.MinimalApi/Northwind.MinimalApi.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
<PublishAot>true</PublishAot> | ||
</PropertyGroup> | ||
|
||
</Project> |
11 changes: 11 additions & 0 deletions
11
code/PracticalApps/Northwind.MinimalApi/Northwind.MinimalApi.http
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
@Northwind.MinimalApi_HostAddress = http://localhost:5279 | ||
|
||
GET {{Northwind.MinimalApi_HostAddress}}/todos/ | ||
Accept: application/json | ||
|
||
### | ||
|
||
GET {{Northwind.MinimalApi_HostAddress}}/todos/1 | ||
Accept: application/json | ||
|
||
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Northwind.MinimalApi; | ||
using System.Text.Json.Serialization; | ||
|
||
var builder = WebApplication.CreateSlimBuilder(args); | ||
|
||
builder.Services.ConfigureHttpJsonOptions(options => | ||
{ | ||
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default); | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
var sampleTodos = TodoGenerator.GenerateTodos().ToArray(); | ||
|
||
var todosApi = app.MapGroup("/todos"); | ||
todosApi.MapGet("/", () => sampleTodos); | ||
todosApi.MapGet("/{id}", (int id) => | ||
sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo | ||
? Results.Ok(todo) | ||
: Results.NotFound()); | ||
|
||
app.Run(); | ||
|
||
[JsonSerializable(typeof(Todo[]))] | ||
internal partial class AppJsonSerializerContext : JsonSerializerContext | ||
{ | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
code/PracticalApps/Northwind.MinimalApi/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "todos", | ||
"applicationUrl": "http://localhost:5152", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace Northwind.MinimalApi | ||
{ | ||
public class Todo | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string? Title { get; set; } | ||
|
||
public DateOnly? DueBy { get; set; } | ||
|
||
public bool IsComplete { get; set; } | ||
} | ||
|
||
internal static class TodoGenerator | ||
{ | ||
private static readonly (string[] Prefixes, string[] Suffixes)[] _parts = new[] | ||
{ | ||
(new[] { "Walk the", "Feed the" }, new[] { "dog", "cat", "goat" }), | ||
(new[] { "Do the", "Put away the" }, new[] { "groceries", "dishes", "laundry" }), | ||
(new[] { "Clean the" }, new[] { "bathroom", "pool", "blinds", "car" }) | ||
}; | ||
|
||
internal static IEnumerable<Todo> GenerateTodos(int count = 5) | ||
{ | ||
var titleCount = _parts.Sum(row => row.Prefixes.Length * row.Suffixes.Length); | ||
var titleMap = new (int Row, int Prefix, int Suffix)[titleCount]; | ||
var mapCount = 0; | ||
for (var i = 0; i < _parts.Length; i++) | ||
{ | ||
var prefixes = _parts[i].Prefixes; | ||
var suffixes = _parts[i].Suffixes; | ||
for (var j = 0; j < prefixes.Length; j++) | ||
{ | ||
for (var k = 0; k < suffixes.Length; k++) | ||
{ | ||
titleMap[mapCount++] = (i, j, k); | ||
} | ||
} | ||
} | ||
|
||
Random.Shared.Shuffle(titleMap); | ||
|
||
for (var id = 1; id <= count; id++) | ||
{ | ||
var (rowIndex, prefixIndex, suffixIndex) = titleMap[id]; | ||
var (prefixes, suffixes) = _parts[rowIndex]; | ||
yield return new Todo | ||
{ | ||
Id = id, | ||
Title = string.Join(' ', prefixes[prefixIndex], suffixes[suffixIndex]), | ||
DueBy = Random.Shared.Next(-200, 365) switch | ||
{ | ||
< 0 => null, | ||
var days => DateOnly.FromDateTime(DateTime.Now.AddDays(days)) | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
code/PracticalApps/Northwind.MinimalApi/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Northwind.Mvc.Models; | ||
|
||
public record ToDo(int Id, string? Title, DateOnly? DueBy, bool IsComplete); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.