public async Task <IActionResult> Update(CalendarEmployeeViewModel viewModel) { if (viewModel.Id == 0) { return(BadRequest("Employee id must not be empty")); } Employee existingEmployee = await _employeeRepository.GetAsync(viewModel.Id); if (existingEmployee == null) { return(NotFound("Employee does not exist")); } int userId = existingEmployee.UserId; int authenticatedUserId = Int32.Parse(User.Claims.Single(x => x.Type == ApplicationClaimTypes.UserId).Value); bool isAdmin = User.IsInRole(Roles.Admin); Employee employee = new Employee() { Id = existingEmployee.Id, FirstName = viewModel.FirstName, LastName = viewModel.LastName, UserId = existingEmployee.UserId }; if (!await _employeeRepository.UpdateAsync(employee)) { return(StatusCode(StatusCodes.Status500InternalServerError)); } return(NoContent()); }
public async Task <IActionResult> Get(int id) { if (id == 0) { return(BadRequest("Id must not be empty")); } Employee existingEmployee = await _employeeRepository.GetAsync(id); if (existingEmployee == null) { return(NotFound("Employee does not exist")); } int userId = existingEmployee.UserId; int authenticatedUserId = Int32.Parse(User.Claims.Single(x => x.Type == ApplicationClaimTypes.UserId).Value); bool isAdmin = User.IsInRole(Roles.Admin); if (!isAdmin && userId != authenticatedUserId) { return(Unauthorized()); } CalendarEmployeeViewModel viewModel = _mapper.Map <CalendarEmployeeViewModel>(existingEmployee); return(Json(viewModel)); }