From d131ec1d16fff92dc546e0e548e0b35f58207a6d Mon Sep 17 00:00:00 2001 From: franc Date: Mon, 5 May 2025 12:17:06 +0200 Subject: [PATCH] Implement a new endpoint to retrieve all units for a dynamic dropdown. --- .../Controller/Unit/UnitController.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Francesco.Recipes.World/Controller/Unit/UnitController.cs b/Francesco.Recipes.World/Controller/Unit/UnitController.cs index c0efcda..d01ffd8 100644 --- a/Francesco.Recipes.World/Controller/Unit/UnitController.cs +++ b/Francesco.Recipes.World/Controller/Unit/UnitController.cs @@ -1,6 +1,25 @@ namespace Francesco.Recipes.World.Controller.Unit { - public class UnitController + using Francesco.Recipes.World.Repositories.Unit; + using Microsoft.AspNetCore.Mvc; + + [Route("Unit")] + public class UnitController : Controller { + private readonly IUnitRepository _unitRepository; + + public UnitController(IUnitRepository unitRepository) + { + _unitRepository = unitRepository; + } + + [HttpGet("GetAllUnits")] + public async Task GetAllUnits() + { + var units = (await _unitRepository.GetAllUnitsAsync()) + .Select(u => new { id = u.Id, name = u.Name }) + .ToList(); + return Json(units); + } } }