41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Options;
|
|
using PepApi.Core.Configuration;
|
|
using PepLib.IO;
|
|
|
|
namespace PepApi.Core.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("materials")]
|
|
public class MaterialsController : ControllerBase
|
|
{
|
|
private readonly string _materialsFile;
|
|
|
|
public MaterialsController(IOptions<PepSettings> settings)
|
|
{
|
|
_materialsFile = settings.Value.MaterialsFile;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<List<MaterialData>>> GetMaterials()
|
|
{
|
|
var reader = new MaterialDataReader();
|
|
await Task.Run(() => reader.Read(_materialsFile));
|
|
|
|
return Ok(reader.Materials);
|
|
}
|
|
|
|
[HttpGet("{materialNo:int}")]
|
|
public async Task<ActionResult<List<MaterialData>>> GetMaterial(int materialNo)
|
|
{
|
|
var reader = new MaterialDataReader();
|
|
await Task.Run(() => reader.Read(_materialsFile));
|
|
|
|
var materials = reader.Materials.Where(m => m.Number == materialNo).ToList();
|
|
|
|
if (!materials.Any())
|
|
return NotFound(new { message = $"Material {materialNo} not found" });
|
|
|
|
return Ok(materials);
|
|
}
|
|
} |