public async Task <JsonResult> Edit(ArticleIM vm) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR, JsonRequestBehavior.DenyGet)); } try { var article = await _db.Articles.FindAsync(vm.Id); var editArticle = _mapper.Map(vm, article); // _articleServices.Update(editArticle); _db.Entry(editArticle).State = EntityState.Modified; await _db.SaveChangesAsync(); // _pageMetaServices.SetPageMeta(ModelType.ARTICLE, vm.Id.ToString(), vm.Title, vm.SEOTitle, vm.Keywords, vm.SEODescription); await SetPageMetaAsync(_db, (short)ModelType.ARTICLE, editArticle.Id.ToString(), editArticle.Title, vm.SEOTitle, vm.Keywords, vm.SEODescription); AR.SetSuccess(String.Format(Messages.AlertUpdateSuccess, EntityNames.Article)); return(Json(AR, JsonRequestBehavior.DenyGet)); } catch (Exception er) { AR.Setfailure(er.Message); return(Json(AR, JsonRequestBehavior.DenyGet)); } }
public async Task <JsonResult> AddA(ArticleIM vm) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR, JsonRequestBehavior.DenyGet)); } try { var newArticle = _mapper.Map <ArticleIM, Article>(vm); _db.Articles.Add(newArticle); var result = await _db.SaveChangesAsync(); if (result > 0) { // _pageMetaServices.SetPageMeta(ModelType.ARTICLE, result.Id.ToString(),result.Title, vm.SEOTitle,vm.Keywords,vm.SEODescription); await SetPageMetaAsync(_db, (short)ModelType.ARTICLE, newArticle.Id.ToString(), newArticle.Title, vm.SEOTitle, vm.Keywords, vm.SEODescription); } AR.SetSuccess(String.Format(Messages.AlertCreateSuccess, EntityNames.Article)); return(Json(AR, JsonRequestBehavior.DenyGet)); } catch (Exception er) { AR.Setfailure(er.Message); return(Json(AR, JsonRequestBehavior.DenyGet)); } }
public async Task <IActionResult> PostArticle([FromBody] ArticleIM article) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var model = _mapper.Map <Article>(article); _context.Articles.Add(model); await _context.SaveChangesAsync(); return(Ok()); }
public async Task <ActionResult> Edit(int?id) { var categoryList = await _db.ArticleCategories.OrderByDescending(c => c.Importance).ToListAsync(); ViewBag.Categories = new SelectList(categoryList, "Id", "Title"); if (id == null) { var article = new ArticleIM { Active = true, Source = SettingsManager.Site.SiteName, Pubdate = DateTime.Now }; return(View(article)); } else { var vArticle = await _db.Articles.FindAsync(id); if (vArticle == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR, JsonRequestBehavior.AllowGet)); } var editArticle = _mapper.Map <Article, ArticleIM>(vArticle); var pageMeta = await _db.PageMetas.FirstOrDefaultAsync(d => d.ModelType == (short)ModelType.ARTICLE && d.ObjectId == editArticle.Id.ToString()); if (pageMeta != null) { editArticle.SEOTitle = pageMeta.Title; editArticle.Keywords = pageMeta.Keyword; editArticle.SEODescription = pageMeta.Description; } return(View(editArticle)); } }
// GET: Admin/Articles/Edit/5 public async Task <IActionResult> Edit(int?id) { var vm = new ArticleIM(); if (id == null) { vm.Active = true; vm.PubDate = DateTime.Now; } else { var article = await _context.Articles.FindAsync(id); if (article == null) { return(NotFound()); } vm = _mapper.Map <ArticleIM>(article); var pm = await _context.PageMetas.FirstOrDefaultAsync(d => d.ModuleType == (short)ModuleType.ARTICLE && d.ObjectId == vm.Id.ToString()); if (pm != null) { vm.SEOTitle = pm.Title; vm.SEOKeywords = pm.Keywords; vm.SEODescription = pm.Description; } } var categories = await _context.ArticleCategories.AsNoTracking() .OrderByDescending(d => d.Importance).ToListAsync(); ViewData["Categories"] = new SelectList(categories, "Id", "Title"); return(View(vm)); }
public async Task <IActionResult> PutArticle([FromRoute] int id, [FromBody] ArticleIM article) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != article.Id) { return(BadRequest()); } var model = await _context.Articles.FirstOrDefaultAsync(d => d.Id == id); model = _mapper.Map(article, model); _context.Entry(model).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ArticleExists(id)) { return(NotFound()); } else { throw; } } return(Ok()); }
public async Task <IActionResult> Edit(ArticleIM article) { if (!ModelState.IsValid) { AR.Setfailure(GetModelErrorMessage()); return(Json(AR)); } try { var pm = new PageMeta { Title = article.SEOTitle, Description = article.SEODescription, Keywords = article.SEOKeywords, ModuleType = (short)ModuleType.ARTICLE }; if (article.Id > 0) { var model = await _context.Articles.FirstOrDefaultAsync(d => d.Id == article.Id); if (model == null) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } model = _mapper.Map(article, model); model.UpdatedBy = User.Identity.Name; model.UpdatedDate = DateTime.Now; _context.Entry(model).State = EntityState.Modified; await _context.SaveChangesAsync(); AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Article)); pm.ObjectId = model.Id.ToString(); } else { var model = _mapper.Map <Article>(article); model.CreatedBy = User.Identity.Name; model.CreatedDate = DateTime.Now; //model.Body = WebUtility.HtmlEncode(page.Body); _context.Add(model); await _context.SaveChangesAsync(); pm.ObjectId = model.Id.ToString(); AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Article)); } await CreatedUpdatedPageMetaAsync(_context, pm); return(Json(AR)); } catch (DbUpdateConcurrencyException) { if (!ArticleExists(article.Id)) { AR.Setfailure(Messages.HttpNotFound); return(Json(AR)); } else { throw; } } }