initial
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
using Fisher;
|
||||
using Fisher.AspNetCore;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddValidation();
|
||||
|
||||
builder.Services.ConfigureHttpJsonOptions(options =>
|
||||
{
|
||||
options.SerializerOptions.WriteIndented = true;
|
||||
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
|
||||
|
||||
});
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddDefaultPolicy(policy =>
|
||||
{
|
||||
policy.AllowAnyOrigin()
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod();
|
||||
});
|
||||
});
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddFisher(options =>
|
||||
{
|
||||
options.Connection("Data Source=app.db");
|
||||
options.InitialData.Add(new InitialTrails());
|
||||
}).ApplyAllDatabaseChangesOnStartup().SeedInitialDataOnStartup();
|
||||
var app = builder.Build();
|
||||
app.UseCors();
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.MapGet("/trails", (IQuerySession session) => session.Query<Trail>().StreamMany());
|
||||
app.MapPost("/trails", async (TrailCreate trailCreate, IDocumentSession session) =>
|
||||
{
|
||||
var trail = new Trail()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = trailCreate.Name,
|
||||
Miles = trailCreate.Miles,
|
||||
Difficulty = trailCreate.Difficulty
|
||||
};
|
||||
session.Store(trail);
|
||||
await session.SaveChangesAsync();
|
||||
return Results.Created($"/trails/{trail.Id}", trail);
|
||||
});
|
||||
|
||||
app.Run();
|
||||
public enum DifficultyRatings
|
||||
{
|
||||
Easy,
|
||||
Moderate,
|
||||
Hard,
|
||||
Extreme
|
||||
}
|
||||
|
||||
public class Trail
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public decimal Miles { get; set; }
|
||||
public string Difficulty { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public record TrailCreate
|
||||
{
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
[Required]
|
||||
public decimal Miles { get; set; }
|
||||
[Required]
|
||||
public string Difficulty { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public static class DifficultyLevels
|
||||
{
|
||||
public const string Easy = "easy";
|
||||
public const string Moderate = "moderate";
|
||||
public const string Hard = "hard";
|
||||
public const string Extreme = "extreme";
|
||||
}
|
||||
public class InitialTrails : IInitialData
|
||||
{
|
||||
public async Task Populate(IDocumentStore store, CancellationToken cancellation)
|
||||
{
|
||||
await using var session = store.LightweightSession();
|
||||
session.Store(new Trail()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Pine Ridge Trail",
|
||||
Miles = 2.5m,
|
||||
Difficulty = DifficultyLevels.Easy
|
||||
});
|
||||
session.Store(new Trail()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Canyon Loop",
|
||||
Miles = 5.0m,
|
||||
Difficulty = DifficultyLevels.Moderate
|
||||
});
|
||||
session.Store(new Trail()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Summit Ascent",
|
||||
Miles = 8.0m,
|
||||
Difficulty = DifficultyLevels.Hard
|
||||
});
|
||||
session.Store(new Trail()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Rocky Ridge",
|
||||
Miles = 12.0m,
|
||||
Difficulty = DifficultyLevels.Extreme
|
||||
});
|
||||
session.Store(new Trail()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Name = "Forest Loop",
|
||||
Miles = 3.5m,
|
||||
Difficulty = DifficultyLevels.Moderate
|
||||
});
|
||||
await session.SaveChangesAsync(token: cancellation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:1337",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Fisher" Version="1.14.0" />
|
||||
<PackageReference Include="Fisher.AspNetCore" Version="1.14.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,15 @@
|
||||
@Trails.Api_HostAddress = http://localhost:1337
|
||||
|
||||
GET http://localhost:1337/trails/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
|
||||
POST http://localhost:1337/trails/
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"name": "Chagrin Valley Trail",
|
||||
"miles": 3.8,
|
||||
"difficulty": "extreme"
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user