BoardGameGeekApiClient.Service.BoardGameGeekClient.SearchBoardGames C# (CSharp) Method

SearchBoardGames() public method

public SearchBoardGames ( string query, bool exactMatch = false ) : List
query string
exactMatch bool
return List
        public List<SearchBoardGameResult> SearchBoardGames(string query, bool exactMatch = false)
        {
            try
            {
                var uriString = BASE_URL_API_V2 + $"/search?query={CleanUpQuery(query)}&type=boardgame";
                if (exactMatch)
                {
                    uriString += "&exact=1";
                }

                var searchUrl = new Uri(uriString);


                var xDoc = _apiDownloadService.DownloadApiResult(searchUrl);

                // LINQ to XML.
                var gameCollection = xDoc.Descendants("item").Select(boardgame => new SearchBoardGameResult
                {
                    BoardGameName = boardgame.Element("name").GetStringValue("value"),
                    BoardGameId = boardgame.GetIntValue("id",-1).Value,
                    YearPublished = boardgame.Element("yearpublished").GetIntValue("value",-1).Value
                }).ToList();

                if (gameCollection.Any())
                {
                    gameCollection = gameCollection.SortSearchResults(query);
                }
                return gameCollection;

            }
            catch (Exception ex)
            {
                _rollbar.SendException(ex);
                return new List<SearchBoardGameResult>();
            }
        }

Usage Example

        public void CleanUpExistingRecords()
        {
            using (NemeStatsDbContext dbContext = new NemeStatsDbContext())
            {
                using (NemeStatsDataContext dataContext = new NemeStatsDataContext(dbContext, new SecuredEntityValidatorFactory()))
                {
                    var games = dataContext.GetQueryable<GameDefinition>()
                                                            .Where(game => game.BoardGameGeekGameDefinitionId == null)
                                                            .ToList();

                    var bggSearcher = new BoardGameGeekClient(new ApiDownloaderService(), new RollbarClient());
                    int updateCount = 0;

                    foreach (GameDefinition game in games)
                    {
                        var bggResults = bggSearcher.SearchBoardGames(game.Name.Trim(), true);

                        if (bggResults.Count() == 1)
                        {
                            game.BoardGameGeekGameDefinitionId = bggResults.First().BoardGameId;
                            ApplicationUser user = new ApplicationUser
                            {
                                CurrentGamingGroupId = game.GamingGroupId
                            };
                            dataContext.Save(game, user);
                            dataContext.CommitAllChanges();
                            Console.WriteLine(game.Name + " had exactly one match and was updated.");
                            updateCount++;
                        }
                    }

                    Console.WriteLine("Updated " + updateCount + " records.");
                }
            }
        }
All Usage Examples Of BoardGameGeekApiClient.Service.BoardGameGeekClient::SearchBoardGames