Beispiel #1
0
        public async Task <IHttpActionResult> GetAllAsync([FromUri] SearchExerciseModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            if (model == null)
            {
                model = new SearchExerciseModel();
            }
            var exercises = await _exerciseService.GetAllExercisesAsync(model);

            return(Ok(exercises));
        }
        private Expression <Func <Exercise, bool> > BuildSearchExpression(SearchExerciseModel searchModel)
        {
            var predicate = PredicateBuilder.New <Exercise>();

            if (string.IsNullOrEmpty(searchModel.Query))
            {
                return(PredicateBuilder.True <Exercise>());
            }
            if (searchModel.ByExercises)
            {
                predicate = predicate.Or(s => s.Description.Contains(searchModel.Query) || s.Name.Contains(searchModel.Query) || s.PreviewText.Contains(searchModel.Query));
            }
            if (searchModel.ByCriterias)
            {
                predicate = predicate.Or(s => s.ExerciseCriterias.Any(z => z.Criteria.Name.Contains(searchModel.Query)));
            }
            return(predicate);
        }
        public async Task <ExerciseDTO[]> GetAllExercisesAsync(SearchExerciseModel searchModel)
        {
            var exercies = await _exerciseRepository.Collection.Include(s => s.ExerciseCriterias).Where(BuildSearchExpression(searchModel)).ToArrayAsync();

            return(Mapper.Map <Exercise[], ExerciseDTO[]>(exercies));
        }