52 lines
1.6 KiB
C#
52 lines
1.6 KiB
C#
|
using Aps_Single_Page_Anwendung.Models;
|
|||
|
using Microsoft.AspNetCore.Hosting;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Text.Json;
|
|||
|
|
|||
|
namespace Aps_Single_Page_Anwendung.Repositories
|
|||
|
{
|
|||
|
public class FileSpeiseRepository : ISpeiseRepository
|
|||
|
{
|
|||
|
private readonly string _path;
|
|||
|
|
|||
|
// Um den Absoluten Pfad zu bekommen, unabhängig davon, wo das Programm liegt
|
|||
|
public FileSpeiseRepository(IWebHostEnvironment env)
|
|||
|
{
|
|||
|
_path = Path.Combine(env.ContentRootPath, "data", "speisen.json"); // Die Trennung via Slashes wird automatisch erkannt
|
|||
|
}
|
|||
|
|
|||
|
// Implementierung der Methoden vom Interfave ISpeisenRepository
|
|||
|
public Speise CreateSpeise(Speise speise)
|
|||
|
{
|
|||
|
throw new System.NotImplementedException();
|
|||
|
}
|
|||
|
|
|||
|
public void DeleteSpeise(int id)
|
|||
|
{
|
|||
|
throw new System.NotImplementedException();
|
|||
|
}
|
|||
|
|
|||
|
public Speise GetSpeiseById(int id)
|
|||
|
{
|
|||
|
throw new System.NotImplementedException();
|
|||
|
}
|
|||
|
|
|||
|
public IEnumerable<Speise> GetSpeisen()
|
|||
|
{
|
|||
|
var json = File.ReadAllText(_path);
|
|||
|
var option = new JsonSerializerOptions
|
|||
|
{
|
|||
|
AllowTrailingCommas = true, // Ignoriert letzte Komma, fals vorhanden
|
|||
|
PropertyNameCaseInsensitive = true // Ignoriert Groß und Kleinschreibung
|
|||
|
};
|
|||
|
var speisen = JsonSerializer.Deserialize<Speise[]>(json, option);
|
|||
|
return speisen;
|
|||
|
}
|
|||
|
public Speise UpdateSpeise(Speise speise)
|
|||
|
{
|
|||
|
throw new System.NotImplementedException();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|