public async Task<bool> SaveProductAsync(Product product)
        {
            IMongoCollection<Product> collection = db.GetCollection<Product>("product");

            if (product.Id == ObjectId.Empty)
            {
                await collection.InsertOneAsync(product);
            }
            else
            {
                var filter = new BsonDocument("_id", product.Id);

                var dbEntry = await db.GetCollection<Product>("product").Find(filter).FirstOrDefaultAsync();

                if (dbEntry != null)
                {
                    dbEntry.Name = product.Name;
                    dbEntry.Description = product.Description;
                    dbEntry.Price = product.Price;
                    dbEntry.Category = product.Category;
                    dbEntry.ImageDataBig = product.ImageDataBig;
                    dbEntry.ImageDataSmall = product.ImageDataSmall;
                    dbEntry.ImageMimeType = product.ImageMimeType;

                    await collection.ReplaceOneAsync(filter, dbEntry);
                }
                else
                    return false;
                
            }

            return true;
        }
Example #2
0
 public void AddItem(Product product, int quantity)
 {
     CartLine line = lineCollection.Where(p => p.Product.ProductID == product.ProductID).FirstOrDefault();
     if (line == null)
         lineCollection.Add(new CartLine { Product = product, Quantity = quantity });
     else
         line.Quantity++;
 }
Example #3
0
 public ActionResult AddNewProduct(Product model)
 {
     if (ModelState.IsValid)
     {
         repository.SaveProduct(model);
         return RedirectToAction("Products", new { app_culture = Request.RequestContext.RouteData.Values["app_culture"] });
     }
     else
     {
         ViewBag.Categories = repository.Categories;
         ViewBag.Suppliers = repository.Suppliers;
         return View("EditProduct", model);
     }
 }
Example #4
0
 public void AddItem(Product product, int quantity)
 {
     CartLine line = lineCollection.Where(p => p.Product.IdString == product.IdString).FirstOrDefault();
     if (line == null)
     {
         lineCollection.Add
         (
             new CartLine
             {
                 Product = product,
                 Quantity = quantity
             }
          );
     }
     else
     {
         line.Quantity += quantity;
     }
 }
Example #5
0
        public async Task<ActionResult> Edit(Product product, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    product.ImageDataBig = new byte[image.ContentLength];
                    image.InputStream.Read(product.ImageDataBig, 0, image.ContentLength);

                    using (var img = Image.FromStream(image.InputStream, true, true)) /* Creates Image from specified data stream */
                    {
                        using (var thumb = img.GetThumbnailImage(
                             75, /* width*/
                             75, /* height*/
                             () => false,
                             IntPtr.Zero))
                        {
                            var imgInfo = ImageCodecInfo.GetImageEncoders().Where(codecInfo => codecInfo.MimeType == product.ImageMimeType).First(); /* Returns array of image encoder objects built into GDI+ */
                            using (var encParams = new EncoderParameters(1))
                            {
                                long quality = 100;
                                encParams.Param[0] = new EncoderParameter(Encoder.Quality, quality);

                                var temp = new MemoryStream();
                                thumb.Save(temp, imgInfo, encParams);
   
                                product.ImageDataSmall = temp.ToArray();
                            }
                        }
                    }
                }

                await repository.SaveProductAsync(product);
                TempData["message"] = string.Format("{0} has been saved", product.Name);
                return RedirectToAction("Index");
            }
            else
            {
                // there is something wrong with the data values
                return View(product);
            }
        }
 public void SaveProduct(Product product)
 {
     if (product.ProductID == 0)
     {
         context.Products.Add(product);
     }
     else
     {
         Product dbEntry = context.Products.Find(product.ProductID);
         if (dbEntry != null)
         {
             dbEntry.Name = product.Name;
             dbEntry.Description = product.Description;
             dbEntry.Price = product.Price;
             dbEntry.Category = product.Category;
             dbEntry.ImageData = product.ImageData;
             dbEntry.ImageMimeType = product.ImageMimeType;
         }
     }
     context.SaveChanges();
 }
        public ActionResult Edit(Product product,HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    product.ImageData = new byte[image.ContentLength];
                    image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }
                repository.SaveProduct(product);
                TempData["message"] = string.Format("Chenge in product \"{0}\" was saved", product.Name);
                return RedirectToAction("Index");

            }
            else
            {
                // something is wrong with the data value
                return View(product);
            }
        }
Example #8
0
 public void CreateProduct(Product prod)
 {
     context.Products.Add(prod);
     context.SaveChanges();
 }
Example #9
0
 public void RemoveLine(Product product)
 {
     lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
 }
Example #10
0
 public void RemoveLine(Product product)
 {
     lineCollection.RemoveAll(l => l.Product.IdString == product.IdString);
 }
Example #11
0
        public ActionResult EditProduct(Product model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    HttpPostedFileBase file = Request.Files[0];
                    byte[] imageSize = new byte[file.ContentLength];
                    file.InputStream.Read(imageSize, 0, (int)file.ContentLength);
                    model.Image = imageSize;
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("Image", AdminStrings.image_error);
                }
                repository.SaveProduct(model);
                return RedirectToAction("Products", new { app_culture = Request.RequestContext.RouteData.Values["app_culture"] });
            }
            else
            {

                ViewBag.Categories = repository.Categories;
                ViewBag.Suppliers = repository.Suppliers;
                return View(model);
            }
        }
Example #12
0
 public ActionResult Create(Product prod)
 {
     repository.CreateProduct(prod);
     return View();
 }
Example #13
0
 private void MarkProductForSearcher(Product product)
 {
     if (IndexWriter.IsLocked(directory))
     {
         IndexWriter.Unlock(directory);
     }
     using (var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED))
     {
         writer.AddDocument(product.GetDocument());
         writer.Optimize();
         writer.Close();
     }
 }
Example #14
0
 public void SaveProduct(Product product)
 {
     if (product.ProductID == 0)
         context.Products.Add(product);
     else
     {
         Product product_ = context.Products.Single(p => p.ProductID == product.ProductID);
         context.Entry(product_).CurrentValues.SetValues(product);
     }
     context.SaveChanges();
     MarkProductForSearcher(product);
     Logger.Info(string.Format("Product with ID = {0} and name {1} in database must be inserted or updated in database", product.ProductID, product.ProductName));
 }
Example #15
0
 public void DeleteProduct(Product product)
 {
     context.Products.Remove(product);
     context.SaveChanges();
     MarkProductForSearcher(product);
     Logger.Info(String.Format("Product with id = {0} and name {0} has been removed from database", product.ProductID, product.ProductName));
 }