예제 #1
0
        public ActionResult Create(Article article)
        {
            var file = Request.Files["ArticleLogo"];
            ArticleSubsection subsection;
            int firstSectionId;

            try
            {
                if (article.SubsectionId == 0)
                {
                    ModelState.AddModelError("SubsectionId", "Необходимо указать подраздел");
                    TempData["error"] = "Укажи подраздел!";
                }

                if (file == null || file.ContentLength == 0)
                {
                    ModelState.AddModelError("Logo", "Вы не загрузили изображение");
                }

                int id;
                var uploader = new ImageUploader(file);
                uploader.Interpolation = InterpolationMode.HighQualityBilinear;
                uploader.Quality       = CompositingQuality.HighQuality;
                article.Logo           = uploader.UniqueName;
                article.CreatedAt      = DateTime.Now;
                article.ViewsCount     = 0;

                // TODO: Add a slug logic!

                if (ModelState.IsValid)
                {
                    try
                    {
                        uploader.Convert(ProjectConfiguration.Get.ArticleImageWidth, ProjectConfiguration.Get.ArticleImageHeight);
                        uploader.Save("articles");
                    }
                    catch (Exception)
                    {
                        TempData["error"] = "Возникли проблемы при загрузке изображения. Попробуйте снова";
                    }

                    try
                    {
                        id = _articleRepository.Save(article);
                    }
                    catch (Exception)
                    {
                        TempData["error"] = "Возникли проблемы при загрузке изображения. Попробуйте снова";
                        return(View(article));
                    }

                    return(RedirectToAction("Details", new { id = id }));
                }
            }
            catch
            {
                subsection = _subsectionRepository.Read(article.SubsectionId);

                if (subsection != null)
                {
                    firstSectionId = PopulateSectionDropDownList(subsection.Section);
                    PopulateSubsectionDropDownList((subsection.Section != null) ? subsection.Section.Id : firstSectionId, article.Subsection);
                }
                else
                {
                    PopulateSectionDropDownList(allowEmpty: true);
                }
                return(View(article));
            }

            subsection = _subsectionRepository.Read(article.SubsectionId);

            if (subsection != null)
            {
                firstSectionId = PopulateSectionDropDownList(subsection.Section);
                PopulateSubsectionDropDownList((subsection.Section != null) ? subsection.Section.Id : firstSectionId, article.Subsection);
            }
            else
            {
                PopulateSectionDropDownList(allowEmpty: true);
            }
            return(View(article));
        }