Example #1
0
    public static string SaveComments(int workRequestID, dynamic comments)
    {
        Dictionary <string, string> result = new Dictionary <string, string>()
        {
            { "saved", "" }, { "ids", "" }, { "error", "" }
        };

        bool   saved = false;
        string errorMsg = string.Empty, ids = string.Empty;
        int    commentId = 0, parentCommentId = 0;
        string commentText = string.Empty;

        foreach (dynamic comment in comments)
        {
            commentId       = 0;
            parentCommentId = 0;
            //comment[0]=commentid, comment[1]=parentid, comment[2]=text
            try
            {
                commentText = comment[2].ToString();
                if (string.IsNullOrWhiteSpace(comment[0].ToString()) ||
                    !int.TryParse(comment[0].ToString(), out commentId))
                {
                    int.TryParse(comment[1].ToString(), out parentCommentId);
                    saved = WorkRequest.WorkRequest_Comment_Add(out commentId, out errorMsg, workRequestID, parentCommentId, commentText);
                }
                else
                {
                    saved = WorkRequest.WorkRequest_Comment_Update(out errorMsg, commentId, commentText);
                }


                if (saved)
                {
                    ids += commentId.ToString() + ",";
                }
            }
            catch (Exception ex)
            {
                LogUtility.LogException(ex);
                errorMsg += ": " + ex.Message;
            }
        }

        ids = ids.TrimEnd(new char[] { ',' });
        if (ids.Length > 0)
        {
            saved = true;
        }

        result["saved"] = saved.ToString();
        result["ids"]   = ids;
        result["error"] = errorMsg;

        return(JsonConvert.SerializeObject(result, Formatting.None));
    }