Esempio n. 1
0
        public async Task UpdateExercise(ExerciseUpdateDto exerciceUpdateDto)
        {
            var exercise = await _exerciseRepo.GetWorkoutExerciseByIdAsync(exerciceUpdateDto.Id);

            _mapper.Map(exerciceUpdateDto, exercise);
            await _unitOfWork.SaveAllAsync();
        }
        public async Task <IActionResult> UpdateExercise(int workoutId, ExerciseUpdateDto exerciceUpdateDto)
        {
            if (!await _workoutService.UserHasWorkout(User.GetUserId(), workoutId) &&
                !await _workoutService.ExerciseExistsOnWorkout(workoutId, exerciceUpdateDto.Id))
            {
                return(Unauthorized("You dont own this exercise"));
            }

            await _exerciseService.UpdateExercise(exerciceUpdateDto);

            return(NoContent());
        }
        // PUT: api/Exercises/5
        //[ResponseType(typeof(void))]

        public IHttpActionResult PutExercise(int id, ExerciseUpdateDto exercise)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != exercise.ExerciseId)
            {
                return(BadRequest());
            }

            var exer = db.Exercises.Find(id);

            AutoMapper.Mapper.Map(exercise, exer);

            // Making list of musclegroups ids got from dto
            var exerciseMuscleGroupsIdList = new List <int>();

            foreach (var i in exercise.MuscleGroups)
            {
                exerciseMuscleGroupsIdList.Add(i.MuscleGroupId);
            }

            // Checking if musclegroup existed in current exercise, if not, remove it
            foreach (var i in exer.MuscleGroups.ToList())
            {
                if (!exerciseMuscleGroupsIdList.Contains(i.MuscleGroupId))
                {
                    exer.MuscleGroups.Remove(i);
                }
            }

            // Cheking if muscle group exists in database and adds it to workout if it does
            foreach (var i in exercise.MuscleGroups)
            {
                if (!exer.MuscleGroups.Any(r => r.MuscleGroupId == i.MuscleGroupId))
                {
                    var addMuscleGroup = db.MuscleGroups.Find(i.MuscleGroupId);
                    exer.MuscleGroups.Add(addMuscleGroup);
                }
            }


            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExerciseExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            var             updatedExer = db.Exercises.Find(id);
            ExercisesGetDto result      = AutoMapper.Mapper.Map <ExercisesGetDto>(updatedExer);

            return(Ok(result));
        }