using Microsoft.AspNetCore.Mvc; using ProRestaurant.Models.Accounts; using ProRestaurant.Repository.Accounts; using System.Transactions; namespace ProRestaurant.Controllers.Accounts { [Route("api/[controller]")] [ApiController] public class UserController : ControllerBase { private readonly IUserRepository userRepository; public UserController(IUserRepository userRepo) { userRepository = userRepo; } [HttpGet] public IActionResult Get() { return new OkObjectResult(userRepository.GetUsers()); } [HttpGet("{id}")] public IActionResult Get(int id) { var user = userRepository.GetUser(u => u.Id == id); return new OkObjectResult(user); } [HttpPost] public IActionResult Post([FromBody] User user) { using (var scope = new TransactionScope()) { userRepository.Insert(user); scope.Complete(); return CreatedAtAction(nameof(Get), new { id = user.Id }, user); } } [HttpPut] public IActionResult Put([FromBody] User user) { if (user != null) { using (var scope = new TransactionScope()) { userRepository.Update(user); scope.Complete(); return new OkResult(); } } return new NoContentResult(); } [HttpDelete("{id}")] public IActionResult Delete(int id) { userRepository.Remove(userRepository.GetUser(u => u.Id == id)); return new OkResult(); } } }