public async Task <ActionResult <MuscleForReturnDto> > GetMuscleAsync(int id) { var muscle = await muscleRepository.GetByIdAsync(id); if (muscle == null) { return(NotFound($"Muscle with id {id} does not exist.")); } var muscleToReturn = mapper.Map <MuscleForReturnDto>(muscle); return(Ok(muscleToReturn)); }
public async Task <ActionResult <MuscleForReturnDto> > GetMuscleAsync(int id) { var muscle = await muscleRepository.GetByIdAsync(id); if (muscle == null) { return(NotFound(new ProblemDetailsWithErrors($"Muscle with id {id} does not exist.", 404, Request))); } var muscleToReturn = mapper.Map <MuscleForReturnDto>(muscle); return(Ok(muscleToReturn)); }
public async Task <ActionResult <ExerciseForReturnDto> > CreateExercise([FromBody] ExerciseForCreationDto exercise) { var newExercise = mapper.Map <Exercise>(exercise); if (exercise.PrimaryMuscleId != null) { var primaryMuscle = await muscleRepository.GetByIdAsync(exercise.PrimaryMuscleId.Value); newExercise.PrimaryMuscle = primaryMuscle; } if (exercise.SecondaryMuscleId != null) { var secondaryMuscle = await muscleRepository.GetByIdAsync(exercise.SecondaryMuscleId.Value); newExercise.SecondaryMuscle = secondaryMuscle; } if (exercise.EquipmentIds != null) { newExercise.Equipment = exercise.EquipmentIds.Select(eqId => new EquipmentExercise { EquipmentId = eqId }).ToList(); } if (exercise.ExerciseCategoryIds != null) { newExercise.ExerciseCategorys = exercise.ExerciseCategoryIds.Select(ecId => new ExerciseCategoryExercise { ExerciseCategoryId = ecId }).ToList(); } exerciseRepository.Add(newExercise); var saveResult = await exerciseRepository.SaveAllAsync(); if (!saveResult) { return(BadRequest(new ProblemDetailsWithErrors("Unable to create exercise.", 400, Request))); } var exerciseToReturn = mapper.Map <ExerciseForReturnDto>(newExercise); return(CreatedAtRoute("GetExercise", new { id = newExercise.Id }, exerciseToReturn)); }