Exemplo n.º 1
0
        public async Task <IActionResult> DeleteConfirmed(int id)
        {
            ProductsRatings productsRatings = await _colibriDbContext.ProductsRatings.FindAsync(id);

            if (productsRatings == null)
            {
                return(NotFound());
            }
            else
            {
                // remove the Entry from the DB
                _colibriDbContext.ProductsRatings.Remove(productsRatings);

                // save the Changes asynchronously
                await _colibriDbContext.SaveChangesAsync();

                // avoid Refreshing the POST Operation -> Redirect
                return(RedirectToAction(nameof(Index)));
            }
        }
Exemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public async Task <IActionResult> RateAdvertisementPost(int id, string command)
        {
            // Check the State Model Binding
            if (ModelState.IsValid)
            {
                // Security Claims
                System.Security.Claims.ClaimsPrincipal currentUser = this.User;

                // Claims Identity
                var claimsIdentity = (ClaimsIdentity)this.User.Identity;
                var claim          = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);

                // to overwrite a Rating, first get the old One
                // get the Product from the DB
                var productFromDb = await _colibriDbContext.Products
                                    .Where(m => m.Id == id)
                                    .FirstOrDefaultAsync();

                // another Table ProductRatings for the Description
                var productRatingFromDb = await _colibriDbContext.ProductsRatings
                                          .Where(p => p.ProductId == id)
                                          .FirstOrDefaultAsync();

                // current User
                var currentUserId = claim.Value;
                //bool userAlreadyRated = false;

                if (productRatingFromDb != null)
                {
                    // check, if already rated
                    if (productRatingFromDb.ApplicationUserId == currentUserId)
                    {
                        // already rated!
                        //userAlreadyRated = true;

                        TempData["msg"]               = "<script>alert('Already rated!');</script>";
                        TempData["returnButton"]      = "<div><p><b>Already rated!</b></p></div>";
                        TempData["returnBackButton"]  = "return";
                        TempData["showProductRating"] = "showProductRating";
                        TempData["productId"]         = productFromDb.Id;

                        ViewData["BackToList"]     = _localizer["BackToListText"];
                        ViewData["ShowAllRatings"] = _localizer["ShowAllRatingsText"];
                        ViewData["ShowRating"]     = _localizer["ShowRatingText"];

                        return(View());
                    }
                    else
                    {
                        int tempProductRating = 0;

                        if (command.Equals("1"))
                        {
                            tempProductRating = 1;
                        }
                        else if (command.Equals("2"))
                        {
                            tempProductRating = 2;
                        }
                        else if (command.Equals("3"))
                        {
                            tempProductRating = 3;
                        }
                        else if (command.Equals("4"))
                        {
                            tempProductRating = 4;
                        }
                        else if (command.Equals("5"))
                        {
                            tempProductRating = 5;
                        }

                        // go to the Product Table
                        // calculate the new ProductRating
                        if (productFromDb.NumberOfProductRates == 0)
                        {
                            productFromDb.ProductRating = tempProductRating;
                        }
                        else
                        {
                            productFromDb.ProductRating = Math.Round((productFromDb.ProductRating * productFromDb.NumberOfProductRates + tempProductRating) / (productFromDb.NumberOfProductRates + 1), 2);
                        }

                        // Rating Create
                        ProductsRatings productsRatings = new ProductsRatings()
                        {
                            ProductId   = productFromDb.Id,
                            ProductName = productFromDb.Name,
                            //ApplicationUserId = productFromDb.ApplicationUserId,
                            // add the current User as the Creator of the Rating
                            ApplicationUserId   = claim.Value,
                            ApplicationUserName = claim.Subject.Name,
                            ProductRating       = tempProductRating,
                            CreatedOn           = System.DateTime.Now
                        };

                        // update the ProductsRatings Entity
                        _colibriDbContext.ProductsRatings.Add(productsRatings);

                        // increment the Number of Product Rates of the Product
                        productFromDb.NumberOfProductRates += 1;

                        // save the Changes in DB
                        await _colibriDbContext.SaveChangesAsync();
                    }

                    return(View(ProductsViewModel));
                }

                //else if (productRatingFromDb == null && !userAlreadyRated)
                else
                {
                    int tempProductRating = 0;

                    if (command.Equals("1"))
                    {
                        tempProductRating = 1;
                    }
                    else if (command.Equals("2"))
                    {
                        tempProductRating = 2;
                    }
                    else if (command.Equals("3"))
                    {
                        tempProductRating = 3;
                    }
                    else if (command.Equals("4"))
                    {
                        tempProductRating = 4;
                    }
                    else if (command.Equals("5"))
                    {
                        tempProductRating = 5;
                    }

                    // go to the Product Table
                    // calculate the new ProductRating
                    if (productFromDb.NumberOfProductRates == 0)
                    {
                        productFromDb.ProductRating = tempProductRating;
                    }
                    else
                    {
                        productFromDb.ProductRating = Math.Round((productFromDb.ProductRating * productFromDb.NumberOfProductRates + tempProductRating) / (productFromDb.NumberOfProductRates + 1), 2);
                    }

                    // Rating Create
                    ProductsRatings productsRatings = new ProductsRatings()
                    {
                        ProductId   = productFromDb.Id,
                        ProductName = productFromDb.Name,
                        //ApplicationUserId = productFromDb.ApplicationUserId,
                        // add the current User as the Creator of the Rating
                        ApplicationUserId   = claim.Value,
                        ApplicationUserName = claim.Subject.Name,
                        ProductRating       = tempProductRating,
                        CreatedOn           = System.DateTime.Now
                    };

                    // update the ProductsRatings Entity
                    _colibriDbContext.ProductsRatings.Add(productsRatings);

                    // increment the Number of Product Rates of the Product
                    productFromDb.NumberOfProductRates += 1;

                    // save the Changes in DB
                    await _colibriDbContext.SaveChangesAsync();
                }

                return(RedirectToAction(nameof(Details)));
            }
            else
            {
                // one can simply return to the Form View again for Correction
                return(View(ProductsViewModel));
            }
        }