public async Task Should_retrieve_users_game_collection() { CollectionResponse response = await bgg.GetCollectionAsync(new CollectionRequest( USERNAME, stats : true)); Assert.True(response.Succeeded); CollectionResponse.ItemCollection items = response.Result; Assert.NotNull(items); Assert.Equal(56, items.Count); }
public async Task <bool> RemoveGame(Game g) { string collectionId = null; CollectionRequest request = new CollectionRequest(Credentials.Username); CollectionResponse r = await _bggClient.GetCollectionAsync(request); CollectionResponse.ItemCollection collection = r.Result; foreach (var c in collection) { if (c.ObjectId == g.Id) { collectionId = c.CollectionId.ToString(); break; } } if (string.IsNullOrEmpty(collectionId)) { return(false); } var uri = new Uri(baseUrl, "/geekcollection.php"); var client = GetHttpClient(baseUrl); var data = new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("ajax", "1"), new KeyValuePair <string, string>("action", "delete"), new KeyValuePair <string, string>("collid", collectionId.ToString()) }; var response = await client.PostAsync(uri, new FormUrlEncodedContent(data)); if (response.StatusCode != HttpStatusCode.OK) { return(false); } return(true); }
/// <summary> /// Gets a collection of games from BGG. "Collection" being a term BGG uses - owned, rated, played (and other categories) games all count /// </summary> /// <returns></returns> public async Task <IList <Game> > GetCollection() { List <Game> games = new List <Game>(); CollectionRequest request = new CollectionRequest(await Username(), stats: true); CollectionResponse response = await _bggApiClient.GetCollectionAsync(request); CollectionResponse.ItemCollection collection = response.Result; foreach (var g in collection) { var game = Mapper.Map <Game>(g); if (g.Status.Owned == true) { game.GameStatus = Status.Owned; } game.InCollection = true; games.Add(game); } return(games); }
public async Task <bool> RateGame(Game game, double rating, string review) { /* Fetch the collections first*/ string collectionId = null; CollectionRequest request = new CollectionRequest(Credentials.Username); CollectionResponse r = await _bggClient.GetCollectionAsync(request); CollectionResponse.ItemCollection collection = r.Result; //Try and find what the collection ID is, if anything foreach (var c in collection) { if (c.ObjectId == game.Id) { collectionId = c.CollectionId.ToString(); break; } } var client = GetHttpClient(baseUrl); var data = new Root() { item = new Item() { Objecttype = "thing", Objectid = game.Id.ToString(), Objectname = game.Name, Rating = rating, } }; //Text field is optional if (!string.IsNullOrEmpty(review)) { data.item.Textfield = new Textfield { comment = new Comment { value = review, } }; } // Updating a review? PUT /api/collectionitems/<collectionid> // Otherwise a *second* copy of the game will be added! if (!string.IsNullOrEmpty(collectionId)) { data.item.Collid = collectionId; var uri = new Uri(baseUrl, string.Format("/api/collectionitems/{0}", collectionId)); var response = await client.PutAsync(uri, new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); if (response.StatusCode != HttpStatusCode.OK) { return(false); } } else // New review? POST /api/collectionitems { var uri = new Uri(baseUrl, "/api/collectionitems"); var response = await client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json")); if (response.StatusCode != HttpStatusCode.OK) { return(false); } } return(true); }