private void heart_filled_Loaded(object sender, RoutedEventArgs e) { Listing cur = heart_empty.DataContext as Listing; currentListing = cur; if (cur.isFavorite) heart_filled.Visibility = Visibility.Visible; else heart_filled.Visibility = Visibility.Collapsed; }
/// <summary> /// Mark/unmark as a favorite, and add/remove to the favorites /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void favoriteButton_Click(object sender, RoutedEventArgs e) { currentListing = favoriteButton.DataContext as Listing; await FavoritesAccess.RemoveFavoriteListing(currentListing, App.userID); // Remove from the collection locally, to avoid refreshing via a GET call for (int a = 0; a < FavoritesAccess.favoriteListings.Count; a++) { Listing li = FavoritesAccess.favoriteListings[a]; if (li.listing_id == currentListing.listing_id) { FavoritesAccess.favoriteListings.Remove(li); break; } } }
public static async Task<ShopCart> addToCart(Listing listing) { int listing_id = listing.listing_id; HttpClient client = new HttpClient(); string baseURL = App.baseURL, errorMessage = ""; ShopCart current_cart = new ShopCart(); if (App.logged_in == false) // this function only applies to a logged in user return current_cart; List<Parameter> parameters = new List<Parameter>(); parameters.Add(new Parameter("listing_id", listing_id.ToString())); baseURL = string.Format("{0}/users/{1}/carts", baseURL, App.userID); baseURL = AuthenticationAccess.addAuthentication(baseURL, parameters, "POST"); // Create the POST request content var values = new List<KeyValuePair<string, string>> { // add the information pair to the values list new KeyValuePair<string, string>("listing_id", listing_id.ToString()) }; try { HttpResponseMessage response = await client.PostAsync(baseURL, new FormUrlEncodedContent(values)); var jsonStream = await response.Content.ReadAsStreamAsync(); //var jsonString = await response.Content.ReadAsStringAsync(); using (StreamReader reader = new StreamReader(jsonStream)) { var serializer = new DataContractJsonSerializer(typeof(ShopCart)); current_cart = (ShopCart)serializer.ReadObject(jsonStream); } } catch(Exception e) { errorMessage = e.Message; } return current_cart; }
/// <summary> /// Add the given listing to the user's favorites. Mark it as a favorite upon success. /// Return the new listing optionally /// </summary> /// <param name="listing_id"></param> /// <param name="user_id"></param> /// <returns></returns> public static async Task AddFavoriteListing(Listing listing, string user_id) { HttpClient client = new HttpClient(); string baseURL = App.baseURL, errorMessage = ""; if (App.logged_in == false) return; List<Parameter> parameters = new List<Parameter>(); parameters.Add(new Parameter("listing_id", listing.listing_id.ToString())); baseURL = string.Format("{0}/users/{1}/favorites/listings/{2}", baseURL, App.userID, listing.listing_id); baseURL = AuthenticationAccess.addAuthentication(baseURL, parameters, "POST"); // Create the POST request content var values = new List<KeyValuePair<string, string>> { // add the information pair to the values list new KeyValuePair<string, string>("listing_id", listing.listing_id.ToString()) }; try { HttpResponseMessage response = await client.PostAsync(baseURL, new FormUrlEncodedContent(values)); var jsonString = await response.Content.ReadAsStringAsync(); listing.isFavorite = true; } catch (Exception e) { errorMessage = e.Message; } return; }
/// <summary> /// Load the item info and set up the page sources according to the item /// </summary> /// <param name="sender"> /// The source of the event; typically <see cref="NavigationHelper"/> /// </param> /// <param name="e">Event data that provides both the navigation parameter passed to /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested and /// a dictionary of state preserved by this page during an earlier /// session. The state will be null the first time a page is visited.</param> private async void navigationHelper_LoadState(object sender, LoadStateEventArgs e) { initial_Orientation(); Window.Current.SizeChanged += Current_SizeChanged; //this.DefaultViewModel["itemTitle"] = listing.title; try { // assign the page listing to the one that was passed as an argument listing = (Listing)e.NavigationParameter; } catch { } // Set up variations *************************************************** // See var1Box and var2Box loaded events await setupVariations(); // assign the page shop shop.general_info = listing.Shop; this.DefaultViewModel["listing"] = listing; this.DefaultViewModel["listingImages"] = listing.Images; // Images this.DefaultViewModel["shopInfo"] = shop.general_info; this.DefaultViewModel["shippingList"] = listing.shippingPractical; //ShippingInfo; // shipping // Select the first image to be displayed as the main image if (imgListView.Items.Count > 0) imgListView.SelectedIndex = 0; await loadUserFeedBack(); // get user feedback this.DefaultViewModel["userReviews"] = user_reviews; // user feedback }
public static async Task<ShopCart> addToCart_WithVariations(Listing listing) { int listing_id = listing.listing_id; HttpClient client = new HttpClient(); string baseURL = App.baseURL, errorMessage = ""; ShopCart current_cart = new ShopCart(); if (App.logged_in == false) // this function only applies to a logged in user return current_cart; // Build variations string string variations = "{"; foreach (var variation in listing.variations.results) { if (variation.selected_option_id != -1) { variations += "\"" + Convert.ToString(variation.property_id) + "\"" + ":" + Convert.ToString(variation.selected_option_id) + ","; } } if (variations[variations.Length - 1] == ',') variations = variations.Substring(0, variations.Length - 1) + '}'; // parameters List<Parameter> parameters = new List<Parameter>(); parameters.Add(new Parameter("listing_id", listing_id.ToString())); parameters.Add(new Parameter("quantity", listing.quantity_chosen.ToString())); parameters.Add(new Parameter("selected_variations", variations)); baseURL = string.Format("{0}/users/{1}/carts", baseURL, App.userID); baseURL = AuthenticationAccess.addAuthentication(baseURL, parameters, "POST"); // Create the POST request content var values = new List<KeyValuePair<string, string>> { // add the information pair to the values list new KeyValuePair<string, string>("listing_id", listing_id.ToString()), new KeyValuePair<string, string>("quantity", listing.quantity_chosen.ToString()), new KeyValuePair<string, string>("selected_variations", variations) }; try { HttpResponseMessage response = await client.PostAsync(baseURL, new FormUrlEncodedContent(values)); var jsonStream = await response.Content.ReadAsStreamAsync(); //var jsonString = await response.Content.ReadAsStringAsync(); using (StreamReader reader = new StreamReader(jsonStream)) { var serializer = new DataContractJsonSerializer(typeof(ShopCart)); current_cart = (ShopCart)serializer.ReadObject(jsonStream); } } catch (Exception e) { errorMessage = e.Message; } return current_cart; }
/// <summary> /// Send a DELETE request to remove the specified listing from the user's favorites /// </summary> /// <param name="listing"></param> /// <param name="user_id"></param> /// <returns></returns> public static async Task RemoveFavoriteListing(Listing listing, string user_id) { HttpClient client = new HttpClient(); string baseURL = App.baseURL, errorMessage = ""; if (App.logged_in == false) // this function only applies to a logged in user return; List<Parameter> parameters = new List<Parameter>(); parameters.Add(new Parameter("listing_id", listing.listing_id.ToString())); baseURL = string.Format("{0}/users/{1}/favorites/listings/{2}", baseURL, App.userID, listing.listing_id); baseURL = AuthenticationAccess.addAuthentication(baseURL, parameters, "DELETE"); try { HttpResponseMessage response = await client.DeleteAsync(baseURL); var jsonString = await response.Content.ReadAsStringAsync(); listing.isFavorite = false; } catch (Exception e) { errorMessage = e.Message; } }