using Microsoft.AspNetCore.Mvc; using ProRestaurant.Models.Restaurants; using ProRestaurant.Repository.Restaurants; using System.Linq; using System.Transactions; namespace ProRestaurant.Controllers.Restaurants { [Route("api/[controller]")] [ApiController] public class MenuCategoryController : ControllerBase { private readonly IMenuCategoryRepository repo; public MenuCategoryController(IMenuCategoryRepository _repo) { repo = _repo; } [HttpGet("{id}")] public IActionResult Get(int id) { return new OkObjectResult(repo.GetMenuCategory(r => r.Id == id)); } [HttpGet("GetRestaurantCategories/{id}")] public IActionResult GetRestaurantCategories(int id) { return new OkObjectResult(repo.GetMenuCategories(r => r.RestaurantId == id).OrderBy(r => r.Description)); } [HttpPost] public IActionResult Post([FromBody] MenuCategory menuCategory) { using (var scope = new TransactionScope()) { repo.Insert(menuCategory); scope.Complete(); return CreatedAtAction(nameof(Get), new { id = menuCategory.Id }, menuCategory); } } [HttpPut] public IActionResult Put([FromBody] MenuCategory menuCategory) { if (menuCategory != null) { using (var scope = new TransactionScope()) { repo.Update(menuCategory); scope.Complete(); return new OkResult(); } } return new NoContentResult(); } [HttpDelete("{id}")] public IActionResult Delete(int id) { repo.Remove(repo.GetMenuCategory(u => u.Id == id)); return new OkResult(); } } }