public async Task <IActionResult> Search([FromQuery] ItemSearchRequest requestArgs)
 {
     return(await FunctionWrapper.ExecuteFunction(this, async() =>
     {
         return await _itemRepository.Search(requestArgs);
     }));
 }
Example #2
0
        public async Task <List <Item> > Search(ItemSearchRequest request)
        {
            List <FilterDefinition <Item> > filters = new List <FilterDefinition <Item> >();

            if (!string.IsNullOrEmpty(request.TypeId))
            {
                filters.Add(Builders <Item> .Filter.Eq("TypeId", new ObjectId(request.TypeId)));
            }
            if (!string.IsNullOrEmpty(request.ParentId))
            {
                filters.Add(Builders <Item> .Filter.Eq("ParentId", new ObjectId(request.ParentId)));
            }
            if (!string.IsNullOrEmpty(request.Name))
            {
                filters.Add(Builders <Item> .Filter.Regex("Name", new BsonRegularExpression($".*{request.Name}.*")));
            }
            if (!string.IsNullOrEmpty(request.Json))
            {
                filters.Clear(); // For now, I see no reason to support both Json filtering and explicit field filtering
                filters.Add(new JsonFilterDefinition <Item>(request.Json));
            }

            try
            {
                var filterConcat = Builders <Item> .Filter.And(filters);

                ResetTemporaryCaches();

                var items = await _context.Items.Find(filterConcat).ToListAsync();

                List <Item> itemsToReturn = new List <Item>();

                foreach (var item in items)
                {   // Hydrate items before returning
                    itemsToReturn.Add(await HydrateForGetAndSave(item));
                }

                return(itemsToReturn);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }