Esempio n. 1
0
        public ActionResult Create(Carousel carousel)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("",
                                         ValidationResources.InvalidState);

                return(ViewOrPartialView(carousel));
            }

            if (carousel.Slide == null)
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageRequired);

                return(ViewOrPartialView(carousel));
            }

            if (carousel.Slide.ContentLength > 1000000)
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageLength);

                return(ViewOrPartialView(carousel));
            }

            if (!UploadUtilities.IsValidImageBinary(carousel.Slide.InputStream))
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageInvalidBinary);

                return(ViewOrPartialView(carousel));
            }

            _carouselService.SaveCarousel(carousel);

            bool isSaved;

            try
            {
                isSaved = _unitOfWork.SaveChanges() > 0;
            }
            catch
            {
                isSaved = false;
            }

            if (isSaved)
            {
                Logger.SaveLog(new CreateCarouselProvider(carousel));
            }
            else
            {
                ModelState.AddModelError("", ValidationResources.CreationFailure);

                return(ViewOrPartialView(carousel));
            }

            UploadUtilities.TryToSaveImage(carousel.Slide, Constants.CarouselsUrl, carousel.Id.ToString());

            if (IsReferrerValid())
            {
                return(Redirect(Request.UrlReferrer.AbsolutePath));
            }

            return(RedirectToAction("List", new { page = 1 }));
        }
Esempio n. 2
0
        public ActionResult EditPost(int id)
        {
            Carousel dbCarousel = _carouselService.GetCarouselById(id);

            if (dbCarousel == null)
            {
                return(EntityNotFoundView());
            }

            TryUpdateModel(dbCarousel, new[] { "Title", "Description", "LinkUrl", "Slide", "DisplayOrder" });

            if (!TryValidateModel(dbCarousel))
            {
                ModelState.AddModelError("",
                                         ValidationResources.InvalidState);

                return(ViewOrPartialView(dbCarousel));
            }

            if (dbCarousel.Slide != null && dbCarousel.Slide.ContentLength > 1000000)
            {
                ModelState.AddModelError("Slide",
                                         ValidationResources.SlideImageLength);

                return(ViewOrPartialView(dbCarousel));
            }

            bool isSaved = true;

            try
            {
                isSaved = _unitOfWork.SaveChanges() > 0;
            }
            catch
            {
                isSaved = false;
            }

            if (isSaved)
            {
                Logger.SaveLog(new UpdateCarouselProvider(dbCarousel));
            }
            else
            {
                ModelState.AddModelError("", ValidationResources.UpdateFailure);

                return(ViewOrPartialView(dbCarousel));
            }

            if (dbCarousel.Slide != null)
            {
                UploadUtilities.TryToSaveImage(dbCarousel.Slide, Constants.CarouselsUrl, dbCarousel.Id.ToString());
            }

            if (IsReferrerValid())
            {
                return(Redirect(Request.UrlReferrer.AbsolutePath));
            }

            return(RedirectToAction("List", new { page = 1 }));
        }