AddComment add comment
(ctx context.Context, req *schema.AddCommentReq)
| 132 | |
| 133 | // AddComment add comment |
| 134 | func (cs *CommentService) AddComment(ctx context.Context, req *schema.AddCommentReq) ( |
| 135 | resp *schema.GetCommentResp, err error) { |
| 136 | comment := &entity.Comment{} |
| 137 | _ = copier.Copy(comment, req) |
| 138 | comment.Status = entity.CommentStatusAvailable |
| 139 | |
| 140 | objInfo, err := cs.objectInfoService.GetInfo(ctx, req.ObjectID) |
| 141 | if err != nil { |
| 142 | return nil, err |
| 143 | } |
| 144 | if objInfo.IsDeleted() { |
| 145 | return nil, errors.BadRequest(reason.NewObjectAlreadyDeleted) |
| 146 | } |
| 147 | objInfo.ObjectID = uid.DeShortID(objInfo.ObjectID) |
| 148 | objInfo.QuestionID = uid.DeShortID(objInfo.QuestionID) |
| 149 | objInfo.AnswerID = uid.DeShortID(objInfo.AnswerID) |
| 150 | if objInfo.ObjectType == constant.QuestionObjectType || objInfo.ObjectType == constant.AnswerObjectType { |
| 151 | comment.QuestionID = objInfo.QuestionID |
| 152 | } |
| 153 | |
| 154 | if len(req.ReplyCommentID) > 0 { |
| 155 | replyComment, exist, err := cs.commentCommonRepo.GetComment(ctx, req.ReplyCommentID) |
| 156 | if err != nil { |
| 157 | return nil, err |
| 158 | } |
| 159 | if !exist { |
| 160 | return nil, errors.BadRequest(reason.CommentNotFound) |
| 161 | } |
| 162 | comment.SetReplyUserID(replyComment.UserID) |
| 163 | comment.SetReplyCommentID(replyComment.ID) |
| 164 | } else { |
| 165 | comment.SetReplyUserID("") |
| 166 | comment.SetReplyCommentID("") |
| 167 | } |
| 168 | |
| 169 | err = cs.commentRepo.AddComment(ctx, comment) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | |
| 174 | comment.Status = cs.reviewService.AddCommentReview(ctx, comment, req.IP, req.UserAgent) |
| 175 | if err := cs.commentRepo.UpdateCommentStatus(ctx, comment.ID, comment.Status); err != nil { |
| 176 | return nil, err |
| 177 | } |
| 178 | |
| 179 | resp = &schema.GetCommentResp{} |
| 180 | resp.SetFromComment(comment) |
| 181 | resp.MemberActions = permission.GetCommentPermission(ctx, req.UserID, resp.UserID, |
| 182 | time.Now(), req.CanEdit, req.CanDelete) |
| 183 | |
| 184 | if comment.Status == entity.CommentStatusAvailable { |
| 185 | if err := cs.addCommentNotification(ctx, req, resp, comment, objInfo); err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // get user info |
| 191 | userInfo, exist, err := cs.userCommon.GetUserBasicInfoByID(ctx, resp.UserID) |
nothing calls this directly
no test coverage detected