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("GetRestaurants")] public IActionResult GetRestaurants() { return new OkObjectResult(repo.GetRestaurants()); } [HttpGet] public IActionResult Get() { return new OkObjectResult(repo.GetSearch()); } [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(); } } }