Example #1
0
        public async Task <string> Load()
        {
            try
            {
                var fileContent = await File.ReadAllTextAsync(Environment.CurrentDirectory + @"\transformed.txt");

                List <Review> reviews = JsonConvert.DeserializeObject <List <Review> >(fileContent);

                foreach (var review in reviews)
                {
                    var exists = await _reviewsDbContext.Reviews.AnyAsync(x => x.ProductRating.Equals(review.ProductRating) && x.ReviewDate.Equals(review.ReviewDate) &&
                                                                          x.ReviewText.Equals(review.ReviewText) && x.ReviewTitle.Equals(review.ReviewTitle) && x.ReviewerName.Equals(review.ReviewerName));

                    if (exists)
                    {
                        continue;
                    }

                    await _reviewsDbContext.Reviews.AddAsync(review);

                    await _reviewsDbContext.SaveChangesAsync();
                }

                //File.Delete(Environment.CurrentDirectory + @"\transformed.txt");
                //File.Delete(Environment.CurrentDirectory + @"\webpage.txt");
                return($"Successfully loaded {reviews.Count} reviews");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return("Error occured during loading the reviews.");
            }
        }
        public async Task <ActionResult> Create([Bind(Include = "ID,Name,RatingDescription,Rating,Author,Date")] Review review)
        {
            if (ModelState.IsValid)
            {
                review.Date   = DateTime.Now;
                review.Author = User.Identity.Name;
                _reviews.Reviews.Add(review);
                await _reviews.SaveChangesAsync();

                return(RedirectToAction("Reviews", "Feedback"));
            }
            return(View(review));
        }