using Microsoft.AspNetCore.Mvc; using System.Transactions; using UnivateProperties_API.Model.Users; using UnivateProperties_API.Repository; namespace UnivateProperties_API.Controllers.Users { [Route("api/[controller]")] [ApiController] public class UserRoleController : ControllerBase { private readonly IRepository _Repo; public UserRoleController(IRepository rp) { _Repo = rp; } [HttpGet] public IActionResult Get() { var roles = _Repo.GetAll(); return new OkObjectResult(roles); } [HttpGet("{id}")] public IActionResult Get(int id) { return new OkObjectResult(_Repo.Get(x => x.Id == id)); } [HttpPost] public IActionResult Post([FromBody] UserRole userRole) { using (var scope = new TransactionScope()) { _Repo.Insert(userRole); scope.Complete(); return CreatedAtAction(nameof(Get), new { id = userRole.Id }, userRole); } } [HttpPut] public IActionResult Put([FromBody] UserRole userRole) { if (userRole != null) { using (var scope = new TransactionScope()) { _Repo.Update(userRole); scope.Complete(); return new OkResult(); } } return new NoContentResult(); } [HttpDelete("{id}")] public IActionResult Delete(int id) { _Repo.RemoveAtId(id); return new OkResult(); } } }