public ActionResult Create(Comment comment) { if (ModelState.IsValid) { comment.Author = User.Identity.Name; cdb.Comments.Add(comment); cdb.SaveChanges(); Post post = pdb.Posts.Find(comment.RefferedPost); IntegerData idata = new IntegerData(); idata.Data = comment.ID; post.Comments.Add(idata); pdb.Entry(post).State = EntityState.Modified; pdb.SaveChanges(); //if is hitchhiker if (comment.Hitchhiker == true) { Hitchhiker hitch = new Hitchhiker() { Name = comment.Author, Phone = comment.PhoneNumber, PostID = comment.RefferedPost }; hdb.Hitchhikers.Add(hitch); hdb.SaveChanges(); } return(RedirectToAction("Details", "Post", new { id = post.ID })); } else { return(View(comment)); } }
public ActionResult AddTheme(ForumTheme theme) { theme.themeDate = DateTime.Now; cm.forumThemes.Add(theme); cm.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Comment(Comment comment) { comment.Date = DateTime.Now; cdb.Comments.Add(comment); cdb.SaveChanges(); return(RedirectToAction("Comment")); }
/// <summary> /// Сохраняет в БД /// </summary> /// <param name="comments"></param> public static void Save(Comments comments) { using (var db = new CommentsContext()) { db.Comments.Add(comments); db.SaveChanges(); } }
public IActionResult DeleteById(int id, [FromHeader] int UserID, [FromHeader] string UserRole) { var deletedComment = _context.Comments.Find(id); if (deletedComment == null) { return(StatusCode(StatusCodes.Status404NotFound)); } if (_user.GetUserById(UserID) == null) { return(StatusCode(StatusCodes.Status400BadRequest)); } if (deletedComment.CreatedByUserId != UserID && UserRole != "Admin") { return(StatusCode(StatusCodes.Status403Forbidden)); } _context.Comments.Remove(deletedComment); var success = _context.SaveChanges(); if (success < 1) { return(StatusCode(StatusCodes.Status400BadRequest)); } this._loggerCommunication.logAction("Deleted a comment with id:" + id); return(StatusCode(StatusCodes.Status202Accepted, new JsonResult(deletedComment))); }
public ActionResult DeleteConfirmed(int?id) { Post post = pdb.Posts.Find(id); post.Comments.Clear(); pdb.Posts.Remove(post); pdb.SaveChanges(); var controller = DependencyResolver.Current.GetService <Comment>(); //delete post from reffered photos cdb.Comments.RemoveRange(cdb.Comments.Where(c => c.RefferedPost == id)); cdb.SaveChanges(); //delete all reffered hitchhikers hdb.Hitchhikers.RemoveRange(hdb.Hitchhikers.Where(c => c.PostID == id)); hdb.SaveChanges(); return(RedirectToAction("Index", "Users")); }
public void Save() { db.SaveChanges(); }