mirror of
https://github.com/dotnet/intro-to-dotnet-web-dev.git
synced 2025-12-06 05:32:03 +01:00
Add start and complete for Minimal API
This commit is contained in:
parent
23f6abfcdf
commit
0592fc7498
9
4-minimal-api/0-start/PizzaStore/PizzaStore.csproj
Normal file
9
4-minimal-api/0-start/PizzaStore/PizzaStore.csproj
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
6
4-minimal-api/0-start/PizzaStore/Program.cs
Normal file
6
4-minimal-api/0-start/PizzaStore/Program.cs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
var builder = WebApplication.CreateBuilder(args);
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapGet("/", () => "Hello World!");
|
||||
|
||||
app.Run();
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
4-minimal-api/0-start/PizzaStore/appsettings.json
Normal file
9
4-minimal-api/0-start/PizzaStore/appsettings.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
51
4-minimal-api/1-complete/PizzaStore/Db.cs
Normal file
51
4-minimal-api/1-complete/PizzaStore/Db.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
13
4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj
Normal file
13
4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj
Normal 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>
|
||||
33
4-minimal-api/1-complete/PizzaStore/Program.cs
Normal file
33
4-minimal-api/1-complete/PizzaStore/Program.cs
Normal 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();
|
||||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
4-minimal-api/1-complete/PizzaStore/appsettings.json
Normal file
9
4-minimal-api/1-complete/PizzaStore/appsettings.json
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
|
|
@ -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`:
|
||||
|
||||
```bash
|
||||
dotnet new web -o PizzaStore
|
||||
dotnet new web -o PizzaStore -f net6.0
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
```bash
|
||||
cd PizzaStore
|
||||
dotnet run
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue