You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.8 KiB
77 lines
1.8 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using WebAPIWithEF.Models;
|
|
using WebAPIWithEF.Services;
|
|
|
|
namespace WebAPIWithEF.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("controller")]
|
|
public class PizzaController : ControllerBase
|
|
{
|
|
PizzaService _service;
|
|
|
|
public PizzaController(PizzaService service)
|
|
{
|
|
_service = service;
|
|
}
|
|
|
|
[HttpGet]
|
|
public IEnumerable<Pizza> GetAll()
|
|
{
|
|
return _service.GetAll();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public ActionResult<Pizza> GetById(int id)
|
|
{
|
|
var pizza = _service.GetById(id);
|
|
if (pizza is null)
|
|
return NotFound();
|
|
|
|
return pizza;
|
|
}
|
|
|
|
[HttpPost]
|
|
public IActionResult Create(Pizza newPizza)
|
|
{
|
|
var pizza = _service.Create(newPizza);
|
|
return CreatedAtAction(nameof(GetById), new { id = pizza!.Id }, pizza);
|
|
}
|
|
|
|
[HttpPut("{id}/addtopping")]
|
|
public IActionResult AddTopping(int id, int toppingId)
|
|
{
|
|
var pizzaToUpdate = _service.GetById(id);
|
|
if (pizzaToUpdate is null)
|
|
return NotFound();
|
|
|
|
_service.AddTopping(id, toppingId);
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpPut("{id}/updatesauce")]
|
|
public IActionResult UpdateSauce(int id, int sauceId)
|
|
{
|
|
var pizzaToUpdate = _service.GetById(id);
|
|
if (pizzaToUpdate is null)
|
|
return NotFound();
|
|
|
|
_service.UpdateSauce(id, sauceId);
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public IActionResult Delete(int id)
|
|
{
|
|
var pizza = _service.GetById(id);
|
|
if (pizza is null)
|
|
return NotFound();
|
|
|
|
_service.DeleteById(id);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|
|
|