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");
} }
@ -53,7 +56,7 @@ namespace Aps_Single_Page_Anwendung.Controllers
} }
[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)
@ -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();
}
} }
} }