2022-05-17 12:26:45 +02:00
|
|
|
|
using Aps_Single_Page_Anwendung.Models;
|
|
|
|
|
using Aps_Single_Page_Anwendung.Repositories;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Aps_Single_Page_Anwendung.Controllers
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Routing für den Controller
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
|
|
|
|
|
// Muss von ControllerBase erben, sonst muss die Schnittstelle selbst implmentiert werden
|
|
|
|
|
public class SpeisenController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ISpeiseRepository _repository;
|
|
|
|
|
|
|
|
|
|
public SpeisenController(ISpeiseRepository repository)
|
|
|
|
|
{
|
|
|
|
|
_repository = repository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2022-05-20 11:10:58 +02:00
|
|
|
|
// Muss etwas zurück geben
|
|
|
|
|
public IEnumerable<Speise> Get()
|
2022-05-17 12:26:45 +02:00
|
|
|
|
{
|
2022-05-20 11:10:58 +02:00
|
|
|
|
return _repository.GetSpeisen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// api/speisen/1 wird zur angegebenen id geroutet
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
public IActionResult Get(int id)
|
|
|
|
|
{
|
|
|
|
|
var sepeise = _repository.GetSpeiseById(id);
|
|
|
|
|
if(sepeise == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound(); // StatusCode 404
|
|
|
|
|
}
|
|
|
|
|
return Ok(sepeise);
|
2022-05-17 12:26:45 +02:00
|
|
|
|
}
|
2022-05-23 08:53:44 +02:00
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult Post([FromBody] Speise speise)
|
|
|
|
|
{
|
|
|
|
|
if(!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ModelState); // Alle Felder müssen ausgefüllt werden, stonst Statuscode 400
|
|
|
|
|
}
|
|
|
|
|
var result = _repository.CreateSpeise(speise);
|
|
|
|
|
return CreatedAtAction("Get",new { id = result.Id },result); //Name der Action (Get), gibt Statuscode 201 zurück
|
|
|
|
|
}
|
2022-05-17 12:26:45 +02:00
|
|
|
|
}
|
|
|
|
|
}
|