Exemplo n.º 1
0
        //put comment
        public static bool PutComment(string name, int id, CommentModel model)
        {
            try
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = DB_Config.GetConnectionString();
                    con.Open();

                    IssueModel issue = DB_AuxGets.GetIssueById(name, id, con);
                    if (issue == null)
                    {
                        throw new NotFoundException(string.Format("The issue {0} was not found in the project {1}.", id, name));
                    }

                    if (DB_AuxGets.GetIssueById(name, id, con).issue_state == "closed")
                    {
                        throw new ClosedIssueException(string.Format("The issue {0} is closed, so it does not allow edit to comments.", id));
                    }

                    //gets existing comment to edit
                    CommentModel old_model = DB_AuxGets.GetComments(name, id, con).Find(c => c.comment_id == model.comment_id);
                    if (old_model == null)
                    {
                        throw new NotFoundException("The comment with id " + model.comment_id + " does not exist in this issue.");
                    }

                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "update comment set comment_text=@text and modifiedDate=GETUTCDATE() where proj_name=@name and issue_id=@issue_id and comment_id=@id";
                        SqlParameter proj_name = new SqlParameter("@proj_name", System.Data.SqlDbType.VarChar, 30);
                        proj_name.Value = name;
                        cmd.Parameters.Add(proj_name);

                        SqlParameter iss_id = new SqlParameter("@issue_id", System.Data.SqlDbType.Int);
                        iss_id.Value = id;
                        cmd.Parameters.Add(iss_id);

                        SqlParameter cmt_id = new SqlParameter("@id", System.Data.SqlDbType.Int);
                        //because comments can't be removed, the number of comments stored +1 = id for new comment
                        cmt_id.Value = issue.comments.Count + 1;
                        cmd.Parameters.Add(cmt_id);

                        SqlParameter cmt_text = new SqlParameter("@text", System.Data.SqlDbType.VarChar, 200);
                        cmt_text.Value = model.comment_text;
                        cmd.Parameters.Add(cmt_text);

                        cmd.ExecuteReader();
                    }
                }
                //sucess
                return true;
            }
            catch (SqlException e)
            {
                throw new InternalDBException(e.ToString());
            }
        }
Exemplo n.º 2
0
        //post comment
        public static bool PostComment(string name, int id, CommentModel model)
        {
            try
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = DB_Config.GetConnectionString();
                    con.Open();

                    IssueModel issue = DB_AuxGets.GetIssueById(name, id, con);
                    if (issue == null)
                    {
                        throw new NotFoundException("The issue " + id + " does not exist in the project " + name + ".");
                    }
                    if (issue.issue_state == "closed")
                    {
                        throw new ClosedIssueException("The issue " + id + " in the project " + name + " is closed, so it doesn't allow new comments.");
                    }
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandText = "insert into comment(proj_name,issue_id,comment_id,comment_text,comment_creationDate) values(@name,@issue_id,@id,@text,GETUTCDATE())";
                        SqlParameter proj_name = new SqlParameter("@name", System.Data.SqlDbType.VarChar, 100);
                        proj_name.Value = name;
                        cmd.Parameters.Add(proj_name);

                        SqlParameter iss_id = new SqlParameter("@issue_id", System.Data.SqlDbType.Int);
                        iss_id.Value = id;
                        cmd.Parameters.Add(iss_id);

                        SqlParameter cmt_id = new SqlParameter("@id", System.Data.SqlDbType.Int);
                        //because comments can't be removed, the number of comments stored +1 = id for new comment

                        if (issue.comments==null)
                        {
                            cmt_id.Value = 1;
                        }
                        else
                        {
                            cmt_id.Value = issue.comments.Count + 1;
                        }

                        cmd.Parameters.Add(cmt_id);

                        SqlParameter cmt_text = new SqlParameter("@text", System.Data.SqlDbType.VarChar, 200);
                        cmt_text.Value = model.comment_text;
                        cmd.Parameters.Add(cmt_text);

                        cmd.ExecuteReader();
                    }
                }
                //sucess
                return true;
            }
            catch (SqlException e)
            {
                throw new InternalDBException(e.ToString());
            }
        }