Images implementiert

This commit is contained in:
marcusferl@weifer.de 2022-05-30 09:22:35 +02:00
parent 9f59a64a88
commit 7f6ddac2b8

View File

@ -1,5 +1,6 @@
using Aps_Single_Page_Anwendung.Models; using Aps_Single_Page_Anwendung.Models;
using Aps_Single_Page_Anwendung.Repositories; using Aps_Single_Page_Anwendung.Repositories;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic; using System.Collections.Generic;
@ -14,10 +15,12 @@ namespace Aps_Single_Page_Anwendung.Controllers
public class SpeisenController : ControllerBase public class SpeisenController : ControllerBase
{ {
private readonly ISpeiseRepository _repository; private readonly ISpeiseRepository _repository;
private readonly string _path;
public SpeisenController(ISpeiseRepository repository) public SpeisenController(ISpeiseRepository repository, IWebHostEnvironment env)
{ {
_repository = repository; _repository = repository;
_path = System.IO.Path.Combine(env.ContentRootPath, "data", "images", "speisen");
} }
@ -33,7 +36,7 @@ namespace Aps_Single_Page_Anwendung.Controllers
public IActionResult Get(int id) public IActionResult Get(int id)
{ {
var sepeise = _repository.GetSpeiseById(id); var sepeise = _repository.GetSpeiseById(id);
if(sepeise == null) if (sepeise == null)
{ {
return NotFound(); // StatusCode 404 return NotFound(); // StatusCode 404
} }
@ -44,16 +47,16 @@ namespace Aps_Single_Page_Anwendung.Controllers
[HttpPost] [HttpPost]
public IActionResult Post([FromBody] Speise speise) 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 return BadRequest(ModelState); // Alle Felder müssen ausgefüllt werden, stonst Statuscode 400
} }
var result = _repository.CreateSpeise(speise); 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}")] [HttpPut("{id}")]
// Zum ändern der Daten // Zum ändern der Daten der Speise
public IActionResult Put(int id, [FromBody] Speise speise) public IActionResult Put(int id, [FromBody] Speise speise)
{ {
if (id != speise.Id) if (id != speise.Id)
@ -66,7 +69,7 @@ namespace Aps_Single_Page_Anwendung.Controllers
return BadRequest(ModelState); return BadRequest(ModelState);
} }
if(_repository.GetSpeiseById(id) == null) if (_repository.GetSpeiseById(id) == null)
{ {
return NotFound(); return NotFound();
} }
@ -77,7 +80,7 @@ namespace Aps_Single_Page_Anwendung.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public IActionResult Delete(int id) public IActionResult Delete(int id)
{ {
if(_repository.GetSpeiseById(id) == null) if (_repository.GetSpeiseById(id) == null)
{ {
return NotFound(); return NotFound();
} }
@ -85,5 +88,20 @@ namespace Aps_Single_Page_Anwendung.Controllers
return NoContent(); 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();
}
} }
} }