From b700d5deffa7c7cebddbfab371a3677a6775f74c Mon Sep 17 00:00:00 2001 From: hanwoong kim Date: Sun, 25 Feb 2024 21:25:26 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=A0=9C=EA=B1=B0=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/input.js | 61 -------------------- db_review.go | 43 ++++++++++++++ handler.go | 5 +- restapiReview.go | 140 +++------------------------------------------ 4 files changed, 54 insertions(+), 195 deletions(-) diff --git a/assets/js/input.js b/assets/js/input.js index b11ee73..8f81ee1 100644 --- a/assets/js/input.js +++ b/assets/js/input.js @@ -4353,9 +4353,6 @@ function addReviewStatusMode() { }); } -function clickCommentButton() { - addReviewComment() -} function clickReviewStatusModeCommentButton() { addReviewStatusModeComment() @@ -4680,64 +4677,6 @@ function setReviewName() { } -function addReviewComment() { - $.ajax({ - url: "/api/addreviewcomment", - type: "POST", - data: { - id: document.getElementById("current-review-id").value, - text: document.getElementById("review-comment").value, - media: document.getElementById("review-media").value, - frame: document.getElementById("currentframe").innerHTML, - framecomment: document.getElementById("review-framecomment").checked, - }, - headers: { - "Authorization": "Basic "+ document.getElementById("token").value - }, - dataType: "json", - success: function(data) { - // 데이터가 잘 들어가면 review-comments 에 들어간 데이터를 드로잉한다. - let body = data.text.replace(/(?:\r\n|\r|\n)/g, '
'); - let newComment = `
- ${data.date} / ${data.authorname} - - × -
- ` - if (data.framecomment) { - newComment += `${data.frame}f / ${data.frame+data.productionstartframe-1}f` - } - newComment += `${body}` - if (data.media != "") { - if (data.media.includes("http")) { - newComment += `
- - - - ${data.mediatitle} -
` - } else { - newComment += `
- - - - ${data.mediatitle} -
` - } - } - newComment += `
` - document.getElementById("review-comments").innerHTML = newComment + document.getElementById("review-comments").innerHTML; - // 입력한 값을 초기화 한다. - document.getElementById("review-comment").value = ""; - document.getElementById("review-media").value = ""; - document.getElementById("review-framecomment").checked = false; - }, - error: function(request,status,error){ - alert("code:"+request.status+"\n"+"status:"+status+"\n"+"msg:"+request.responseText+"\n"+"error:"+error); - } - }); -} - diff --git a/db_review.go b/db_review.go index 2b15374..f7bbd98 100644 --- a/db_review.go +++ b/db_review.go @@ -243,3 +243,46 @@ func addReviewCommentV2(client *mongo.Client, id string, cmt Comment) error { } return nil } + +func EditReviewCommentV2(client *mongo.Client, id, date, text, media string, frame int) error { + + reviewItem, err := getReviewV2(client, id) + if err != nil { + return err + } + var newComments []Comment + for _, comment := range reviewItem.Comments { + if comment.Date == date { + comment.Text = text + comment.Media = media + comment.Frame = frame + } + newComments = append(newComments, comment) + } + reviewItem.Comments = newComments + err = setReviewItemV2(client, reviewItem) + if err != nil { + return err + } + return nil +} + +func RmReviewCommentV2(client *mongo.Client, id, date string) error { + reviewItem, err := getReviewV2(client, id) + if err != nil { + return err + } + var newComments []Comment + for _, comment := range reviewItem.Comments { + if comment.Date == date { + continue + } + newComments = append(newComments, comment) + } + reviewItem.Comments = newComments + err = setReviewItemV2(client, reviewItem) + if err != nil { + return err + } + return nil +} diff --git a/handler.go b/handler.go index 04d99ce..9f8f4ea 100755 --- a/handler.go +++ b/handler.go @@ -567,10 +567,9 @@ func webserver(port string) { r.HandleFunc("/api/review", handleAPIReview).Methods("POST") r.HandleFunc("/api/searchreview", handleAPISearchReview) r.HandleFunc("/api/setreviewitemstatus", handleAPISetReviewItemStatus).Methods("POST") - r.HandleFunc("/api/addreviewcomment", handleAPIAddReviewComment) r.HandleFunc("/api/addreviewstatusmodecomment", handleAPIAddReviewStatusModeComment).Methods("POST") - r.HandleFunc("/api/editreviewcomment", handleAPIEditReviewComment) - r.HandleFunc("/api/rmreviewcomment", handleAPIRmReviewComment) + r.HandleFunc("/api/editreviewcomment", handleAPIEditReviewComment).Methods("POST") + r.HandleFunc("/api/rmreviewcomment", handleAPIRmReviewComment).Methods("POST") r.HandleFunc("/api/rmreview", handleAPIRmReview) r.HandleFunc("/api/setreviewproject", handleAPISetReviewProject) r.HandleFunc("/api/setreviewtask", handleAPISetReviewTask) diff --git a/restapiReview.go b/restapiReview.go index dc5dcfb..e5cf192 100644 --- a/restapiReview.go +++ b/restapiReview.go @@ -312,113 +312,6 @@ func handleAPISetReviewItemStatus(w http.ResponseWriter, r *http.Request) { w.Write(data) } -// handleAPIAddReviewComment 함수는 review에 comment를 설정하는 RestAPI 이다. -func handleAPIAddReviewComment(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, "Post Only", http.StatusMethodNotAllowed) - return - } - type Recipe struct { - UserID string `json:"userid"` - ID string `json:"id"` - Text string `json:"text"` - Media string `json:"media"` - MediaTitle string `json:"mediatitle"` - Author string `json:"author"` - AuthorName string `json:"authorname"` - Date string `json:"date"` - Frame int `json:"frame"` - FrameComment bool `json:"framecomment"` - ProductionStartFrame int `json:"productionstartframe"` // UX 를 그릴 때 필요하다. - Protocol string `json:"protocol"` - } - rcp := Recipe{} - rcp.Protocol = CachedAdminSetting.Protocol - rcp.ProductionStartFrame = CachedAdminSetting.ProductionStartFrame - session, err := mgo.Dial(*flagDBIP) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - defer session.Close() - rcp.UserID, _, err = TokenHandler(r, session) - if err != nil { - http.Error(w, err.Error(), http.StatusUnauthorized) - return - } - _, _, err = net.SplitHostPort(r.RemoteAddr) - if err != nil { - http.Error(w, err.Error(), http.StatusUnauthorized) - return - } - // 사용자의 이름을 구한다. - u, err := getUser(session, rcp.UserID) - if err != nil { - http.Error(w, err.Error(), http.StatusUnauthorized) // 사용자가 존재하지 않으면 당연히 Comment를 작성하면 안된다. - return - } - rcp.AuthorName = u.LastNameKor + u.FirstNameKor - r.ParseForm() - id := r.FormValue("id") - if id == "" { - http.Error(w, "need id", http.StatusBadRequest) - return - } - rcp.ID = id - rcp.Text = r.FormValue("text") - rcp.Media = r.FormValue("media") - if rcp.Text == "" && rcp.Media == "" { - http.Error(w, "comment(text) 또는 첨부파일(media) 값 둘중 하나는 반드시 입력되어야 합니다", http.StatusBadRequest) - return - } - rcp.MediaTitle = r.FormValue("mediatitle") - - review, err := getReview(session, rcp.ID) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - rcp.FrameComment = str2bool(r.FormValue("framecomment")) - frame, err := strconv.Atoi(r.FormValue("frame")) - if err != nil { - frame = 0 - } - if rcp.FrameComment { - rcp.Frame = frame - } - cmt := Comment{} - cmt.Date = time.Now().Format(time.RFC3339) - rcp.Date = cmt.Date - cmt.Author = rcp.UserID - rcp.Author = rcp.UserID - cmt.AuthorName = rcp.AuthorName - cmt.Text = rcp.Text - cmt.Media = rcp.Media - cmt.MediaTitle = rcp.MediaTitle - cmt.Frame = rcp.Frame - - err = addReviewComment(session, rcp.ID, cmt) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - // slack log - err = slacklog(session, review.Project, fmt.Sprintf("Add Review Comment: %s, \nProject: %s, Name: %s, Author: %s", rcp.Text, review.Project, review.Name, rcp.UserID)) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - data, err := json.Marshal(rcp) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json; charset=utf-8") - w.WriteHeader(http.StatusOK) - w.Write(data) -} - // handleAPIAddReviewStatusModeComment 함수는 review status mode에서 comment를 설정하는 RestAPI 이다. func handleAPIAddReviewStatusModeComment(w http.ResponseWriter, r *http.Request) { type Recipe struct { @@ -530,10 +423,6 @@ func handleAPIAddReviewStatusModeComment(w http.ResponseWriter, r *http.Request) // handleAPIEditReviewComment 함수는 리뷰에서 코멘트를 수정합니다. func handleAPIEditReviewComment(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, "Post Only", http.StatusMethodNotAllowed) - return - } type Recipe struct { ID string `json:"id"` Time string `json:"time"` @@ -547,13 +436,13 @@ func handleAPIEditReviewComment(w http.ResponseWriter, r *http.Request) { rcp := Recipe{} rcp.Protocol = CachedAdminSetting.Protocol rcp.ProductionStartFrame = CachedAdminSetting.ProductionStartFrame - session, err := mgo.Dial(*flagDBIP) + client, err := initMongoClient() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - defer session.Close() - rcp.UserID, _, err = TokenHandler(r, session) + defer client.Disconnect(context.Background()) + rcp.UserID, _, err = TokenHandlerV2(r, client) if err != nil { http.Error(w, err.Error(), http.StatusUnauthorized) return @@ -585,7 +474,7 @@ func handleAPIEditReviewComment(w http.ResponseWriter, r *http.Request) { return } rcp.Frame = frame - err = EditReviewComment(session, rcp.ID, rcp.Time, rcp.Text, rcp.Media, rcp.Frame) + err = EditReviewCommentV2(client, rcp.ID, rcp.Time, rcp.Text, rcp.Media, rcp.Frame) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -603,10 +492,6 @@ func handleAPIEditReviewComment(w http.ResponseWriter, r *http.Request) { // handleAPIRmReviewComment 함수는 리뷰에서 수정사항을 삭제합니다. func handleAPIRmReviewComment(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, "Post Only", http.StatusMethodNotAllowed) - return - } type Recipe struct { ID string `json:"id"` Time string `json:"time"` @@ -616,13 +501,13 @@ func handleAPIRmReviewComment(w http.ResponseWriter, r *http.Request) { UserID string `json:"userid"` } rcp := Recipe{} - session, err := mgo.Dial(*flagDBIP) + client, err := initMongoClient() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } - defer session.Close() - rcp.UserID, _, err = TokenHandler(r, session) + defer client.Disconnect(context.Background()) + rcp.UserID, _, err = TokenHandlerV2(r, client) if err != nil { http.Error(w, err.Error(), http.StatusUnauthorized) return @@ -647,7 +532,7 @@ func handleAPIRmReviewComment(w http.ResponseWriter, r *http.Request) { rcp.Time = reviewTime // ID를 이용해서 삭제할 리뷰아이템을 가져와 Project, Name, Text를 반환될 json에 설정합니다. - review, err := getReview(session, rcp.ID) + review, err := getReviewV2(client, rcp.ID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -662,14 +547,7 @@ func handleAPIRmReviewComment(w http.ResponseWriter, r *http.Request) { } // 리뷰데이터를 삭제합니다. - err = RmReviewComment(session, rcp.ID, rcp.Time) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - // slack log - err = slacklog(session, rcp.Project, fmt.Sprintf("Rm Review Comment: %s\nProject: %s, Name: %s, Author: %s", rcp.Text, rcp.Project, rcp.Name, rcp.UserID)) + err = RmReviewCommentV2(client, rcp.ID, rcp.Time) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return