1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using Microsoft.AspNetCore.Mvc;
- using ProRestaurant.Models.Restaurants;
- using ProRestaurant.Repository.Restaurants;
- using System.Transactions;
-
- namespace ProRestaurant.Controllers.Restaurants
- {
- [Route("api/[controller]")]
- [ApiController]
- public class RestaurantController : ControllerBase
- {
- private readonly IRestaurantRepository repo;
-
- public RestaurantController(IRestaurantRepository _repo)
- {
- repo = _repo;
- }
-
- [HttpGet]
- public IActionResult Get()
- {
- return new OkObjectResult(repo.GetUsers());
- }
-
- [HttpGet("{id}")]
- public IActionResult Get(int id)
- {
- var restaurant = repo.GetRestaurant(u => u.Id == id);
- return new OkObjectResult(restaurant);
- }
-
- [HttpPost]
- public IActionResult Post([FromBody] Restaurant restaurant)
- {
- using (var scope = new TransactionScope())
- {
- repo.Insert(restaurant);
- scope.Complete();
- return CreatedAtAction(nameof(Get), new { id = restaurant.Id }, restaurant);
- }
- }
-
- [HttpPut]
- public IActionResult Put([FromBody] Restaurant restaurant)
- {
- if (restaurant != null)
- {
- using (var scope = new TransactionScope())
- {
- repo.Update(restaurant);
- scope.Complete();
- return new OkResult();
- }
- }
- return new NoContentResult();
- }
-
- [HttpDelete("{id}")]
- public IActionResult Delete(int id)
- {
- repo.Remove(repo.GetRestaurant(u => u.Id == id));
- return new OkResult();
- }
- }
- }
|