parent
0dd6a438d5
commit
20d8557098
@ -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<List<Pizza>> GetAll() => PizzaService.GetAll(); |
||||
|
||||
[HttpGet("{id}")] |
||||
public ActionResult<Pizza> Get(int id) |
||||
{ |
||||
var pizza = PizzaService.Get(id); |
||||
if (pizza == null) |
||||
return NotFound(); |
||||
|
||||
return pizza; |
||||
} |
||||
} |
@ -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; } |
||||
} |
@ -0,0 +1,48 @@ |
||||
using WebApi.Models; |
||||
|
||||
namespace WebApi.Services; |
||||
|
||||
public static class PizzaService |
||||
{ |
||||
static List<Pizza> Pizzas { get; } |
||||
static int nextId = 0; |
||||
|
||||
static PizzaService() |
||||
{ |
||||
Pizzas = new List<Pizza>() |
||||
{ |
||||
new Pizza() { Id = 1, Name = "Classic Italian", IsGluentFree = false }, |
||||
new Pizza() { Id = 2, Name = "Veggie", IsGluentFree = true }, |
||||
}; |
||||
|
||||
nextId = Pizzas.Count + 1; |
||||
} |
||||
|
||||
public static List<Pizza> 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; |
||||
} |
||||
} |
@ -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 |
||||
|
||||
### |
Loading…
Reference in new issue