public async Task <ActionResult> Edit([Bind(Exclude = "Product.StoreId")] RegisterProductViewModel productVM, List <HttpPostedFileBase> imageFiles)
        {
            if (ModelState.IsValid)
            {
                Product product    = productVM.Product;
                Product oldProduct = ProductRepo.Get(x => x.Id == product.Id);

                if (!string.IsNullOrWhiteSpace(productVM.NewCategory))
                {
                    product.Category = productVM.NewCategory;
                }
                ProcessImages(ref product, imageFiles);


                if (product.Image1 == null)
                {
                    product.Image1     = oldProduct.Image1;
                    product.Image2     = oldProduct.Image2;
                    product.extension1 = oldProduct.extension1;
                    product.extension2 = oldProduct.extension2;
                }

                product.StoreId         = LoggedInUser.StoreId;
                db.Entry(product).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.Categories = await FetchCategories();

            return(View(productVM));
        }
Example #2
0
 public async Task <IActionResult> RegisterProduct(RegisterProductViewModel Product)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest());
     }
     return(Ok());
 }
        public async Task <ActionResult> Create(RegisterProductViewModel productVM, List <HttpPostedFileBase> imageFiles)
        {
            if (ModelState.IsValid)
            {
                Product product = productVM.Product;

                if (!string.IsNullOrWhiteSpace(productVM.NewCategory))
                {
                    product.Category = productVM.NewCategory;
                }
                product.StoreId = LoggedInUser.StoreId;
                ProcessImages(ref product, imageFiles);

                ProductRepo.Add(product);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.Categories = await FetchCategories();

            return(View(productVM));
        }
Example #4
0
        public async Task <IActionResult> Index(RegisterProductViewModel registerModel, IFormFile representativeImage,
                                                IList <string> keys, IList <string> values, IEnumerable <IFormFile> images)
        {
            SellerView seller = loginPersistence.PersistLogin();

            ViewData[GlobalViewBagKeys.ECommerceService] = eCommerce;
            if (ModelState.IsValid)
            {
                ICollection <string> errors = new List <string>();

                //product attributes
                var attributes = new Dictionary <string, HashSet <string> >();
                for (short i = 0; i < keys.Count; i++)
                {
                    if (!string.IsNullOrEmpty(keys[i]) && !attributes.Any(a => a.Key == keys[i]) && values[i] != null)
                    {
                        HashSet <string> separatedValues = values[i]
                                                           .Split(',', StringSplitOptions.RemoveEmptyEntries)
                                                           .ToHashSet();
                        if (separatedValues.Any())
                        {
                            attributes.Add(keys[i], separatedValues);
                        }
                    }
                }
                registerModel.AddModel.Attributes = attributes;

                //product images
                if (images.Any())
                {
                    var imagesList = new List <FileContent>();
                    foreach (IFormFile image in images)
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            await image.CopyToAsync(memoryStream);

                            imagesList.Add(new FileContent(memoryStream.ToArray(), image.ContentType));
                        }
                    }
                    registerModel.AddModel.Images = imagesList;
                }

                //product representative image
                if (representativeImage != null)
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        await representativeImage.CopyToAsync(memoryStream);

                        registerModel.AddModel.RepresentativeImage = new FileContent(memoryStream.ToArray(), representativeImage.ContentType);
                    }
                }
                else
                {
                    errors.Add("Representative image is required");
                }

                if (errors.Any())
                {
                    return(View(registerModel));
                }

                eCommerce.RegisterProduct(int.Parse(seller.Id), registerModel.AddModel, out errors);

                if (errors.Any())
                {
                    ViewData[GlobalViewBagKeys.Errors] = errors;
                }
                else
                {
                    ICollection <string> messages = new List <string>();
                    messages.Add("Register product succeed, please wait for validation");
                    ViewData[GlobalViewBagKeys.Messages] = messages;

                    return(RedirectToAction("Product", "Seller"));
                }
            }
            return(View(registerModel));
        }
Example #5
0
        public async Task <ActionResult> Register_Product(FormCollection frm, RegisterProductViewModel rgp, IEnumerable <HttpPostedFileBase> ProductImage)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    Random       rd          = new Random();
                    int          productcode = rd.Next(99999);
                    int          pcv         = Convert.ToInt32(frm["ProductCategory"]);
                    int          cv          = Convert.ToInt32(frm["Country"]);
                    int          sv          = Convert.ToInt32(frm["State"]);
                    var          userId      = Session["Id"].ToString();
                    ProductsInfo pr          = new ProductsInfo();
                    var          c           = await db.country.FindAsync(cv);

                    var pc = await db.productCategory.FindAsync(pcv);

                    var st = await db.State.FindAsync(sv);

                    pr.ProductCode                = productcode;
                    pr.ProductName                = rgp.ProductName;
                    pr.Manufacturer               = rgp.Manufacturer;
                    pr.ProductCategory            = pc.PCatName;
                    pr.Pharmaceutic_Drug_Category = rgp.drugs_categories.Drug_Category_Id;
                    pr.Product_Summary            = Server.HtmlEncode(sanitizer.Sanitize(rgp.Product_Summary));
                    pr.ProductDescription         = Server.HtmlEncode(sanitizer.Sanitize(rgp.ProductDescription, "", null));
                    pr.price            = Convert.ToDecimal(frm["price"]);
                    pr.Address          = rgp.Address;
                    pr.Country          = c.CountryName;
                    pr.State            = st.StateName;
                    pr.location         = frm["city"];
                    pr.UserID           = userId;
                    pr.ApprovedStatus   = "P";
                    pr.VerifiedStatus   = "NV";
                    pr.NotifyStatus     = 0;
                    pr.regDate          = DateTime.UtcNow;
                    pr.ProductCondition = rgp.productCond.id;
                    if (ProductImage.Count() != 0 || ProductImage.FirstOrDefault() != null)
                    {
                        #region
                        int counter = 0;
                        foreach (HttpPostedFileBase image in ProductImage)
                        {
                            counter++;
                            byte[] img;
                            //image.InputStream.Read(img, 0, (int)image.InputStream.Length);
                            System.Drawing.Image validateWH = System.Drawing.Image.FromStream(image.InputStream, true, true);
                            img = imgR.imageToByteArray(validateWH, validateWH.RawFormat, 305, 400);
                            decimal size = Math.Round(image.ContentLength / (decimal)1024, 2);
                            if (size <= 1000)
                            {
                                if (counter == 1)
                                {
                                    pr.ProductImage = img;
                                }
                                else if (counter == 2)
                                {
                                    pr.ProductImage2 = img;
                                }
                                else if (counter == 3)
                                {
                                    pr.ProductImage3 = img;
                                }
                                else
                                {
                                    pr.ProductImage4 = img;
                                }
                            }
                            else
                            {
                                TempData["error"] = "Image " + counter + "'s size exceeded 1000kb(i.e 1MB) ";
                                return(View(rgp));
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        TempData["error"] = "Please Upload at least one product's image";
                        return(View(rgp));
                    }
                    db.ProductTb.Add(pr);
                    await db.SaveChangesAsync();

                    TempData["success"] = "Product was created successfull!";
                    TempData["info"]    = "We will Notify you upon approver, Thanks.";
                    ViewBag.Alert       = "Add More Products";

                    return(Redirect(Request.Url.ToString()));
                }
                ModelState.AddModelError("", "Fill All Required field!");
                return(View(rgp));
            }
            catch
            {
                TempData["error"] = "Error occurred";
                return(View(rgp));
            }
        }