Esempio n. 1
0
        public async Task <ActionResult> Add()
        {
            var model = new ProductAppIG4Item();

            UpdateModel(model);

            List <Gallery_Picture> images = new List <Gallery_Picture>();

            for (int i = 0; i < Request.Files.Count; i++)
            {
                var img = await UploadImage(i);

                if (img.Code != 200)
                {
                    return(Json(img));
                }
                var picture = new Gallery_Picture
                {
                    Type        = !string.IsNullOrEmpty(Request["type"]) ? Convert.ToInt32(Request["type"]) : 0,
                    CategoryID  = model.CateId,
                    Folder      = img.Data.Folder,
                    Name        = img.Data.Name,
                    DateCreated = DateTime.Now.TotalSeconds(),
                    IsShow      = true,
                    Url         = img.Data.Url,
                    IsDeleted   = false,
                };
                images.Add(picture);
                _da.Add(picture);
            }
            if (images.Count == 0)
            {
                return(Json(new JsonMessage(1000, "Ảnh sản phẩn không được để trống.")));
            }
            await _da.SaveAsync();

            //lay address
            var address = customerAddressDA.GetById(model.AddressId, CustomerId);

            if (address == null)
            {
                return(Json(new JsonMessage(1000, "Địa chỉ không tồn tại.")));
            }
            var product = new Shop_Product
            {
                Name        = model.Name,
                NameAscii   = FDIUtils.Slug(FDIUtils.NewUnicodeToAscii(model.Name)),
                Quantity    = model.Quantity,
                Description = model.Description,
                PriceNew    = model.PriceNew,
                PictureID   = images[0].ID,
                CustomerID  = CustomerId,
                freeShipFor = model.freeShipFor,
                HasTransfer = model.HasTransfer,
                Type        = model.Type,
                IsShow      = true,
                IsDelete    = false,
                AddressId   = address.ID,
                Latitude    = address.Latitude.Value,
                CategoryId  = model.CateId.Value,
                Longitude   = address.Longitude.Value,
                DateCreated = DateTime.Now.TotalSeconds(),
                CustomerID1 = model.CustomerId1,
                //Gallery_Picture1 = _productDa.GetListPictureByArrId(images.Select(m => m.ID).ToList()),
            };

            //product.Categories = _productDa.GetListCategoryByArrId(new List<int>() { model.CateId.Value });

            _productDa.Add(product);
            await _productDa.SaveAsync();

            return(Json(new JsonMessage(200, ""), JsonRequestBehavior.AllowGet));
        }
Esempio n. 2
0
        public async Task <ActionResult> Update()
        {
            var model = new ProductAppIG4Item();

            UpdateModel(model);
            var product = _productDa.GetById(model.ID);

            if (product.CustomerID != CustomerId)
            {
                return(Json(new JsonMessage(4001, "Bạn không có quyển xóa sản phẩm này")));
            }
            var files = Request.Files;

            if (files.Count == 0 && model.LstPictures != null && model.LstPictures.Count() >= product.Shop_Product_Picture.Count())
            {
                return(Json(new JsonMessage(1000, "Ảnh sản phẩn không được để trống.")));
            }

            var date       = DateTime.Now;
            var folder     = date.Year + "\\" + date.Month + "\\" + date.Day + "\\";
            var fileinsert = date.Year + "/" + date.Month + "/" + date.Day + "/";

            var urlFolder = ConfigData.OriginalFolder;

            ImageProcess.CreateForder(ConfigData.OriginalFolder);
            ImageProcess.CreateForder(ConfigData.ThumbsFolder);
            ImageProcess.CreateForder(ConfigData.MediumsFolder);

            List <Gallery_Picture> images = new List <Gallery_Picture>();

            for (int i = 0; i < Request.Files.Count; i++)
            {
                var img = await UploadImage(i);

                if (img.Code != 200)
                {
                    return(Json(img));
                }

                var picture = new Gallery_Picture
                {
                    Type       = !string.IsNullOrEmpty(Request["type"]) ? Convert.ToInt32(Request["type"]) : 0,
                    CategoryID =
                        model.CateId,
                    Folder      = img.Data.Folder,
                    Name        = img.Data.Name,
                    DateCreated = DateTime.Now.TotalSeconds(),
                    IsShow      = true,
                    Url         = img.Data.Url,
                    IsDeleted   = false,
                };
                images.Add(picture);
                _da.Add(picture);
            }

            await _da.SaveAsync();

            if (model.AddressId != product.AddressId)
            {
                //lay address
                var address = customerAddressDA.GetById(model.AddressId, CustomerId);
                if (address == null)
                {
                    return(Json(new JsonMessage(1000, "Địa chỉ không tồn tại.")));
                }
                product.AddressId = model.AddressId;
                product.Latitude  = address.Latitude.Value;
                product.Longitude = address.Longitude.Value;
            }
            product.Name        = model.Name;
            product.Quantity    = model.Quantity;
            product.Description = model.Description;
            product.PriceNew    = model.PriceNew;
            product.CustomerID  = CustomerId;
            product.HasTransfer = model.HasTransfer;
            product.Type        = model.Type;
            product.CategoryId  = model.CateId.Value;
            product.CustomerID1 = model.CustomerId1;

            var idImages = product.Shop_Product_Picture.Where(m => model.PictureDeleteIds == null || !model.PictureDeleteIds.Any(n => n == m.Gallery_Picture.ID)).Select(m => m.Gallery_Picture.ID).ToList();

            idImages.AddRange(images.Select(m => m.ID).ToList());
            //product.Shop_Product_Picture = _productDa.GetListPictureByArrId(idImages);

            //if (!product.Categories.Any(m => m.Id == model.CateId))
            //{
            //    product.Categories.Clear();
            //    product.Categories = _productDa.GetListCategoryByArrId(new List<int>() { model.CateId.Value });
            //}

            await _productDa.SaveAsync();

            return(Json(new JsonMessage(200, ""), JsonRequestBehavior.AllowGet));
        }