From 0592fc7498a261d1bed27aadfa02bac3ce4382b1 Mon Sep 17 00:00:00 2001 From: Jon Galloway Date: Fri, 30 Jun 2023 10:50:26 -0700 Subject: [PATCH] Add start and complete for Minimal API --- .../0-start/PizzaStore/PizzaStore.csproj | 9 ++++ 4-minimal-api/0-start/PizzaStore/Program.cs | 6 +++ .../PizzaStore/Properties/launchSettings.json | 28 ++++++++++ .../PizzaStore/appsettings.Development.json | 8 +++ .../0-start/PizzaStore/appsettings.json | 9 ++++ 4-minimal-api/1-complete/PizzaStore/Db.cs | 51 +++++++++++++++++++ .../1-complete/PizzaStore/PizzaStore.csproj | 13 +++++ .../1-complete/PizzaStore/Program.cs | 33 ++++++++++++ .../PizzaStore/Properties/launchSettings.json | 28 ++++++++++ .../PizzaStore/appsettings.Development.json | 8 +++ .../1-complete/PizzaStore/appsettings.json | 9 ++++ 4-minimal-api/README.md | 3 +- 12 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 4-minimal-api/0-start/PizzaStore/PizzaStore.csproj create mode 100644 4-minimal-api/0-start/PizzaStore/Program.cs create mode 100644 4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json create mode 100644 4-minimal-api/0-start/PizzaStore/appsettings.Development.json create mode 100644 4-minimal-api/0-start/PizzaStore/appsettings.json create mode 100644 4-minimal-api/1-complete/PizzaStore/Db.cs create mode 100644 4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj create mode 100644 4-minimal-api/1-complete/PizzaStore/Program.cs create mode 100644 4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json create mode 100644 4-minimal-api/1-complete/PizzaStore/appsettings.Development.json create mode 100644 4-minimal-api/1-complete/PizzaStore/appsettings.json diff --git a/4-minimal-api/0-start/PizzaStore/PizzaStore.csproj b/4-minimal-api/0-start/PizzaStore/PizzaStore.csproj new file mode 100644 index 0000000..c78c9c7 --- /dev/null +++ b/4-minimal-api/0-start/PizzaStore/PizzaStore.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/4-minimal-api/0-start/PizzaStore/Program.cs b/4-minimal-api/0-start/PizzaStore/Program.cs new file mode 100644 index 0000000..1760df1 --- /dev/null +++ b/4-minimal-api/0-start/PizzaStore/Program.cs @@ -0,0 +1,6 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Hello World!"); + +app.Run(); diff --git a/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json b/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json new file mode 100644 index 0000000..569ef24 --- /dev/null +++ b/4-minimal-api/0-start/PizzaStore/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/4-minimal-api/0-start/PizzaStore/appsettings.Development.json b/4-minimal-api/0-start/PizzaStore/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/4-minimal-api/0-start/PizzaStore/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/4-minimal-api/0-start/PizzaStore/appsettings.json b/4-minimal-api/0-start/PizzaStore/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/4-minimal-api/0-start/PizzaStore/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/4-minimal-api/1-complete/PizzaStore/Db.cs b/4-minimal-api/1-complete/PizzaStore/Db.cs new file mode 100644 index 0000000..77961dd --- /dev/null +++ b/4-minimal-api/1-complete/PizzaStore/Db.cs @@ -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 _pizzas = new List() + { + new Pizza{ Id=1, Name="Cheese" }, + new Pizza{ Id=2, Name="Pepperoni" }, + new Pizza{ Id=3, Name="Pineapple extravaganza"} + }; + + public static List 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(); + } + } \ No newline at end of file diff --git a/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj b/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj new file mode 100644 index 0000000..2c33b1c --- /dev/null +++ b/4-minimal-api/1-complete/PizzaStore/PizzaStore.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/4-minimal-api/1-complete/PizzaStore/Program.cs b/4-minimal-api/1-complete/PizzaStore/Program.cs new file mode 100644 index 0000000..8841f9f --- /dev/null +++ b/4-minimal-api/1-complete/PizzaStore/Program.cs @@ -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(); \ No newline at end of file diff --git a/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json b/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json new file mode 100644 index 0000000..569ef24 --- /dev/null +++ b/4-minimal-api/1-complete/PizzaStore/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/4-minimal-api/1-complete/PizzaStore/appsettings.Development.json b/4-minimal-api/1-complete/PizzaStore/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/4-minimal-api/1-complete/PizzaStore/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/4-minimal-api/1-complete/PizzaStore/appsettings.json b/4-minimal-api/1-complete/PizzaStore/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/4-minimal-api/1-complete/PizzaStore/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/4-minimal-api/README.md b/4-minimal-api/README.md index db214e4..c9c2d5e 100644 --- a/4-minimal-api/README.md +++ b/4-minimal-api/README.md @@ -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 ```