//Comments public string AddComment(CommentWCF comment) { logger.Info("Registeres POST request - attempting to add new comment."); var response = _userController.Exist(JObject.Parse("{\"username\": \"" + comment.User + "\"}")); if ((Boolean)response["exist"]) { var post = _blogContext.Posts.ToList().Find(p => p.PostID == comment.PostID); if (post == null) { logger.Warn("Cannot add comment - There is no such PostID."); return("Error there is no such PostId"); } else { var calories = getCalories(comment.Content); var content1 = comment.Content + " Counted calories:" + calories; var commentDB = new Comment() { User = comment.User, Content = content1, Date = comment.Date, PostID = comment.PostID }; _blogContext.Comments.Add(commentDB); _blogContext.SaveChanges(); logger.Info("New comment added."); return("Comment added"); } } else { logger.Info("Cannot add Comment - there is no such user."); return("There is no such user"); } }
public int AddCode(int userID) { var rnd = new Random(); var code = new Code { }; //if code exist only update codeValue //else insert all var codeCheck = _db.Code.FirstOrDefault(x => x.User_Id == userID); if (codeCheck == null) { code.CodeValue = rnd.Next(10000, int.MaxValue); code.User_Id = userID; _db.Code.Add(code); _db.SaveChanges(); return(code.CodeValue); } else { //update only code codeCheck.CodeValue = rnd.Next(10000, int.MaxValue); _db.SaveChanges(); return(codeCheck.CodeValue); } }
public void RemovePostTag(int id, int id2) { var listaforLoop = _db.PostTags.FirstOrDefault(x => x.Post_Id == id2 && x.Tag_Id == id); // if null do not remove if (listaforLoop != null) { _db.PostTags.Remove(listaforLoop); } //save changes _db.SaveChanges(); }
public void Delete(Comment comment) { //remove tags if are not used anywhere else var commTags = _db.CommentTags.Where(x => x.Comment_Id == comment.Comment_Id); //list of PostTags //Ilist because this allows to perform Savechanges in Foreach IList <CommentTag> listaforLoop = _db.CommentTags.Where(x => x.Comment_Id == comment.Comment_Id).ToList(); foreach (var item in listaforLoop) { var tagID = item.Tag_Id; //get list of ID _db.CommentTags.Remove(item); //save changes _db.SaveChanges(); if (!_db.CommentTags.Any(x => x.Tag_Id == tagID) && !_db.PostTags.Any(x => x.Tag_Id == tagID)) { var tagToRemove = _db.Tags.FirstOrDefault(x => x.Tag_Id == tagID); if (tagToRemove == null) { throw new FieldAccessException(); } _db.Tags.Remove(tagToRemove); } } IList <IsCommUpvoted> lostOfIsCommUpvoted = _db.IsCommUpvoted .Where(x => x.Comment_Id == comment.Comment_Id) .ToList(); foreach (var item in lostOfIsCommUpvoted) { _db.IsCommUpvoted.Remove(item); _db.SaveChanges(); } _db.Comments.Remove(comment); }
public void SaveChanges() { _db.SaveChanges(); }
public void Create(T item) { _database.Set <T>().Add(item); _database.SaveChanges(); }
public void SaveChanges() { context.SaveChanges(); }
public void Commit() { _db.SaveChanges(); }
public int AddPost(Post post) { _context.Posts.Add(post); _context.SaveChanges(); return(post.Id); }
public void Delete(Post post) { //remove comments proper way #region var commentsChildren = _db.Comments.Where(x => x.Post_Id == post.Post_Id); var commentChildenlist = commentsChildren.ToList(); // to avoid dataReader excep - caused by iterating on IQueryable foreach (var item in commentChildenlist) { //remove comments var commTags = _db.CommentTags.Where(x => x.Comment_Id == item.Comment_Id); //listofPostTags //Ilist because this allows to perform Savechanges in Foreach IList <CommentTag> listaCommentLoop = _db.CommentTags.Where(x => x.Comment_Id == item.Comment_Id).ToList(); foreach (var item2 in listaCommentLoop) { var tagID = item2.Tag_Id; //get list of ID _db.CommentTags.Remove(item2); //save changes _db.SaveChanges(); if (!_db.CommentTags.Any(x => x.Tag_Id == tagID) && !_db.PostTags.Any(x => x.Tag_Id == tagID)) { var tagToRemove = _db.Tags.FirstOrDefault(x => x.Tag_Id == tagID); if (tagToRemove == null) { throw new FieldAccessException(); } _db.Tags.Remove(tagToRemove); } } IList <IsCommUpvoted> listaifPlusComm = _db.IsCommUpvoted .Where(x => x.Comment_Id == item.Comment_Id).ToList(); foreach (var item3 in listaifPlusComm) { _db.IsCommUpvoted.Remove(item3); _db.SaveChanges(); } _db.Comments.Remove(item); } #endregion //remove tags if are not used anywhere else var wpisTags = _db.PostTags.Where(x => x.Post_Id == post.Post_Id); //listofPostTags //Ilist because this allows to perform Savechanges in Foreach IList <PostTag> listaforLoop = _db.PostTags.Where(x => x.Post_Id == post.Post_Id).ToList(); foreach (var item in listaforLoop) { var tagID = item.Tag_Id; //get list of ID _db.PostTags.Remove(item); //save changes _db.SaveChanges(); if (!_db.PostTags.Any(x => x.Tag_Id == tagID) && !_db.CommentTags.Any(x => x.Tag_Id == tagID)) { var tagToRemove = _db.Tags.FirstOrDefault(x => x.Tag_Id == tagID); if (tagToRemove == null) { break; //??? } _db.Tags.Remove(tagToRemove); } } IList <IsPostUpvd> listIfPostUpvd = _db.IsPostUpvd .Where(x => x.Post_Id == post.Post_Id).ToList(); foreach (var item in listIfPostUpvd) { _db.IsPostUpvd.Remove(item); _db.SaveChanges(); } _db.Post.Remove(post); }