Exemplo n.º 1
0
        public ActionResult Get([FromQuery] CollectionRequestModel model)
        {
            try
            {
                int skip  = 0;
                var query = this.abilitiesRepository.GetAll() as IQueryable <Ability>;



                if (!string.IsNullOrEmpty(model.Name))
                {
                    query = query.Where(x => x.Name.Contains(model.Name));
                }

                if (model.Skip > 0)
                {
                    skip = model.Skip;
                }

                query = query
                        .OrderBy(x => x.Name)
                        .Skip(skip)
                        .Take(25);

                bool withRelations = !model.NoRelations;

                if (withRelations)
                {
                    query = this.abilitiesRepository.GetRelations(query) as IQueryable <Ability>;
                }

                IQueryable <AbilityViewModel> viewQuery = query.Select(x => ViewModelHelper.BuildAbilityViewModel(x, withRelations));

                return(Ok(viewQuery.ToList()));
            }
            catch (Exception ex)
            {
                return(BadRequest(new JsonResult(ex.Message)));
            }
            //return Ok(this.mapper.Map<AbilityViewModel>(query));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Get([FromRoute] Guid id, [FromQuery] CollectionRequestModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                var query = this.abilitiesRepository.GetAll()
                            .Where(x => x.Id.Equals(id))
                            as IQueryable <Ability>;

                bool withRelations = !model.NoRelations;

                if (withRelations)
                {
                    query = this.abilitiesRepository.GetRelations(query) as IQueryable <Ability>;
                }

                var viewModel = await query.Select(x => ViewModelHelper.BuildAbilityViewModel(x, withRelations))
                                .FirstOrDefaultAsync();

                //this.mapper.Map<AbilityViewModel>(query);

                if (viewModel == null)
                {
                    return(NotFound());
                }

                return(Ok(viewModel));
            }
            catch (Exception ex)
            {
                return(BadRequest(new JsonResult(ex.Message)));
            }
        }