示例#1
0
        public async Task <ActionResult <SearchPetsResult> > SearchPets([FromBody] SearchPetsQuery query)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            //An AuthorizeAsync overload is invoked to determine whether the current user is allowed to search pets with particular statuses.
            var authorizationResult = await _authorizationService.AuthorizeAsync(User, query, new PetsAuthorizationRequirement());

            if (!authorizationResult.Succeeded)
            {
                return(Unauthorized());
            }
            var result = await _petsSearchService.SearchPetsAsync(query);

            return(Ok(result));
        }
示例#2
0
        public async Task <SearchPetsResult> SearchPetsAsync(SearchPetsQuery query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            //TechDept: Since the public petstore API doesn't support pagination on the server side, we must load all the pets and do this on the client side in memory.
            var pets = await _petsApiClient.FindPetsByStatusAsync(query.Statuses.Select(x => (Anonymous)x));

            var result = new SearchPetsResult
            {
                TotalCount = pets.Count,
                //Use automapper for map  dto's types from remote  service into our domains types
                Pets = pets.Skip(query.Skip).Take(query.Take)
                       .Select(x => _mapper.Map <Models.Pet>(x))
            };

            return(result);
        }