Images implementiert
This commit is contained in:
parent
9f59a64a88
commit
7f6ddac2b8
|
@ -1,5 +1,6 @@
|
|||
using Aps_Single_Page_Anwendung.Models;
|
||||
using Aps_Single_Page_Anwendung.Repositories;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
@ -14,10 +15,12 @@ namespace Aps_Single_Page_Anwendung.Controllers
|
|||
public class SpeisenController : ControllerBase
|
||||
{
|
||||
private readonly ISpeiseRepository _repository;
|
||||
private readonly string _path;
|
||||
|
||||
public SpeisenController(ISpeiseRepository repository)
|
||||
public SpeisenController(ISpeiseRepository repository, IWebHostEnvironment env)
|
||||
{
|
||||
_repository = repository;
|
||||
_path = System.IO.Path.Combine(env.ContentRootPath, "data", "images", "speisen");
|
||||
}
|
||||
|
||||
|
||||
|
@ -25,7 +28,7 @@ namespace Aps_Single_Page_Anwendung.Controllers
|
|||
// Muss etwas zurück geben
|
||||
public IEnumerable<Speise> Get()
|
||||
{
|
||||
return _repository.GetSpeisen();
|
||||
return _repository.GetSpeisen();
|
||||
}
|
||||
|
||||
// api/speisen/1 wird zur angegebenen id geroutet
|
||||
|
@ -33,7 +36,7 @@ namespace Aps_Single_Page_Anwendung.Controllers
|
|||
public IActionResult Get(int id)
|
||||
{
|
||||
var sepeise = _repository.GetSpeiseById(id);
|
||||
if(sepeise == null)
|
||||
if (sepeise == null)
|
||||
{
|
||||
return NotFound(); // StatusCode 404
|
||||
}
|
||||
|
@ -44,16 +47,16 @@ namespace Aps_Single_Page_Anwendung.Controllers
|
|||
[HttpPost]
|
||||
public IActionResult Post([FromBody] Speise speise)
|
||||
{
|
||||
if(!ModelState.IsValid)
|
||||
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
|
||||
return CreatedAtAction("Get", new { id = result.Id }, result); //Name der Action (Get), gibt Statuscode 201 zurück
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
// Zum ändern der Daten
|
||||
// Zum ändern der Daten der Speise
|
||||
public IActionResult Put(int id, [FromBody] Speise speise)
|
||||
{
|
||||
if (id != speise.Id)
|
||||
|
@ -66,18 +69,18 @@ namespace Aps_Single_Page_Anwendung.Controllers
|
|||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
if(_repository.GetSpeiseById(id) == null)
|
||||
if (_repository.GetSpeiseById(id) == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
var result = _repository.UpdateSpeise(speise);
|
||||
var result = _repository.UpdateSpeise(speise);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
if(_repository.GetSpeiseById(id) == null)
|
||||
if (_repository.GetSpeiseById(id) == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
@ -85,5 +88,20 @@ namespace Aps_Single_Page_Anwendung.Controllers
|
|||
return NoContent();
|
||||
|
||||
}
|
||||
// Über die Id wird das Image ausgegeben
|
||||
[HttpGet("{id}/image")]
|
||||
|
||||
public IActionResult Image(int id)
|
||||
{
|
||||
var file = System.IO.Path.Combine(_path, $"{id}.jpg");
|
||||
|
||||
if (System.IO.File.Exists(file))
|
||||
{
|
||||
var bytes = System.IO.File.ReadAllBytes(file);
|
||||
return File(bytes, "image/jpeg");
|
||||
|
||||
}
|
||||
return NotFound();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user