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.

RestaurantController.cs 1.9KB

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