public ActionResult Index()
 {
     var model = new ArticleViewModel();
     model.Articles = articleService.GetArticlesByUserId(CurrentUser.Id);
   
     return View(model);
 }
        public ActionResult NewArticle()
        {
            var model = new ArticleViewModel();
            model.AddCategories(paramService.GetAllCategories());
            model.AddDegrees(paramService.GetAllDegrees());
            model.AddCurrentUserAsAuthor(userManager.FindById(CurrentUser.Id));

            return View(model);
        }
        public ActionResult NewArticle(ArticleViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.DocumentFile == null || model.DocumentFile.ContentLength <= 0)
                {
                    AddError("DocumentFile", "Por favor suba el documento de su artículo. (Formato Microsoft Word .doc o .docx)");
                }
                else
                {
                    var fileExtension = Path.GetExtension(model.DocumentFile.FileName);
                    if (fileExtension != ".docx" && fileExtension != ".doc")
                        AddError("DocumentFile", "El archivo debe de estar en formato Microsoft Word (.doc o .docx)");
                }

                var keywords = model.Keywords.Split(new char[] { ',', ';' }, 5, StringSplitOptions.RemoveEmptyEntries)
                                .Select(x => x.Trim());
                if(keywords != null && keywords.Count() < 3 || keywords.Count() > 5)
                {
                    AddError("Keywords", "Por favor escriba por lo menos 3 palabras clave.");
                }

                if(ModelHasErrors)
                    return RedirectToAction("NewArticle");

                var article = Mapper.Map<Article>(model);
                var authors = Mapper.Map<IEnumerable<ArticleAuthor>>(model.AuthorList.Where(x => x.IsVisible));

                article.UserID = CurrentUser.Id;
                article.FilePath = model.DocumentFile.FileName;
                article.FileStream = model.DocumentFile.InputStream;
                article.Authors = authors;
                string directoryPath = Server.MapPath("~\\");

                var result = articleService.SaveArticle(article, directoryPath);

                DisplayMessage(result);
                if (result.Success)
                    return RedirectToAction("Index");
            }

            TempData["ModelState"] = ModelState; //don't delete!
            return RedirectToAction("NewArticle");
        }
        public ActionResult EditArticle(ArticleViewModel model)
        {
            var item = articleService.GetArticleByUserId(model.ArticleID, CurrentUser.Id);
            if (item == null)
                return HttpNotFound();

            if (item.ArticleStatusID == ArticleStatusCode.EnRevision ||
                item.ArticleStatusID == ArticleStatusCode.Aceptado)
                return View("ArticleStatus", item);

            if (ModelState.IsValid)
            {
                var article = Mapper.Map<Article>(model);
                var authors = Mapper.Map<IEnumerable<ArticleAuthor>>(model.AuthorList.Where(x => x.IsVisible));

                if (model.DocumentFile != null && model.DocumentFile.ContentLength > 0)
                {
                    var fileExtension = Path.GetExtension(model.DocumentFile.FileName);
                    if (fileExtension != ".docx" && fileExtension != ".doc")
                        AddError("DocumentFile", "El archivo debe de estar en formato Microsoft Word (.doc o .docx)");
                    else
                    {
                        article.FilePath = model.DocumentFile.FileName;
                        article.FileStream = model.DocumentFile.InputStream;
                    }
                }

                var keywords = model.Keywords.Split(new char[] { ',', ';' }, 5, StringSplitOptions.RemoveEmptyEntries)
                                .Select(x => x.Trim());
                if (keywords != null && keywords.Count() < 3 || keywords.Count() > 5)
                    AddError("Keywords", "Por favor escriba por lo menos 3 palabras clave.");

                if (ModelHasErrors)
                    return RedirectToAction("EditArticle");

                article.UpdatedByUserID = CurrentUser.Id;
                article.Authors = authors;
                string directoryPath = Server.MapPath("~\\");

                var result = articleService.UpdateArticle(article, CurrentUser.Id, directoryPath);

                DisplayMessage(result);
                return RedirectToAction("EditArticle");
            }

            TempData["ModelState"] = ModelState; 
            return RedirectToAction("EditArticle");
        }