示例#1
0
 public BaseResponse SaveImage(ProductImage productImage)
 {
     return Execute(_repository, r =>
     {
         var res = r.SaveImage(productImage);
         _cacheHelper.ClearGetProducts();
         return res;
     });
 }
示例#2
0
        public ActionResult SaveImage(Guid productId, bool? visible, string textSearch)
        {
            var imagePath = string.Empty;

            #region save image

            if (Request.Files["fileImage"] != null && !string.IsNullOrWhiteSpace(Request.Files["fileImage"].FileName))
            {
                var file = Request.Files["fileImage"];

                var extension = Path.GetExtension(file.FileName).ToStr();

                if (!SiteUtils.IsImageFile(file.FileName))
                {
                    return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                }

                if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                {
                    return JsonObject(false, BackendMessage.FileMaxSize5MB);
                }

                imagePath = Guid.NewGuid() + extension;

                var filePath = PhysicalDataFilePath(imagePath);
                file.SaveAs(filePath);
            }

            #endregion

            var productImage = new ProductImage
            {
                ImagePath = imagePath,
                ProductId = productId,
                CreatedDate = DateTime.UtcNow,
                Visible = (visible==null)?false:true
            };

            productImage.InitId();

            var response = ServiceHelper.Product.ExecuteDispose(s => s.SaveImage(productImage));

            if (response.Success)
            {
                var product = ServiceHelper.Product.ExecuteDispose(s => s.GetProduct(productId));
                if (product == null)
                {
                    return JsonObject(false, BackendMessage.CannotLoadData);
                }

                var indexModel = new ProductIndexModel
                {
                    TextSearch = textSearch
                };

                PopulateIndexModel(indexModel);
                return JsonObject(true, BackendMessage.SaveDataSuccess, new
                {
                    productList = PartialViewToString("_list", indexModel),
                    imageList = PartialViewToString("ImageList/_productImageList", product.Map<Product, ProductModel>())
                });
            }

            return JsonObject(false, response.Messages.FirstOrDefault().GetServiceMessageRes());
        }
示例#3
0
        public ActionResult SaveProduct(ProductModel model)
        {
            if (model.IsNew && !CanAdd)
            {
                return GetAddDeniedResult();
            }

            if (!model.IsNew && !CanUpdate)
            {
                return GetUpdateDeniedResult();
            }

            if (!ModelState.IsValid && GetModelStateErrorList().Any())
            {
                return JsonObject(false, GetModelStateErrors());
            }

            #region save list image

            var listImagePath = new List<string>();

            for (var i = 0; i < Request.Files.Count; ++i)
            {
                var file = Request.Files[i];
                if (file != null &&
                    !string.IsNullOrWhiteSpace(file.FileName))
                {
                    var extension = Path.GetExtension(file.FileName).ToStr();

                    if (!SiteUtils.IsImageFile(file.FileName))
                    {
                        return JsonObject(false, BackendMessage.FileTypeIsInvalid);
                    }

                    if (!SiteUtils.ImageSizeIsValid(file.ContentLength))
                    {
                        return JsonObject(false, BackendMessage.FileMaxSize5MB);
                    }

                    var imagePath = Guid.NewGuid() + extension;
                    listImagePath.Add(imagePath);

                    var filePath = PhysicalDataFilePath(imagePath);
                    file.SaveAs(filePath);
                }
            }

            #endregion

            var entity = model.Map<ProductModel, Product>();

            if (entity.IsNew)
            {
                entity.InitId();
                entity.CreatedDate = DateTime.UtcNow;
                entity.UpdateDate = DateTime.UtcNow;
            }
            else
            {
                var oldData = ServiceHelper.Product.ExecuteDispose(s => s.GetProduct(entity.Id));
                if (oldData == null)
                {
                    return JsonObject(false, BackendMessage.CannotLoadData);
                }

                entity.CreatedDate = oldData.CreatedDate;
                entity.UpdateDate = DateTime.UtcNow;
            }

            var response = ServiceHelper.Product.ExecuteDispose(s => s.SaveProduct(new SaveRequest
            {
                Entity = entity
            }));

            if (response.Success)
            {
                if (listImagePath.Any())
                {
                    var listImages = new List<ProductImage>();
                    listImagePath.ForEach(imagePath =>
                    {
                        var image = new ProductImage
                        {
                            ImagePath = imagePath,
                            ProductId = entity.Id,
                            CreatedDate = DateTime.UtcNow,
                            Visible = true
                        };

                        image.InitId();

                        listImages.Add(image);
                    });

                    ServiceHelper.Product.ExecuteDispose(s => s.SaveListImage(listImages));
                }

                var indexModel = new ProductIndexModel
                {
                    TextSearch = model.TextSearch,
                };

                PopulateIndexModel(indexModel);

                return JsonObject(true, BackendMessage.SaveDataSuccess, new
                {
                    html = PartialViewToString("_list", indexModel)
                });
            }

            if (listImagePath.Any())
            {
                listImagePath.ForEach(DeleteImageFile);
            }

            return JsonObject(false, response.Messages.FirstOrDefault().GetServiceMessageRes());
        }
示例#4
0
        public BaseResponse SaveImage(ProductImage productImage)
        {
            var response = new BaseResponse();

            using (var db = DbContext)
            {
                var entityDb = productImage.Map<ProductImage, Entity.ProductImage>();

                if (!db.ProductImages.Any(e => e.Id == entityDb.Id))
                {
                    db.ProductImages.Add(entityDb);
                }
                else
                {
                    db.Entry(entityDb).State = EntityState.Modified;
                }

                response.Success = db.SaveChanges() > 0;
            }

            return response;
        }