コード例 #1
0
        public async Task <JsonResult> CreateComment([FromBody] CommentModel commentModel)
        {
            try
            {
                var comment = new MComments();
                comment.Id          = commentModel.Id;
                comment.PostId      = commentModel.PostId;
                comment.CommentBody = commentModel.CommentBody;
                comment.UserEmail   = commentModel.UserEmail;


                db.MComments.Add(comment);
                await db.SaveChangesAsync();

                return(Json(new
                {
                    res = "ok",
                    data = "Comment saved"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    res = "err",
                    msg = ex.Message
                }));
            }
        }
コード例 #2
0
        public async Task <JsonResult> UpDateComment_([FromBody] CommentModel commentModel)
        {
            try
            {
                var comment = new MComments();
                comment.Id          = commentModel.Id;
                comment.PostId      = commentModel.PostId;
                comment.CommentBody = commentModel.CommentBody;
                comment.UserEmail   = commentModel.UserEmail;

                var existingComment = db.MComments.Find(comment.Id);
                if (existingComment == null)
                {
                    return(Json(new
                    {
                        res = "err",
                        msg = $"Comment with id={comment.Id} does not exist"
                    }));
                }
                //update all the fields
                existingComment.UserEmail       = comment.UserEmail;
                existingComment.CommentBody     = comment.CommentBody;
                db.Entry(existingComment).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                await db.SaveChangesAsync();

                return(Json(new
                {
                    res = "ok",
                    data = "Comment updated"
                }));
            }
            catch (Exception ex)
            {
                return(Json(new
                {
                    res = "err",
                    msg = ex.Message
                }));
            }
        }