diff --git a/WebApi/Controllers/PizzaController.cs b/WebApi/Controllers/PizzaController.cs new file mode 100644 index 0000000..016a53a --- /dev/null +++ b/WebApi/Controllers/PizzaController.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using WebApi.Models; +using WebApi.Services; + +namespace WebApi.Controllers; + +[ApiController] +[Route("[controller]")] +public class PizzaController : Controller +{ + public PizzaController() + { + + } + + [HttpGet] + public ActionResult> GetAll() => PizzaService.GetAll(); + + [HttpGet("{id}")] + public ActionResult Get(int id) + { + var pizza = PizzaService.Get(id); + if (pizza == null) + return NotFound(); + + return pizza; + } +} \ No newline at end of file diff --git a/WebApi/Models/Pizza.cs b/WebApi/Models/Pizza.cs new file mode 100644 index 0000000..937ddb7 --- /dev/null +++ b/WebApi/Models/Pizza.cs @@ -0,0 +1,8 @@ +namespace WebApi.Models; + +public class Pizza +{ + public int Id { get; set; } + public string? Name { get; set; } + public bool IsGluentFree { get; set; } +} \ No newline at end of file diff --git a/WebApi/Services/PizzaService.cs b/WebApi/Services/PizzaService.cs new file mode 100644 index 0000000..e734255 --- /dev/null +++ b/WebApi/Services/PizzaService.cs @@ -0,0 +1,48 @@ +using WebApi.Models; + +namespace WebApi.Services; + +public static class PizzaService +{ + static List Pizzas { get; } + static int nextId = 0; + + static PizzaService() + { + Pizzas = new List() + { + new Pizza() { Id = 1, Name = "Classic Italian", IsGluentFree = false }, + new Pizza() { Id = 2, Name = "Veggie", IsGluentFree = true }, + }; + + nextId = Pizzas.Count + 1; + } + + public static List GetAll() => Pizzas; + + public static Pizza? Get(int id) => Pizzas.FirstOrDefault(p => p.Id == id); + + public static void Add(Pizza pizza) + { + pizza.Id = nextId++; + Pizzas.Add(pizza); + } + + public static void Delete(int id) + { + var pizza = Get(id); + if (pizza is null) + return; + + Pizzas.Remove(pizza); + } + + public static void Update(Pizza pizza) + { + var index = Pizzas.FindIndex(p => p.Id == pizza.Id); + if (index < 0) + return; + + Pizzas[index] = pizza; + } +} \ No newline at end of file diff --git a/WebApi/WebApi.http b/WebApi/WebApi.http index 840c1df..937cc68 100644 --- a/WebApi/WebApi.http +++ b/WebApi/WebApi.http @@ -1,6 +1,15 @@ @WebApi_HostAddress = http://localhost:5002 +### Basic GET {{WebApi_HostAddress}}/weatherforecast/ Accept: application/json -### +### Pizza Get +GET {{WebApi_HostAddress}}/pizza/ +Accept: application/json + +### Pizza Get by id +GET {{WebApi_HostAddress}}/pizza/1 +Accept: application/json + +### \ No newline at end of file