Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MenuItemController.cs 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using Microsoft.AspNetCore.Mvc;
  7. using ProRestaurant.Models.Restaurants;
  8. using ProRestaurant.Repository.Restaurants;
  9. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  10. namespace ProRestaurant.Controllers.Restaurants
  11. {
  12. [Route("api/[controller]")]
  13. [ApiController]
  14. public class MenuItemController : ControllerBase
  15. {
  16. private readonly IMenuItemRepository repo;
  17. public MenuItemController(IMenuItemRepository _repo)
  18. {
  19. repo = _repo;
  20. }
  21. [HttpGet("{id}")]
  22. public IActionResult Get(int id)
  23. {
  24. return new OkObjectResult(repo.GetMenuItems(r => r.RestaurantId == id));
  25. }
  26. [HttpGet("GetMenuItem/{id}")]
  27. public IActionResult GetMenuItem(int id)
  28. {
  29. return new OkObjectResult(repo.GetMenuItem(id));
  30. }
  31. [HttpPost]
  32. public IActionResult Post([FromBody] MenuItem item)
  33. {
  34. using (var scope = new TransactionScope())
  35. {
  36. repo.Insert(item);
  37. scope.Complete();
  38. return CreatedAtAction(nameof(Get), new { id = item.Id }, item);
  39. }
  40. }
  41. [HttpPut]
  42. public IActionResult Put([FromBody] MenuItem item)
  43. {
  44. if (item != null)
  45. {
  46. using (var scope = new TransactionScope())
  47. {
  48. repo.Update(item);
  49. scope.Complete();
  50. return new OkResult();
  51. }
  52. }
  53. return new NoContentResult();
  54. }
  55. [HttpDelete("{id}")]
  56. public IActionResult Delete(int id)
  57. {
  58. repo.Remove(repo.GetMenuItems(u => u.Id == id).FirstOrDefault());
  59. return new OkResult();
  60. }
  61. }
  62. }