public async Task <IActionResult> AddExersiceAsync(int userId, ExerciseForCreateDTO exerciseForCreateDTO) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } if (await _unitOfWork.Exercises.IsExerciseExist(exerciseForCreateDTO.Name)) { return(BadRequest("Error: Exercise already exist!")); } var exericeToCreate = _mapper.Map <ExerciseModel>(exerciseForCreateDTO); _unitOfWork.Exercises.Add(exericeToCreate); if (await _unitOfWork.SaveAllAsync()) { return(StatusCode(201)); } throw new Exception("Error: Saving exercise to database failed!"); }
public async Task <IActionResult> EditExerciseAsync(int userId, int exerciseId, ExerciseForCreateDTO exerciseForCreateDTO) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var exercise = await _unitOfWork.Exercises.FindOneAsync(e => e.ExerciseId == exerciseId); var editedExercise = _mapper.Map(exerciseForCreateDTO, exercise); if (exercise == editedExercise) { return(StatusCode(304)); } if (await _unitOfWork.SaveAllAsync()) { return(Ok("Info: The Exercise has been updated.")); } throw new Exception("Error: Saving edited exercise to database failed!"); }