12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Transactions;
- using Microsoft.AspNetCore.Mvc;
- using ProRestaurant.Models.Restaurants;
- using ProRestaurant.Repository.Restaurants;
-
- // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
-
- namespace ProRestaurant.Controllers.Restaurants
- {
- [Route("api/[controller]")]
- [ApiController]
- public class MenuItemController : ControllerBase
- {
- private readonly IMenuItemRepository repo;
-
- public MenuItemController(IMenuItemRepository _repo)
- {
- repo = _repo;
- }
-
- [HttpGet("{id}")]
- public IActionResult Get(int id)
- {
- return new OkObjectResult(repo.GetMenuItems(r => r.RestaurantId == id));
- }
-
- [HttpGet("GetMenuItem/{id}")]
- public IActionResult GetMenuItem(int id)
- {
- return new OkObjectResult(repo.GetMenuItem(id));
- }
-
- [HttpPost]
- public IActionResult Post([FromBody] MenuItem item)
- {
- using (var scope = new TransactionScope())
- {
- repo.Insert(item);
- scope.Complete();
- return CreatedAtAction(nameof(Get), new { id = item.Id }, item);
- }
- }
-
- [HttpPut]
- public IActionResult Put([FromBody] MenuItem item)
- {
- if (item != null)
- {
- using (var scope = new TransactionScope())
- {
- repo.Update(item);
- scope.Complete();
- return new OkResult();
- }
- }
- return new NoContentResult();
- }
-
- [HttpDelete("{id}")]
- public IActionResult Delete(int id)
- {
- repo.Remove(repo.GetMenuItems(u => u.Id == id).FirstOrDefault());
- return new OkResult();
- }
- }
- }
|