您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RestaurantController.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using Microsoft.AspNetCore.Mvc;
  2. using ProRestaurant.Containers;
  3. using ProRestaurant.Models.Restaurants;
  4. using ProRestaurant.Repository.Restaurants;
  5. using System.Transactions;
  6. namespace ProRestaurant.Controllers.Restaurants
  7. {
  8. [Route("api/[controller]")]
  9. [ApiController]
  10. public class RestaurantController : ControllerBase
  11. {
  12. private readonly IRestaurantRepository repo;
  13. public RestaurantController(IRestaurantRepository _repo)
  14. {
  15. repo = _repo;
  16. }
  17. [HttpGet("GetRestaurants")]
  18. public IActionResult GetRestaurants()
  19. {
  20. return new OkObjectResult(repo.GetRestaurants());
  21. }
  22. [HttpGet]
  23. public IActionResult Get()
  24. {
  25. return new OkObjectResult(repo.GetSearch());
  26. }
  27. [HttpGet("{id}")]
  28. public IActionResult Get(int id)
  29. {
  30. var restaurant = repo.GetRestaurant(u => u.Id == id);
  31. return new OkObjectResult(restaurant);
  32. }
  33. [HttpGet("GetTradingHours/{id}")]
  34. public IActionResult GetTradingHours(int id)
  35. {
  36. return new OkObjectResult(repo.GetTradingHours(id));
  37. }
  38. [HttpPost]
  39. public IActionResult Post([FromBody] Restaurant restaurant)
  40. {
  41. using (var scope = new TransactionScope())
  42. {
  43. repo.Insert(restaurant);
  44. scope.Complete();
  45. return CreatedAtAction(nameof(Get), new { id = restaurant.Id }, restaurant);
  46. }
  47. }
  48. [HttpPut]
  49. public IActionResult Put([FromBody] RestaurantDetails restaurant)
  50. {
  51. if (restaurant != null)
  52. {
  53. using (var scope = new TransactionScope())
  54. {
  55. repo.Update(restaurant);
  56. scope.Complete();
  57. return new OkResult();
  58. }
  59. }
  60. return new NoContentResult();
  61. }
  62. [HttpDelete("{id}")]
  63. public IActionResult Delete(int id)
  64. {
  65. repo.Remove(repo.GetRestaurant(u => u.Id == id));
  66. return new OkResult();
  67. }
  68. [HttpDelete("RemoveTradingHours/{id}")]
  69. public IActionResult RemoveTradingHours(int id)
  70. {
  71. repo.RemoveTradeHours(id);
  72. return new OkResult();
  73. }
  74. }
  75. }