Exemple #1
0
        /// <summary>
        /// Add the images.
        /// </summary>
        /// <param name="image">image object.</param>
        /// <returns>status add images</returns>
        public bool Add_Images(ImageProducts image)
        {
            using (var data = new themanorContext())
            {
                bool rt;
                try
                {
                    data.ImageProducts.Add(image);
                    data.SaveChanges();
                    rt = true;
                }
                catch (Exception)
                {
                    rt = false;
                }

                return(rt);
            }
        }
        public JsonResult GetQuickView(int id)
        {
            ImageProducts image  = new ImageProducts();
            var           result = (from p in db.Products
                                    join c in db.Categories
                                    on p.CategoryID equals c.CategoryID
                                    where p.ProductID == id && p.Status == true
                                    select new ProductModel
            {
                ProductID = p.ProductID,
                ProductName = p.ProductName,
                Price = p.Price,
                PriceSale = p.PriceSale,
                Alias = p.Alias,
                Images = p.Images,
                Quanlity = p.Quanlity,
                CategoryID = p.CategoryID,
                Status = p.Status,
                DescriptShort = p.DescriptShort,
                CategoryName = c.CategoryName,
            }).ToList();

            image.Product = result;
            var image_product = (from i in db.ImageProducts
                                 join p in db.Products
                                 on i.ProductID equals p.ProductID
                                 where p.ProductID == id && p.Status == true
                                 select new ImageMores
            {
                FileImages = i.FileImages
            }).ToList();

            image.Image = image_product;
            return(new JsonResult()
            {
                Data = image, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Exemple #3
0
        public async Task <IActionResult> InseretProduct([FromForm] ProductRespont pd)
        {
            if (pd == null)
            {
                return(Ok("Error"));
            }

            try
            {
                var i       = pd.imagerq.GetFilename().Split(".");
                var nameimg = RandomString(10) + "." + i[1];
                var fpath   = Path.Combine(
                    Directory.GetCurrentDirectory(), "wwwroot/images",
                    nameimg);        //post image to forder
                using (var stream = new FileStream(fpath, FileMode.Create))
                {
                    await pd.imagerq.CopyToAsync(stream);
                }
                var prod = new Products();
                prod.prname      = pd.prname;
                prod.total       = pd.total;
                prod.lineprid    = pd.lineprid;
                prod.importprice = pd.importprice;
                prod.price       = pd.price;
                prod.oldprice    = pd.oldprice;
                prod.image       = nameimg;
                prod.highlight   = pd.highlight;
                prod.status      = 0;
                DateTime mnday = DateTime.ParseExact(pd.mnday, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                prod.mnday = mnday;
                DateTime expirydate = DateTime.ParseExact(pd.expirydate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                prod.expirydate = expirydate;

                DateTime createday = DateTime.ParseExact(pd.createday, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                prod.createday = createday;

                _iproductResponsitory.SaveProduct(prod);

                if (pd.files != null || pd.files.Count > 0)
                {
                    foreach (var file in pd.files)
                    {
                        var x         = file.GetFilename().Split(".");
                        var nameimage = RandomString(10) + "." + x[1];
                        var path      = Path.Combine(
                            Directory.GetCurrentDirectory(), "wwwroot/images",
                            nameimage);
                        var img = new ImageProducts();
                        img.image = nameimage;
                        img.prid  = prod.prid;
                        _iimageProductResponsitory.SaveImg(img);
                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await file.CopyToAsync(stream);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(Ok(new { data = "error" }));
            }
            return(Ok(new { data = "success" }));
        }
Exemple #4
0
        public async Task <IActionResult> UpdateProduct([FromForm] ProductRpUpdate pd)
        {
            if (pd == null)
            {
                return(Ok("Files not found"));
            }
            try
            {
                //var pf = _iproductResponsitory.GetProductById(pd.prid);
                Products newitem = new Products();
                newitem.prid        = pd.prid;
                newitem.prname      = pd.prname;
                newitem.total       = pd.total;
                newitem.importprice = pd.importprice;
                newitem.price       = pd.price;
                newitem.lineprid    = pd.lineprid;
                newitem.totalview   = pd.totalview;
                newitem.totallike   = pd.totallike;
                newitem.highlight   = pd.highlight;
                newitem.oldprice    = pd.oldprice;
                newitem.status      = 0;
                DateTime mnday = DateTime.ParseExact(pd.mnday, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                newitem.mnday = mnday;
                DateTime expirydate = DateTime.ParseExact(pd.expirydate, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                newitem.expirydate = expirydate;
                DateTime createday = DateTime.ParseExact(pd.createday, "dd/MM/yyyy", CultureInfo.InvariantCulture);
                newitem.createday = createday;

                newitem.image = pd.image;
                if (pd.imagerq != null)
                {
                    //delete old picture
                    string webRootPath     = _hostingEnvironment.WebRootPath;
                    string contentRootPath = _hostingEnvironment.ContentRootPath;
                    var    file            = System.IO.Path.Combine(webRootPath, "images/" + pd.image);
                    System.IO.File.Delete(file);//delete in forder


                    var temp        = pd.imagerq.GetFilename().Split(".");
                    var nameimgmain = RandomString(10) + "." + temp[1];
                    var fpath       = Path.Combine(
                        Directory.GetCurrentDirectory(), "wwwroot/images",
                        nameimgmain);        //post image to forder
                    newitem.image = nameimgmain;

                    using (var stream = new FileStream(fpath, FileMode.Create))
                    {
                        await pd.imagerq.CopyToAsync(stream);
                    }
                }
                _iproductResponsitory.UpdateProduct(newitem);
                IEnumerable <ImageProducts> listimg = _iimageProductResponsitory.GetAllImgByPrid(pd.prid);
                if (pd.filesolds != null)
                {
                    foreach (var imgrp in pd.filesolds)
                    {
                        FileUpdate fileul = new FileUpdate(imgrp);
                        if (!fileul.check)
                        {
                            string webRootPath     = _hostingEnvironment.WebRootPath;
                            string contentRootPath = _hostingEnvironment.ContentRootPath;
                            var    file            = System.IO.Path.Combine(webRootPath, "images/" + fileul.image);
                            System.IO.File.Delete(file);                        //delete in forder
                            _iimageProductResponsitory.DeleteImg(fileul.imgid); //delete in database
                        }
                    }
                }
                if (pd.files.Count() > 0)
                {
                    foreach (var file in pd.files)
                    {
                        var x         = file.GetFilename().Split(".");
                        var nameimage = RandomString(10) + "." + x[1];
                        var path      = Path.Combine(
                            Directory.GetCurrentDirectory(), "wwwroot/images",
                            nameimage);
                        var img = new ImageProducts();
                        img.image = nameimage;
                        img.prid  = pd.prid;
                        _iimageProductResponsitory.SaveImg(img);
                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await file.CopyToAsync(stream);
                        }
                    }
                }
                return(Ok(new { data = "success" }));
            }
            catch (Exception e)
            {
                return(Ok(new { data = "error" }));
            }
        }