public virtual ActionResult Edit(ArticleEditVM vm)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { success = false });
            }
            try
            {
                var article = unitOfWork.ArticleRepository.GetByID(vm.ArticleId);
                if (article == null)
                    article = unitOfWork.ArticleRepository.Insert(new Article() { ArticleId = vm.ArticleId, DateCreated = DateTime.Now });
                ArticleMapper.GetArticle(vm, ref article);
                if (article.ArticlesHashtagsXRefs == null)
                    article.ArticlesHashtagsXRefs = new List<ArticlesHashtagsXRef>();
                if (vm.HashtagIds == null)
                    vm.HashtagIds = new List<Guid>();
                ChangeHashtags(article.ArticleId, article.ArticlesHashtagsXRefs.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 articlePath = Server.MapPath(Constants.ArticleImagesDir);
                    string path = Path.Combine(
                                           articlePath, article.ArticleId.ToString() + extension);

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

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

                return Json(new { success = true });
            }
            catch
            {
                return Json(new { success = false });
            }
        }
Beispiel #2
0
        public static void GetArticle(ArticleEditVM vm, ref Article article)
        {

            if (article == null)
            {
                article = new Article();
            }

            if (vm != null)
            {
                article.HeaderBG = vm.HeaderBG;
                article.HeaderEN = vm.HeaderEN;
                article.TextBG = vm.TextBG;
                article.TextEN = vm.TextEN;
                article.IsImportant = vm.IsImportant;
            }
        }
Beispiel #3
0
        public static ArticleEditVM GetVM(Article article)
        {
            var vm = new ArticleEditVM();

            if (article == null)
                article = new Article()
                {
                    ArticleId = Guid.NewGuid()
                };

            vm.ArticleId = article.ArticleId;
            vm.FileName = article.FileName;
            vm.HeaderBG = article.HeaderBG;
            vm.HeaderEN = article.HeaderEN;
            vm.TextBG = article.TextBG;
            vm.TextEN = article.TextEN;
            vm.IsImportant = article.IsImportant;
            vm.HashtagIds = article.ArticlesHashtagsXRefs.Select(e => e.HashtagId).ToList();

            return vm;
        }