Esempio n. 1
0
        //[Authorize]
        public async Task <IActionResult> AddExercise(ExerciseForCreationDto exerciseForCreationDto)
        {
            var exerciseToReturn = _mapper.Map <Exercise>(exerciseForCreationDto);

            exerciseToReturn.Subject = await _subjectRepository.GetSubjectByIdAsync(exerciseForCreationDto.SubjectId);

            _exerciseRepository.Add <Exercise>(exerciseToReturn);
            await _exerciseCategoriesRepository.SaveAll();

            // Add categories to exercise
            var id = exerciseToReturn.Id;

            foreach (var categoryId in exerciseForCreationDto.CategoriesIds)
            {
                var exerciseCategory = new ExerciseCategory {
                    ExerciseId = id,
                    CategoryId = categoryId
                };
                _exerciseCategoriesRepository.Add <ExerciseCategory>(exerciseCategory);
            }

            if (await _exerciseCategoriesRepository.SaveAll())
            {
                var exerciseToReturnDto = _mapper.Map <ExerciseDto>(exerciseToReturn);
                return(CreatedAtRoute("GetExercise", new { exerciseToReturn.Id }, exerciseToReturnDto));
            }

            throw new Exception("Nie udało się dodać zadania");
        }
Esempio n. 2
0
        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));
        }
Esempio n. 3
0
        //[Authorize]
        public async Task <IActionResult> EditExercise(int id, ExerciseForCreationDto exerciseForCreationDto)
        {
            var exerciseToEdit = await _exerciseRepository.GetExerciseByIdAsync(id);

            var exerciseToReturn = _mapper.Map(exerciseForCreationDto, exerciseToEdit);

            exerciseToReturn.Subject = await _subjectRepository.GetSubjectByIdAsync(exerciseForCreationDto.SubjectId);

            _exerciseRepository.Edit(exerciseToReturn);

            await _exerciseRepository.SaveAll();

            // delete exercise categories
            var exerciseCategories = await _exerciseCategoriesRepository.GetExerciseCategoriesForExerciseAsync(id);

            foreach (var exerciseCategory in exerciseCategories)
            {
                _exerciseCategoriesRepository.Delete(exerciseCategory);
            }
            await _exerciseCategoriesRepository.SaveAll();


            // Edit exercise categories
            foreach (var categoryId in exerciseForCreationDto.CategoriesIds)
            {
                var exerciseCategory = new ExerciseCategory {
                    ExerciseId = id,
                    CategoryId = categoryId
                };
                _exerciseCategoriesRepository.Add(exerciseCategory);
            }

            if (await _exerciseCategoriesRepository.SaveAll())
            {
                return(NoContent());
            }
            return(BadRequest("Nie udało się edytować zadania"));
        }