Beispiel #1
0
        private void BindData(int id)
        {
            HttpResponseMessage response = productsService.GetResponse(id.ToString());

            if (response.IsSuccessStatusCode)
            {
                var jsonObject = response.Content.ReadAsStringAsync();
                product = JsonConvert.DeserializeObject <Product>(jsonObject.Result);
                ProducteNameLabel.Text = product.Name_;
                ProductImage.Source    = ImageSource.FromStream(() => new MemoryStream(product.PictureThumb));
                PriceLabel.Text        = product.Price.ToString() + " KM";

                HttpResponseMessage responseRate = ratingsService
                                                   .GetActionResponseTwoParam("GetRatingByProductIdAndStudentId", product.ProductID, Global.loggedStudent.StudentID);
                HttpResponseMessage responseAverageRating = ratingsService.GetActionResponse("GetAverageRating", product.ProductID.ToString());

                HttpResponseMessage responseRecommendedProducts = productsService.GetActionResponse("RecommendedProducts", product.ProductID.ToString());

                if (responseRecommendedProducts.IsSuccessStatusCode)
                {
                    var            recomenndedObjectsJson = responseRecommendedProducts.Content.ReadAsStringAsync();
                    List <Product> recommendedProducts    = JsonConvert.DeserializeObject <List <Product> >(recomenndedObjectsJson.Result);

                    RecommendedProductList.ItemsSource = recommendedProducts;
                }
                else
                {
                    DisplayAlert("Error", "Error code: " + responseRecommendedProducts.StatusCode
                                 + "Message: " + responseRecommendedProducts.ReasonPhrase, "OK");
                }


                if (responseAverageRating.IsSuccessStatusCode)
                {
                    var jsonObjectAverageRating = responseAverageRating.Content.ReadAsStringAsync();
                    product.AverageRating = JsonConvert.DeserializeObject <decimal?>(jsonObjectAverageRating.Result);

                    if (product.AverageRating != null)
                    {
                        RatingLabel.Text = "Average rating: " + product.AverageRating.ToString();
                    }
                }


                if (responseRate.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    isProductRated         = false;
                    YourRatingLabel.Text   = "Your rating: Not rated";
                    RatingLayout.IsVisible = true;
                }
                else if (responseRate.IsSuccessStatusCode)
                {
                    isProductRated = true;
                    var jsonObjectRate = responseRate.Content.ReadAsStringAsync();
                    rate = JsonConvert.DeserializeObject <Rating>(jsonObjectRate.Result);
                    if (rate != null)
                    {
                        YourRatingLabel.Text = "Your rating: " + rate.Rating1;
                    }
                }
                else
                {
                    DisplayAlert("Error", "Error code: " + responseRate.StatusCode
                                 + "Message: " + responseRate.ReasonPhrase, "OK");
                }
            }
        }