Example #1
0
        protected void btnAddNewComment_Click(object sender, EventArgs e)
        {
            List <(bool, int, Exception)> ex = new List <(bool, int, Exception)>();
            int    postId      = (int)Session["CurrentParentPost"];
            string commentText = taComment.InnerText;

            if (string.IsNullOrEmpty(commentText))
            {
                smlCommentHelp.InnerText = "Please enter comment text.";
                return;
            }
            else if (commentText.Length > 500)
            {
                smlCommentHelp.InnerText = "Comment is too long(Max 500 characters)";
            }

            Comment newComment = new Comment(-1, currentUser.Username, postId, commentText);
            bool    result     = DBObjWriter.GenericWriteToDB <Comment>(newComment, "TP_CreateComment", ref ex, new List <string> {
                "Id"
            });


            if (result)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Comment created succesfully')", true);
                Response.Redirect("Home.aspx");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('There was an error creating your comment, please try again')", true);
                return;
            }
        }
        [HttpPost("CreateComment")] //https://localhost:44312/api/Post/CreateComment
        public string CreateComment(string text, string username, int postId)
        {
            Exception ex = null;
            List <(string field, dynamic value, Type type)> filter = new List <(string field, dynamic value, Type type)>();

            filter.Add(DBObjCreator.CreateFilter("Username", $"{username}", typeof(string)));
            List <object[]> records = DBObjCreator.ReadDBObjsWithWhere("TP_GetUser", ref ex, filter);

            if (records.Count != 1)//Username/password combo not valid
            {
                return("Invalid username.");
            }
            Comment newComment = new Comment(-1, username, postId, text);
            List <(bool, int, Exception)> errors = new List <(bool, int, Exception)>();
            bool result = DBObjWriter.GenericWriteToDB <Comment>(newComment, "TP_CreateComment", ref errors, new List <string>()
            {
                "Id"
            });

            return("Comment succesfully created");
        }