private void DeleteGetAndAddReviews(int productId, string pageContent, DatabaseRpoundForestEntities db) { //remove db.Reviews.RemoveRange(db.Reviews.Where(x => x.ProductId == productId)); db.SaveChanges(); //getcustomerReviews int customerReviewsIndex = pageContent.IndexOf("\"customerReviews\"") + 18; int customerReviewsEndIndex = pageContent.IndexOf("\"selected\"", customerReviewsIndex) - 3; string reviewList = pageContent.Substring(customerReviewsIndex, customerReviewsEndIndex - customerReviewsIndex); var reviewList2 = JsonConvert.DeserializeObject <List <ReviewClass> >(reviewList); //List<ReviewShow> reviews = new List<ReviewShow>(); foreach (ReviewClass rc in reviewList2) { var thisReview = new Review { ProductId = productId, ReviewTitle = rc.reviewTitle, ReviewText = rc.reviewText, Stars = Convert.ToInt32(rc.rating) }; db.Reviews.Add(thisReview); //reviews.Add(new ReviewShow { rating = rc.rating, reviewText = rc.reviewText, reviewTitle = rc.reviewTitle } ); } db.SaveChanges(); }
private Product GetAndAddProduct(string pageContent, string productNumber, DatabaseRpoundForestEntities db) { int selectedProductIdIndex = pageContent.IndexOf("\"selectedProductId\""); int productTitleIndex = pageContent.IndexOf("\"title\"", selectedProductIdIndex) + 9; int productTitleEndIndex = pageContent.IndexOf("\"brand\"", selectedProductIdIndex) - 2; string productTitle = pageContent.Substring(productTitleIndex, productTitleEndIndex - productTitleIndex); Product newProduct = new Product { SellerInternalId = productNumber, ProductName = productTitle, Price = 22.33M }; db.Products.Add(newProduct); //and whatever the price is. db.SaveChanges(); return(newProduct); }
protected void Button1_Click(object sender, EventArgs e) { using (DatabaseRpoundForestEntities db = new DatabaseRpoundForestEntities()) { int productId = Convert.ToInt32(Session["productid"]); reviews = db.Reviews .Where((x) => x.ProductId == productId && x.ReviewText.Contains(TextBoxSeek.Text)) .Select((x) => new ReviewShow { reviewTitle = x.ReviewTitle, reviewText = x.ReviewText, rating = x.Stars.ToString() }) .ToList <ReviewShow>(); GridView1.DataSource = reviews; GridView1.DataBind(); } }
public async Task <string> MineProduct(string url) { string pageContent = await HttpTools.DownloadPage(url); //get wallmart number int productNumberIndex = pageContent.IndexOf("\"walmartItemNumber\"") + 22; int productNumberEndIndex = pageContent.IndexOf("\"brand\"", productNumberIndex) - 3; string productNumber = pageContent.Substring(productNumberIndex, productNumberEndIndex - productNumberIndex); using (DatabaseRpoundForestEntities db = new DatabaseRpoundForestEntities()) { Product product = db.Products.Where((x) => x.SellerInternalId == productNumber).FirstOrDefault(); if (product == null) //add it { product = GetAndAddProduct(pageContent, productNumber, db); } //delete all reviews and re-fill DeleteGetAndAddReviews(product.Id, pageContent, db); } return(productNumber); }
protected async void ButtonDownloadPage_Click(object sender, EventArgs e) { IMineProduct mp = new MineWallMart(); //some infrastucture for dependency injection in the future to support other suppliers string productNumber = await mp.MineProduct(TextBoxProductUrl.Text); Product product = null; List <ReviewShow> myReviews; using (DatabaseRpoundForestEntities db = new DatabaseRpoundForestEntities()) { product = db.Products.Where((x) => x.SellerInternalId == productNumber).FirstOrDefault(); myReviews = db.Reviews .Where((x) => x.ProductId == product.Id) .Select((x) => new ReviewShow { reviewTitle = x.ReviewTitle, reviewText = x.ReviewText, rating = x.Stars.ToString() }) .ToList <ReviewShow>(); } Session["productid"] = product.Id; LabelProductInfo.Text = String.Format("Product Seller Number: {0} Product name: {1}", product.SellerInternalId, product.ProductName); GridView1.DataSource = myReviews; GridView1.DataBind(); }