AddOrUpdateReaction add or update reaction
(ctx context.Context, req *schema.UpdateReactionReq)
| 91 | |
| 92 | // AddOrUpdateReaction add or update reaction |
| 93 | func (ms *MetaService) AddOrUpdateReaction(ctx context.Context, req *schema.UpdateReactionReq) (resp *schema.GetReactionByObjectIdResp, err error) { |
| 94 | // check if object exist and it's answer or question |
| 95 | objectType, err := obj.GetObjectTypeStrByObjectID(req.ObjectID) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | var event *schema.EventMsg |
| 100 | switch objectType { |
| 101 | case constant.AnswerObjectType: |
| 102 | answerInfo, exist, err := ms.answerRepo.GetAnswer(ctx, req.ObjectID) |
| 103 | if err != nil { |
| 104 | return nil, err |
| 105 | } |
| 106 | if !exist { |
| 107 | return nil, myErrors.BadRequest(reason.AnswerNotFound) |
| 108 | } |
| 109 | event = schema.NewEvent(constant.EventAnswerReact, req.UserID).TID(answerInfo.ID). |
| 110 | AID(answerInfo.ID, answerInfo.UserID) |
| 111 | case constant.QuestionObjectType: |
| 112 | questionInfo, exist, err := ms.questionRepo.GetQuestion(ctx, req.ObjectID) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | if !exist { |
| 117 | return nil, myErrors.BadRequest(reason.QuestionNotFound) |
| 118 | } |
| 119 | event = schema.NewEvent(constant.EventQuestionReact, req.UserID).TID(questionInfo.ID). |
| 120 | QID(questionInfo.ID, questionInfo.UserID) |
| 121 | default: |
| 122 | return nil, myErrors.BadRequest(reason.ObjectNotFound) |
| 123 | } |
| 124 | |
| 125 | // add or update |
| 126 | reactions := &schema.ReactionsSummaryMeta{} |
| 127 | err = ms.metaCommonService.AddOrUpdateMetaByObjectIdAndKey(ctx, req.ObjectID, entity.ObjectReactSummaryKey, func(meta *entity.Meta, exist bool) (*entity.Meta, error) { |
| 128 | // if not exist, create new one |
| 129 | if exist { |
| 130 | _ = json.Unmarshal([]byte(meta.Value), reactions) |
| 131 | } |
| 132 | |
| 133 | // update reaction |
| 134 | ms.updateReaction(req, reactions) |
| 135 | |
| 136 | // write back to meta repo |
| 137 | reactSumBytes, err := json.Marshal(reactions) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | |
| 142 | return &entity.Meta{ |
| 143 | ObjectID: req.ObjectID, |
| 144 | Key: entity.ObjectReactSummaryKey, |
| 145 | Value: string(reactSumBytes), |
| 146 | }, nil |
| 147 | }) |
| 148 | |
| 149 | if err != nil { |
| 150 | return nil, myErrors.InternalServer(reason.DatabaseError).WithError(err) |
nothing calls this directly
no test coverage detected