public ActionResult DeleteConfirmed(int id, string ImagePath)
        {
            PublicationImage publicationImage = db.PublicationImages.Find(id);

            try
            {
                // Delete from DB
                db.PublicationImages.Remove(publicationImage);
                db.SaveChanges();

                // Delete file in server
                var fullPath = Server.MapPath(ImagePath);

                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                    ViewBag.Message = "Archivo eliminado correctamente";
                }

                // Construct viewbag for principal image of the publication
                ViewBag.CurrentPublication      = Session["CurrentPublication"];
                ViewBag.CurrentPublicationId    = Session["CurrentPublicationId"];
                ViewBag.CurrentPublicationTitle = Session["CurrentPublicationTitle"];
            }
            catch (Exception)
            {
                ViewBag.Message = "Error eliminando el archivo del servidor";
                return(View());
            }

            //return RedirectToAction("Index");
            return(RedirectToAction("ImagesByPublicationId/" + publicationImage.ID));
        }
Example #2
0
        public void AddPubImage(int pubId, int imageId, int?pageNumber)
        {
            var pub = database.Publications.FirstOrDefault(p => p.PublicationID == pubId);
            var img = database.ImageFiles.FirstOrDefault(i => i.ImageID == imageId);

            if (pub == null)
            {
                throw new Exception("Print Material does not exist");
            }
            if (img == null)
            {
                throw new Exception("Image does not exist");
            }
            if (pub.PublicationImages.Any(pi => pi.ImageID == imageId))
            {
                throw new Exception("Image is already associated with Print Material");
            }

            PublicationImage pubImg = new PublicationImage();

            pubImg.ImageFile  = img;
            pubImg.PageNumber = pageNumber;

            pub.PublicationImages.Add(pubImg);

            database.SaveChanges();
        }
 public ActionResult Edit([Bind(Include = "PublicationImagenID,ImagePath,Description,ID")] PublicationImage publicationImage)
 {
     if (ModelState.IsValid)
     {
         db.Entry(publicationImage).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ID = new SelectList(db.Publications, "ID", "Title", publicationImage.ID);
     return(View(publicationImage));
 }
Example #4
0
        public void SetId_Correct(int id)
        {
            // Arrange
            var image = new PublicationImage();

            // Act
            image.Id = id;

            // Assert
            Assert.AreEqual(image.Id, id);
        }
Example #5
0
        public void SetContent_Correct()
        {
            // Arrange
            byte[] content = new byte[] { 12, 45, 65, 22, 11, 66 };
            var    image   = new PublicationImage();

            // Act
            image.Content = content;

            // Assert
            Assert.AreEqual(image.Content, content);
        }
Example #6
0
        public void ReturnInstanceOfPublicationImage_WithSetCorrectProperties()
        {
            // Arrange
            byte[] content = new byte[] { 123, 12, 45, 54 };

            // Act
            var image = new PublicationImage(content);

            // Assert
            Assert.AreEqual(image.Content, content);
            Assert.IsInstanceOf <PublicationImage>(image);
        }
        public void CallPublicationFactory_CreatePublicationOnce()
        {
            // Arrange
            var mockedPublicationRepository = new Mock <IEFRepository <Publication> >();
            var mockedFriendsService        = new Mock <IFriendService>();
            var mockedUserService           = new Mock <IUserService>();
            var user = new CustomUser()
            {
                Id = 1
            };

            mockedUserService.Setup(x => x.GetByIndentityId(It.IsAny <string>())).Returns(user);
            var mockedUnitOfWork         = new Mock <IUnitOfWork>();
            var mockedPublicationFactory = new Mock <IPublicationFactory>();
            var mockedDateTimeService    = new Mock <IDateTimeService>();
            var date = new DateTime(2004, 5, 20);

            mockedDateTimeService.Setup(x => x.GetCurrentDate()).Returns(date);
            var mockedPublicationImageFactory = new Mock <IPublicationImageFactory>();
            var image = new PublicationImage();

            mockedPublicationImageFactory.Setup(x => x.CreatePublicationImage(It.IsAny <byte[]>())).Returns(image);
            var mockedCommentService = new Mock <ICommentService>();

            var publicationService = new PublicationService(
                mockedPublicationRepository.Object,
                mockedFriendsService.Object,
                mockedUserService.Object,
                mockedUnitOfWork.Object,
                mockedPublicationFactory.Object,
                mockedDateTimeService.Object,
                mockedPublicationImageFactory.Object,
                mockedCommentService.Object);
            string content = "some content";
            string userId  = "some-id";

            byte[] imageContent = new byte[] { 12, 43, 65, 12, 1, 45 };

            // Act
            publicationService.CreatePublication(content, userId, imageContent);

            // Assert
            mockedPublicationFactory
            .Verify(
                x => x.CreatePublication(
                    It.Is <string>(c => c == content),
                    It.Is <int>(i => i == user.Id),
                    It.Is <DateTime>(d => d == date),
                    It.Is <PublicationImage>(p => p == image)),
                Times.Once);
        }
        // GET: PublicationImages/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PublicationImage publicationImage = db.PublicationImages.Find(id);

            if (publicationImage == null)
            {
                return(HttpNotFound());
            }
            return(View(publicationImage));
        }
        // GET: PublicationImages/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PublicationImage publicationImage = db.PublicationImages.Find(id);

            if (publicationImage == null)
            {
                return(HttpNotFound());
            }
            ViewBag.ID = new SelectList(db.Publications, "ID", "Title", publicationImage.ID);
            return(View(publicationImage));
        }
        public ActionResult Create([Bind(Include = "Description,ID")] PublicationImage publicationImage, HttpPostedFileBase ImageFile)
        {
            if (ModelState.IsValid)
            {
                // Verify if there is an associated image
                if (ImageFile != null && ImageFile.ContentLength > 0)
                {
                    try
                    {
                        // Construct the name to the image file
                        string fileName  = Path.GetFileNameWithoutExtension(ImageFile.FileName);
                        string extension = Path.GetExtension(ImageFile.FileName);

                        fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
                        publicationImage.ImagePath = "/Content/Images/" + fileName;
                        fileName = Path.Combine(Server.MapPath("~/Content/Images/"), fileName);

                        // Save the image in server
                        ImageFile.SaveAs(fileName);

                        // Save publication in BD
                        db.PublicationImages.Add(publicationImage);
                        db.SaveChanges();

                        ViewBag.Message = "Imagen cargada exitosamente";
                    }
                    catch (Exception ex)
                    {
                        ViewBag.Message = "ERROR:" + ex.Message.ToString();
                    }
                }
                else
                {
                    ViewBag.Message = "No se ha encontrado un archivo vĂ¡lido.";
                }

                return(RedirectToAction("ImagesByPublicationId/" + publicationImage.ID));
            }

            // Construct viewbag for principal image of the publication
            Session["CurrentPublication"] = db.Publications.Find(publicationImage.ID);

            //ViewBag.ID = new SelectList(db.Publications, "ID", "Title", publicationImage.ID);
            return(View(publicationImage));
        }
        private void SavePublicationImage(PostViewModel postViewModel)
        {
            var publicationId      = this._db.Publication.OrderByDescending(p => p.IdPublication).FirstOrDefault().IdPublication;
            var publicationImage   = new PublicationImage();
            var publicationImageId = 0;


            var index = 0;

            foreach (var image in postViewModel.PublicationImage)
            {
                publicationImage.IdPublication = publicationId;

                var publicationImageCount = this._db.PublicationImage.Count();

                if (publicationImageCount > 0)
                {
                    publicationImageId = Convert.ToInt32(this._db.PublicationImage.Max(p => p.IdPublicationImage)) + 1;
                }

                publicationImage.Date  = DateTime.Parse(DateTime.Today.ToString("yyyy-MM-dd HH:mm"));
                publicationImage.Image = this.GetImagePath(publicationId, index, image.ContentType) /*"../Assets/Images/"+ publicationImageId.ToString()+"/Imagen"+ (index+1).ToString()+"."+image.ContentType*/;
                //publicationImage.IdPublicationImage = publicationImageId;
                //publicationImage.Publication.Add(postViewModel.Publication);

                this._db.PublicationImage.Add(publicationImage);

                //var file = postViewModel.PublicationImage[index];

                index++;


                this._db.SaveChanges();
                this.CopyImageToServer(publicationId, image, index);
            }

            //this._db.SaveChanges();
        }