add user controller

This commit is contained in:
Aurelien Rebourg 2021-03-30 16:06:15 +01:00
parent 201467f438
commit 802b730856
1 changed files with 38 additions and 1 deletions

View File

@ -95,6 +95,43 @@ namespace Controllers
return Ok(model);
}
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var user = _userService.GetById(id);
var model = _mapper.Map<UserModel>(user);
return Ok(model);
}
[HttpPut("{id}")]
public IActionResult Update(int id, [FromBody]UpdateModel model)
{
//Finding who is logged in
int logged_in_user = int.Parse(User.Identity.Name);
// map model to entity and set id
var user = _mapper.Map<User>(model);
user.Id = id;
//Rejecting access if the logged in user is not same as the user updating information
if(logged_in_user != id)
{
return BadRequest(new { message = "Access Denied" });
}
try
{
// update user
_userService.Update(user, model.CurrentPassword, model.NewPassword, model.ConfirmNewPassword);
return Ok();
}
catch (AppException ex)
{
// return error message if there was an exception
return BadRequest(new { message = ex.Message });
}
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{