public virtual ActionResult Edit(ImageEditVM vm)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { success = false });
            }
            try
            {
                var image = unitOfWork.ImageRepository.GetByID(vm.ImageId);
                if (image == null)
                    image = unitOfWork.ImageRepository.Insert(new Image() { ImageId = vm.ImageId, DateCreated = DateTime.Now });
                ImageMapper.GetImage(vm, ref image);
                if (image.ImagesHashtagsXRefs == null)
                    image.ImagesHashtagsXRefs = new List<ImagesHashtagsXRef>();
                if (vm.HashtagIds == null)
                    vm.HashtagIds = new List<Guid>();
                ChangeHashtags(image.ImageId, image.ImagesHashtagsXRefs.Select(e => e.HashtagId).ToList(), vm.HashtagIds);


                // Image save
                HttpPostedFileBase file = Request.Files["uploadPhoto"];
                if (file != null && file.ContentLength > 0)
                {
                    string extension = Path.GetExtension(file.FileName);
                    string imagePath = Server.MapPath(Constants.GalleryImagesDir);
                    string path = Path.Combine(
                                           imagePath, image.ImageId.ToString() + extension);

                    //Check if file is image
                    if (file.ContentType.StartsWith("image/"))
                    {
                        //Delete old image
                        var oldPath = Path.Combine(
                                           imagePath, image.ImageId.ToString() + image.FileExtension);
                        if (System.IO.File.Exists(oldPath))
                        {
                            System.IO.File.Delete(oldPath);
                        }

                        image.FileExtension = extension;
                        file.SaveAs(path);
                    }
                }
                unitOfWork.Save();

                return Json(new { success = true });
            }
            catch
            {
                return Json(new { success = false });
            }
        }
Esempio n. 2
0
        public static void GetImage(ImageEditVM vm, ref Image image)
        {

            if (image == null)
            {
                image = new Image();
            }

            if (vm != null)
            {
                image.IsImportant = vm.IsImportant;
            }
        }
Esempio n. 3
0
        public static ImageEditVM GetVM(Image image)
        {
            var vm = new ImageEditVM();

            if (image == null)
                image = new Image()
                {
                    ImageId = Guid.NewGuid()
                };

            vm.ImageId = image.ImageId;
            vm.FileName = image.FileName;
            vm.IsImportant = image.IsImportant;
            vm.HashtagIds = image.ImagesHashtagsXRefs.Select(e => e.HashtagId).ToList();

            return vm;
        }