| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 | using Microsoft.AspNetCore.Mvc;
using ProRestaurant.Containers;
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);
        }
        
        [HttpGet("GetTradingHours/{id}")]
        public IActionResult GetTradingHours(int id)
        {
            return new OkObjectResult(repo.GetTradingHours(id));
        }
        [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] RestaurantDetails 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();
        }
        [HttpDelete("RemoveTradingHours/{id}")]
        public IActionResult RemoveTradingHours(int id)
        {
            repo.RemoveTradeHours(id);
            return new OkResult();
        }
    }
}
 |