Created
October 10, 2019 21:32
-
-
Save Rudyzio/f41fbbb0ae729f26241682aafbec9e53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsersController : ControllerBase | |
{ | |
private readonly IUnitOfWork _unitOfWork; | |
public UsersController(IUnitOfWork unitOfWork) | |
{ | |
_unitOfWork = unitOfWork; | |
} | |
// GET: api/Users | |
[HttpGet] | |
public ActionResult<IEnumerable<User>> GetUsers() | |
{ | |
return _unitOfWork.Repository<User>().Find(new UsersWithRecipesAndIngredientsSpecification()).ToList(); | |
} | |
// GET: api/Users/5 | |
[HttpGet("{id}")] | |
public ActionResult<User> GetUser(int id) | |
{ | |
var user = _unitOfWork.Repository<User>().Find(new UsersWithRecipesAndIngredientsSpecification(id)).SingleOrDefault(); | |
if (user == null) | |
{ | |
return NotFound(); | |
} | |
return user; | |
} | |
//// PUT: api/Users/5 | |
[HttpPut("{id}")] | |
public IActionResult PutUser(int id, User user) | |
{ | |
if (id != user.Id) | |
{ | |
return BadRequest(); | |
} | |
_unitOfWork.Repository<User>().Update(user); | |
try | |
{ | |
_unitOfWork.Complete(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if(!_unitOfWork.Repository<User>().Contains(x => x.Id == id)) | |
{ | |
return NotFound(); | |
} | |
else | |
{ | |
throw; | |
} | |
} | |
return NoContent(); | |
} | |
// POST: api/Users | |
[HttpPost] | |
public ActionResult<User> PostUser(User user) | |
{ | |
_unitOfWork.Repository<User>().Add(user); | |
_unitOfWork.Complete(); | |
return CreatedAtAction("GetUser", new { id = user.Id }, user); | |
} | |
// DELETE: api/Users/5 | |
[HttpDelete("{id}")] | |
public ActionResult<User> DeleteUser(int id) | |
{ | |
var user = _unitOfWork.Repository<User>().FindById(id); | |
if (user == null) | |
{ | |
return NotFound(); | |
} | |
_unitOfWork.Repository<User>().Remove(user); | |
_unitOfWork.Complete(); | |
return user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment