From 7f6ddac2b8e09b3277b4f05d459afde025e55b83 Mon Sep 17 00:00:00 2001 From: "marcusferl@weifer.de" Date: Mon, 30 May 2022 09:22:35 +0200 Subject: [PATCH] Images implementiert --- .../Controllers/SpeisenController.cs | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/Aps Single Page Anwendung/Controllers/SpeisenController.cs b/Aps Single Page Anwendung/Controllers/SpeisenController.cs index 3447e15..3afe970 100644 --- a/Aps Single Page Anwendung/Controllers/SpeisenController.cs +++ b/Aps Single Page Anwendung/Controllers/SpeisenController.cs @@ -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 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(); + } } }