コード例 #1
0
        public ICollection <BrowseViewModel> GetDecksByAdvancedSearch([FromBody] DeckSearchOptionsModel opts)
        {
            //if the request has an invalid body
            if (!ModelState.IsValid)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            //pass the options to the NoSql service
            ICollection <DeckModel> results;

            try
            {
                results = NoSqlService.GetDecksByAdvancedSearch(opts);
            }
            catch (HttpResponseException e)
            {
                throw e;
            }

            //create the return element
            ICollection <BrowseViewModel> searchResults = new Collection <BrowseViewModel>();

            //check to make sure we got something
            if (results == null || results.Count == 0)
            {
                return(searchResults);
            }

            //if we did, populate the search results
            foreach (var deck in results)
            {
                searchResults.Add(new BrowseViewModel
                {
                    Deck = deck,
                    Url  = "/Browse/Deck/" + deck.Id.ToString()
                });
            }
            //return the results
            return(searchResults);
        }
コード例 #2
0
        public ICollection <DeckModel> GetDecksByAdvancedSearch(DeckSearchOptionsModel opts)
        {
            //create filters with the search options
            var filter = Builders <DeckModel> .Filter.Eq(deck => deck.IsPublic, true);

            //if we get a username, create a username filter
            if ((opts.UserName != null) ||
                (string.Compare(opts.UserName, "", StringComparison.CurrentCultureIgnoreCase) == 0))
            {
                filter = filter &
                         Builders <DeckModel> .Filter.Regex(u => u.Creator,
                                                            new BsonRegularExpression("/^(.*)" + opts.UserName + "(.*?)$/i"));
            }

            //if we get a deckname, create a deckname filter
            if ((opts.UserName != null) ||
                (string.Compare(opts.UserName, "", StringComparison.CurrentCultureIgnoreCase) == 0))
            {
                filter = filter &
                         Builders <DeckModel> .Filter.Regex(u => u.DeckName,
                                                            new BsonRegularExpression("/^(.*)" + opts.DeckName + "(.*?)$/i"));
            }

            //add any colour filters
            if (opts.IsBlack)
            {
                filter = filter &
                         Builders <DeckModel> .Filter.ElemMatch(deck => deck.ColourSpread,
                                                                colour => (colour.Colour == "Black") || (colour.Colour == "black"));
            }
            if (opts.IsBlue)
            {
                filter = filter &
                         Builders <DeckModel> .Filter.ElemMatch(deck => deck.ColourSpread,
                                                                colour => (colour.Colour == "Blue") || (colour.Colour == "blue"));
            }
            if (opts.IsGreen)
            {
                filter = filter &
                         Builders <DeckModel> .Filter.ElemMatch(deck => deck.ColourSpread,
                                                                colour => (colour.Colour == "Green") || (colour.Colour == "green"));
            }
            if (opts.IsRed)
            {
                filter = filter &
                         Builders <DeckModel> .Filter.ElemMatch(deck => deck.ColourSpread,
                                                                colour => (colour.Colour == "Red") || (colour.Colour == "red"));
            }
            if (opts.IsWhite)
            {
                filter = filter &
                         Builders <DeckModel> .Filter.ElemMatch(deck => deck.ColourSpread,
                                                                colour => (colour.Colour == "White") || (colour.Colour == "white"));
            }

            //perform the search
            var result = DecksCollection.FindAsync(filter);

            Task.WaitAny(result);

            //error handling
            if (result.IsFaulted || result.IsCanceled)
            {
                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }

            //return the decks
            return(result.Result.ToList());
        }