//[Authorize(Roles = "PR")] public ActionResult AddBlogPost() { var ops = new BlogPostOperations(); var vm = new AddBlogPostViewModel(ops.GetAllCategories()); return(View(vm)); // Author is currently being populated as the user identity email. Switch to username? }
public async Task <IActionResult> AddPost(AddBlogPostViewModel model) { if (ModelState.IsValid) { var curUser = await _userManager.GetUserAsync(User); var author = await _qpa.ProcessAsync(new GetAuthorByAppUserIdQuery { Id = curUser.Id }); var abpc = new AddBlogPostCommand { Author = author, Title = model.Title, Content = model.Content, Description = model.Description, CreatedAt = DateTime.Now, ModifiedAt = DateTime.Now, Public = false, PublishOn = DateTime.Now.AddDays(7) }; var result = await _cp.ProcessAsync(abpc); if (result.Succeeded) { return(RedirectToAction("EditPost", new { id = result.Command.Id })); } else { ModelState.AddModelError("", "Error saving to database!"); return(View(model)); } } return(View(model)); }
public ActionResult EditBlogPost(AddBlogPostViewModel model) { var helper = new BlogRepository(); if (ModelState.IsValid) { helper.UpdatePost(model); return(RedirectToAction("Index", "Blog")); } return(View(model)); }
public void UpdatePost(AddBlogPostViewModel model) { var db = new DbEl(); var post = db.BlogPosts.Find(model.BlogModelId); post.Header = model.Header; post.HtmlContent = model.HtmlContent; post.Ingress = model.Ingress; post.Timestamp = DateTime.Now; db.Entry(post).State = EntityState.Modified; db.SaveChanges(); }
public ActionResult EditBlogPost(int id) { var helper = new BlogRepository(); var obj = helper.GetSinglePost(id); var model = new AddBlogPostViewModel { BlogModelId = obj.BlogModelId, Header = obj.Header, HtmlContent = obj.HtmlContent, Ingress = obj.Ingress, }; return(View(model)); }
public void SavePost(AddBlogPostViewModel model) { var db = new DbEl(); var post = new BlogModel { Header = model.Header, HtmlContent = model.HtmlContent, ImagePath = _ImageHelper.SaveImage(model.Image), Ingress = model.Ingress, Timestamp = DateTime.Now }; db.BlogPosts.Add(post); db.SaveChanges(); }
public async Task <IActionResult> AddPost(AddBlogPostViewModel model) { var content = Request.Form["editor1"]; if (ModelState.IsValid) { var curUser = await _userManager.GetUserAsync(User); var author = await _qpa.ProcessAsync(new GetAuthorByAppUserIdQuery { Id = curUser.Id }); var abpc = new AddBlogPostCommand { Author = author, Title = model.Title, //Content = model.Content, Content = content, Description = model.Description, CreatedAt = DateTime.Now, ModifiedAt = DateTime.Now, Public = false, PublishOn = DateTime.Now.AddDays(7) }; var result = await _cp.ProcessAsync(abpc); if (result.Succeeded) { _logger.LogInformation("Added BlogPost {0} by {1} {2}", model.Title, author.FirstName, author.LastName); return(RedirectToAction("EditPost", new { id = result.Command.Id })); } else { _logger.LogWarning("Unable to add BlogPost {0} by {1} {2}", model.Title, author.FirstName, author.LastName); ModelState.AddModelError("", "Error saving to database!"); return(View(model)); } } return(View(model)); }
public async Task <ActionResult> AddPost(AddBlogPostViewModel post, IFormFile cover) { if (ModelState.IsValid) { string coverPath = "/images/BlogPosts/" + cover.FileName; using (var fileStream = new FileStream(_appEnvironment.WebRootPath + coverPath, FileMode.Create)) { await cover.CopyToAsync(fileStream); } var mappost = _mapper.Map <AddBlogPostViewModel, BlogPost>(post); mappost.Date = DateTime.Now.ToShortDateString(); mappost.AddedBy = _userManager.GetUserAsync(User).Result.UserName; mappost.CoverPath = coverPath; _db.BlogPosts.Add(mappost); _db.SaveChanges(); return(RedirectToAction("Index", "BlogPost")); } return(PartialView()); }
public async Task <IActionResult> AddPost(AddBlogPostViewModel model) { if (ModelState.IsValid) { var curUser = await _userManager.GetUserAsync(User); var author = await(from u in _userManager.Users where u.Id == curUser.Id join a in _context.Authors on u.Id equals a.ApplicationUserId select a).FirstOrDefaultAsync(); BlogPost bp = new BlogPost { Author = author, Title = model.Title, Content = model.Content, Description = model.Description, CreatedAt = DateTime.Now, ModifiedAt = DateTime.Now, Public = false, PublishOn = DateTime.Now.AddDays(7) }; try { _context.BlogPosts.Add(bp); await _context.SaveChangesAsync(); } catch (Exception) { ModelState.AddModelError("", "Error saving to database!"); return(View(model)); } return(RedirectToAction("EditPost", new { id = bp.Id })); } return(View(model)); }
public ActionResult WritePost() { var model = new AddBlogPostViewModel(); return(View(model)); }