public void DeleteComment(Guid commentId) { using (var dbContext = new CommentDbContext()) { var comment = dbContext.Comments.FirstOrDefault(c => c.Id == commentId); DeleteCommentAndChild(comment, dbContext); dbContext.SaveChanges(); } }
public IHttpActionResult LikeComment(int ID) { using (var db = new CommentDbContext()) { db.Comments.FirstOrDefault(c => c.ID == ID).Likes++; db.SaveChanges(); return(Ok()); } }
public void DeleteCommentBySourceId(Guid sourceId) { using (var dbContext = new CommentDbContext()) { var comments = dbContext.Comments.Where(c => c.SourceId == sourceId).ToList(); foreach (var comment in comments) { DeleteCommentAndChild(comment, dbContext); } dbContext.SaveChanges(); } }
public IHttpActionResult Post(Comment comment) { if (comment == null) { return(BadRequest()); } using (var db = new CommentDbContext()) { var returnValue = db.Comments.Add(comment); db.SaveChanges(); return(Ok(returnValue)); //returns the comment that ahs been added } }
public void SaveComment(Models.Comment comment) { using (var dbContext = new CommentDbContext()) { dbContext.Comments.Add(comment); if (dbContext.SaveChanges() > 0 && comment.Files != null) { foreach (var file in comment.Files) { _storageFileService.AssociateFile(comment.Id, CommentModule.Key, CommentModule.DisplayName, file.Id); } } } }
public IActionResult Create(CommentViewModel model) { if (ModelState.IsValid) { _dbContext.Add(new Comment { Username = model.Username, Date = DateTime.Now, Text = model.Text }); _dbContext.SaveChanges(); return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Ошибка ввода!"); } return(View(model)); }
public IHttpActionResult PostAnswer(string author, string answer, int parent) { if (answer == null) { return(BadRequest()); } Comment comment = new Comment(); comment.Parent = parent; comment.Author = author; comment.Message = answer; using (var db = new CommentDbContext()) { var returnValue = db.Comments.Add(comment); db.SaveChanges(); return(Ok(returnValue)); //returns the comment that ahs been added } }
private void Save() { try { context.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { string rs = ""; foreach (var eve in e.EntityValidationErrors) { rs = string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); Console.WriteLine(rs); foreach (var ve in eve.ValidationErrors) { rs += "<br />" + string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw new Exception(rs); } }