You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MenuCategoryController.cs 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Microsoft.AspNetCore.Mvc;
  2. using ProRestaurant.Models.Restaurants;
  3. using ProRestaurant.Repository.Restaurants;
  4. using System.Linq;
  5. using System.Transactions;
  6. namespace ProRestaurant.Controllers.Restaurants
  7. {
  8. [Route("api/[controller]")]
  9. [ApiController]
  10. public class MenuCategoryController : ControllerBase
  11. {
  12. private readonly IMenuCategoryRepository repo;
  13. public MenuCategoryController(IMenuCategoryRepository _repo)
  14. {
  15. repo = _repo;
  16. }
  17. [HttpGet("{id}")]
  18. public IActionResult Get(int id)
  19. {
  20. return new OkObjectResult(repo.GetMenuCategory(r => r.Id == id));
  21. }
  22. [HttpGet("GetRestaurantCategories/{id}")]
  23. public IActionResult GetRestaurantCategories(int id)
  24. {
  25. return new OkObjectResult(repo.GetMenuCategories(r => r.RestaurantId == id).OrderBy(r => r.Description));
  26. }
  27. [HttpPost]
  28. public IActionResult Post([FromBody] MenuCategory menuCategory)
  29. {
  30. using (var scope = new TransactionScope())
  31. {
  32. repo.Insert(menuCategory);
  33. scope.Complete();
  34. return CreatedAtAction(nameof(Get), new { id = menuCategory.Id }, menuCategory);
  35. }
  36. }
  37. [HttpPut]
  38. public IActionResult Put([FromBody] MenuCategory menuCategory)
  39. {
  40. if (menuCategory != null)
  41. {
  42. using (var scope = new TransactionScope())
  43. {
  44. repo.Update(menuCategory);
  45. scope.Complete();
  46. return new OkResult();
  47. }
  48. }
  49. return new NoContentResult();
  50. }
  51. [HttpDelete("{id}")]
  52. public IActionResult Delete(int id)
  53. {
  54. repo.Remove(repo.GetMenuCategory(u => u.Id == id));
  55. return new OkResult();
  56. }
  57. }
  58. }