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.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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]
  17. public IActionResult Get()
  18. {
  19. return new OkObjectResult(repo.GetSearch());
  20. }
  21. [HttpGet("{id}")]
  22. public IActionResult Get(int id)
  23. {
  24. var restaurant = repo.GetRestaurant(u => u.Id == id);
  25. return new OkObjectResult(restaurant);
  26. }
  27. [HttpPost]
  28. public IActionResult Post([FromBody] Restaurant restaurant)
  29. {
  30. using (var scope = new TransactionScope())
  31. {
  32. repo.Insert(restaurant);
  33. scope.Complete();
  34. return CreatedAtAction(nameof(Get), new { id = restaurant.Id }, restaurant);
  35. }
  36. }
  37. [HttpPut]
  38. public IActionResult Put([FromBody] Restaurant restaurant)
  39. {
  40. if (restaurant != null)
  41. {
  42. using (var scope = new TransactionScope())
  43. {
  44. repo.Update(restaurant);
  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.GetRestaurant(u => u.Id == id));
  55. return new OkResult();
  56. }
  57. }
  58. }