Add start and complete for Minimal API

This commit is contained in:
Jon Galloway 2023-06-30 10:50:26 -07:00
parent 23f6abfcdf
commit 0592fc7498
No known key found for this signature in database
GPG key ID: AA8FDF2CB5530226
12 changed files with 204 additions and 1 deletions

View file

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>

View file

@ -0,0 +1,6 @@
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();

View file

@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1201",
"sslPort": 44373
}
},
"profiles": {
"PizzaStore": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7192;http://localhost:5112",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View file

@ -0,0 +1,51 @@
namespace PizzaStore.DB;
public record Pizza
{
public int Id {get; set;}
public string ? Name { get; set; }
}
public class PizzaDB
{
private static List<Pizza> _pizzas = new List<Pizza>()
{
new Pizza{ Id=1, Name="Cheese" },
new Pizza{ Id=2, Name="Pepperoni" },
new Pizza{ Id=3, Name="Pineapple extravaganza"}
};
public static List<Pizza> GetPizzas()
{
return _pizzas;
}
public static Pizza ? GetPizza(int id)
{
return _pizzas.SingleOrDefault(pizza => pizza.Id == id);
}
public static Pizza CreatePizza(Pizza pizza)
{
_pizzas.Add(pizza);
return pizza;
}
public static Pizza UpdatePizza(Pizza update)
{
_pizzas = _pizzas.Select(pizza =>
{
if (pizza.Id == update.Id)
{
pizza.Name = update.Name;
}
return pizza;
}).ToList();
return update;
}
public static void RemovePizza(int id)
{
_pizzas = _pizzas.FindAll(pizza => pizza.Id != id).ToList();
}
}

View file

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,33 @@
using PizzaStore.DB;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "PizzaStore API", Description = "Making the Pizzas you love", Version = "v1" });
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "PizzaStore API V1");
});
app.MapGet("/", () => "Hello World!");
app.MapGet("/pizzas/{id}", (int id) => PizzaDB.GetPizza(id));
app.MapGet("/pizzas", () => PizzaDB.GetPizzas());
app.MapPost("/pizzas", (Pizza pizza) => PizzaDB.CreatePizza(pizza));
app.MapPut("/pizzas", (Pizza pizza) => PizzaDB.UpdatePizza(pizza));
app.MapDelete("/pizzas/{id}", (int id) => PizzaDB.RemovePizza(id));
app.Run();

View file

@ -0,0 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:1201",
"sslPort": 44373
}
},
"profiles": {
"PizzaStore": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7192;http://localhost:5112",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View file

@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View file

@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

View file

@ -52,7 +52,7 @@ First, you need to scaffold a project. You've installed .NET 6 and you're ready
1. Create a web API by running `dotnet new`: 1. Create a web API by running `dotnet new`:
```bash ```bash
dotnet new web -o PizzaStore dotnet new web -o PizzaStore -f net6.0
``` ```
You should see the _PizzaStore_ directory. You should see the _PizzaStore_ directory.
@ -60,6 +60,7 @@ First, you need to scaffold a project. You've installed .NET 6 and you're ready
1. Run the app by calling `dotnet run`. It builds the app and hosts it on a port from 5000 to 5300. HTTPS has a port selected for it in the range of 7000 to 7300. 1. Run the app by calling `dotnet run`. It builds the app and hosts it on a port from 5000 to 5300. HTTPS has a port selected for it in the range of 7000 to 7300.
```bash ```bash
cd PizzaStore
dotnet run dotnet run
``` ```