public async Task <IActionResult> UpdateBestPractice(int userId, int id, BestPracticeForCreationDto bestPracticeForUpdateDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var bestPracticeFromRepo = await _repo.GetBestPractice(id); _mapper.Map(bestPracticeForUpdateDto, bestPracticeFromRepo); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetBestPractice", new { id = bestPracticeFromRepo.Id, userId = userId }, bestPracticeFromRepo)); } throw new Exception($"Updating best practice {id} failed on save"); }
public async Task <IActionResult> AddBestPractice(int userId, BestPracticeForCreationDto bestPracticeForCreation) { var creator = await _userRepo.GetUser(userId); if (creator.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var bestPractice = _mapper.Map <BestPractice>(bestPracticeForCreation); bestPractice.User = creator; _repo.Add(bestPractice); if (await _repo.SaveAll()) { var jobToReturn = _mapper.Map <BestPracticeForReturnDto>(bestPractice); return(CreatedAtRoute("GetBestPractice", new { id = bestPractice.Id, userId = userId }, jobToReturn)); } throw new Exception("Creation of Best practice failed on save"); }