Пример #1
0
        public async Task <IActionResult> Edit(Guid id, [Bind("ProductId,Name,CreatedDate,ModifiedDate,Quantity,Available,Price,Image,Description,MeasuringUnitId")] Product product)
        {
            if (id != product.ProductId)
            {
                return(NotFound());
            }


            if (Request.Form.Files["Image"] != null)
            {
                IFormFile    file    = Request.Form.Files["Image"];
                var          stream  = file.OpenReadStream();
                BinaryReader br      = new BinaryReader(stream);
                byte[]       imgdata = br.ReadBytes((int)stream.Length);
                product.Image = imgdata;
            }
            else
            {
                var image = _context.Product.AsNoTracking().FirstOrDefault(x => x.ProductId == product.ProductId).Image;
                product.Image = image;
                _context.Entry(product).State = EntityState.Detached;
            }


            if (ModelState.IsValid)
            {
                try
                {
                    _context.Entry(product).State = EntityState.Modified;
                    // _context.Update(product);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProductId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["MeasuringUnitId"] = new SelectList(_context.MeasuringUnit, "MeasuringUnitId", "Unit", product.MeasuringUnitId);
            return(View(product));
        }
Пример #2
0
        public async Task <IActionResult> PutReviewContent([FromRoute] Guid id, [FromBody] ReviewContent reviewContent)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != reviewContent.ReviewContentId)
            {
                return(BadRequest());
            }

            _context.Entry(reviewContent).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ReviewContentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> PutProductCategory([FromRoute] Guid id, [FromBody] ProductCategory productCategory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != productCategory.ProductCategoryId)
            {
                return(BadRequest());
            }

            _context.Entry(productCategory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductCategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #4
0
        public async Task <IActionResult> AddToCart(Guid id, int q)
        {
            if (id == null)
            {
                return(NotFound());
            }
            else if (q == 0)
            {
                TempData["Message"] = "Quantity products is 0, Please insert quantity products into your cart";
                return(Json(Url.Action("Details", "WebShop", new { id = id.ToString() })));
            }


            Cart  cart = null;
            Int32?numberElementInCart = 0;

            try
            {
                Product product = await _context.Product.FirstOrDefaultAsync(x => x.ProductId == id);

                if (HttpContext.Session.GetInt32("count") != null)
                {
                    numberElementInCart = HttpContext.Session.GetInt32("count");
                }


                if (product != null)
                {
                    if (product.Quantity > 0 && product.Available)
                    {
                        //nova kartica
                        if (HttpContext.Session.GetObject <List <Cart> >("cartShop") != null)
                        {
                            carts = HttpContext.Session.GetObject <List <Cart> >("cartShop");

                            if (carts.Where(x => x.Product.ProductId == product.ProductId).Any())
                            {
                                cart = carts.Where(x => x.Product.ProductId == product.ProductId).FirstOrDefault();
                                carts.Remove(cart);
                                cart.Quantity    = cart.Quantity + q;
                                product.Quantity = product.Quantity - q;
                                carts.Add(cart);
                            }
                            else
                            {
                                cart                = new Cart();
                                cart.Product        = product;
                                cart.Quantity       = cart.Quantity + q;
                                product.Quantity    = product.Quantity - q;
                                numberElementInCart = numberElementInCart + 1;
                                carts.Add(cart);
                            }
                        }
                        else
                        {
                            cart                = new Cart();
                            cart.Product        = product;
                            cart.Quantity       = cart.Quantity + q;
                            product.Quantity    = product.Quantity - q;
                            numberElementInCart = numberElementInCart + 1;
                            carts.Add(cart);
                        }


                        if (product.Quantity <= 0)
                        {
                            product.Available = false;
                        }
                        _context.Entry(product).State = EntityState.Modified;
                        await _context.SaveChangesAsync();

                        TempData["Message"] = "Product " + product.Name + " add to cart!";
                    }
                    else
                    {
                        TempData["Message"] = "Product " + product.Name + " not available!";
                        return(Json(Url.Action("Index", "WebShop")));
                    }
                }
                else
                {
                    return(NotFound());
                }
            }
            catch (Exception ex)
            {
                TempData["Message"] = ex.Message;
            }

            //vračam košaricu u session
            HttpContext.Session.SetInt32("count", numberElementInCart.Value);
            //nova kartica
            HttpContext.Session.SetObject("cartShop", carts);
            return(Json(Url.Action("Index", "WebShop")));
        }