123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 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);
- }
-
- [HttpGet("Login/{username}/{password}")]
- public IActionResult Login(string username, string password)
- {
- return new OkObjectResult(userRepository.Login(username, password));
- }
-
- [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();
- }
- }
- }
|