Merge branch 'fix/1.1.3/qa' into test

This commit is contained in:
LinkinStars 2023-08-28 17:33:15 +08:00
commit 2641d3e48a
6 changed files with 68 additions and 79 deletions

View File

@ -319,11 +319,11 @@ func (ac *AnswerController) AnswerList(ctx *gin.Context) {
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Param data body schema.AnswerAcceptedReq true "AnswerAcceptedReq" // @Param data body schema.AcceptAnswerReq true "AcceptAnswerReq"
// @Success 200 {string} string "" // @Success 200 {string} string ""
// @Router /answer/api/v1/answer/acceptance [post] // @Router /answer/api/v1/answer/acceptance [post]
func (ac *AnswerController) Accepted(ctx *gin.Context) { func (ac *AnswerController) Accepted(ctx *gin.Context) {
req := &schema.AnswerAcceptedReq{} req := &schema.AcceptAnswerReq{}
if handler.BindAndCheck(ctx, req) { if handler.BindAndCheck(ctx, req) {
return return
} }
@ -341,7 +341,7 @@ func (ac *AnswerController) Accepted(ctx *gin.Context) {
return return
} }
err = ac.answerService.UpdateAccepted(ctx, req) err = ac.answerService.AcceptAnswer(ctx, req)
handler.HandleResponse(ctx, err, nil) handler.HandleResponse(ctx, err, nil)
} }

View File

@ -55,7 +55,6 @@ func (ar *AnswerActivityRepo) SaveAcceptAnswerActivity(ctx context.Context, op *
return nil return nil
} }
ar.data.DB.ShowSQL(true)
// save activity // save activity
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) { _, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
session = session.Context(ctx) session = session.Context(ctx)
@ -331,7 +330,7 @@ func (ar *AnswerActivityRepo) sendAcceptAnswerNotification(
ObjectID: op.AnswerObjectID, ObjectID: op.AnswerObjectID,
} }
if act.ActivityUserID != op.QuestionUserID { if act.ActivityUserID != op.QuestionUserID {
msg.TriggerUserID = op.QuestionUserID msg.TriggerUserID = op.TriggerUserID
msg.ObjectType = constant.AnswerObjectType msg.ObjectType = constant.AnswerObjectType
msg.NotificationAction = constant.NotificationAcceptAnswer msg.NotificationAction = constant.NotificationAcceptAnswer
ar.notificationQueueService.Send(ctx, msg) ar.notificationQueueService.Send(ctx, msg)

View File

@ -166,30 +166,29 @@ func (ar *answerRepo) GetAnswerPage(ctx context.Context, page, pageSize int, ans
return return
} }
// UpdateAccepted // UpdateAcceptedStatus update all accepted status of this question's answers
// If no answer is selected, the answer id can be 0 func (ar *answerRepo) UpdateAcceptedStatus(ctx context.Context, acceptedAnswerID string, questionID string) error {
func (ar *answerRepo) UpdateAccepted(ctx context.Context, id string, questionID string) error { acceptedAnswerID = uid.DeShortID(acceptedAnswerID)
if questionID == "" {
return nil
}
id = uid.DeShortID(id)
questionID = uid.DeShortID(questionID) questionID = uid.DeShortID(questionID)
var data entity.Answer
data.ID = id
data.Accepted = schema.AnswerAcceptedFailed // update all this question's answer accepted status to false
_, err := ar.data.DB.Context(ctx).Where("question_id =?", questionID).Cols("adopted").Update(&data) _, err := ar.data.DB.Context(ctx).Where("question_id = ?", questionID).Cols("adopted").Update(&entity.Answer{
Accepted: schema.AnswerAcceptedFailed,
})
if err != nil { if err != nil {
return err return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
if id != "0" {
data.Accepted = schema.AnswerAcceptedEnable // if acceptedAnswerID is not empty, update accepted status to true
_, err = ar.data.DB.Context(ctx).Where("id = ?", id).Cols("adopted").Update(&data) if len(acceptedAnswerID) > 0 && acceptedAnswerID != "0" {
_, err = ar.data.DB.Context(ctx).Where("id = ?", acceptedAnswerID).Cols("adopted").Update(&entity.Answer{
Accepted: schema.AnswerAcceptedEnable,
})
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
} }
_ = ar.updateSearch(ctx, id) _ = ar.updateSearch(ctx, acceptedAnswerID)
return nil return nil
} }

View File

@ -108,12 +108,19 @@ type AdminAnswerInfo struct {
} `json:"question_info"` } `json:"question_info"`
} }
type AnswerAcceptedReq struct { type AcceptAnswerReq struct {
QuestionID string `json:"question_id"` QuestionID string `validate:"required,gt=0,lte=30" json:"question_id"`
AnswerID string `json:"answer_id"` AnswerID string `validate:"omitempty" json:"answer_id"`
UserID string `json:"-"` UserID string `json:"-"`
} }
func (req *AcceptAnswerReq) Check() (errFields []*validator.FormErrorField, err error) {
if len(req.AnswerID) == 0 {
req.AnswerID = "0"
}
return nil, nil
}
type AdminSetAnswerStatusRequest struct { type AdminSetAnswerStatusRequest struct {
StatusStr string `json:"status"` StatusStr string `json:"status"`
AnswerID string `json:"answer_id"` AnswerID string `json:"answer_id"`

View File

@ -17,7 +17,7 @@ type AnswerRepo interface {
GetAnswer(ctx context.Context, id string) (answer *entity.Answer, exist bool, err error) GetAnswer(ctx context.Context, id string) (answer *entity.Answer, exist bool, err error)
GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error) GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error)
GetAnswerPage(ctx context.Context, page, pageSize int, answer *entity.Answer) (answerList []*entity.Answer, total int64, err error) GetAnswerPage(ctx context.Context, page, pageSize int, answer *entity.Answer) (answerList []*entity.Answer, total int64, err error)
UpdateAccepted(ctx context.Context, id string, questionID string) error UpdateAcceptedStatus(ctx context.Context, acceptedAnswerID string, questionID string) error
GetByID(ctx context.Context, id string) (*entity.Answer, bool, error) GetByID(ctx context.Context, id string) (*entity.Answer, bool, error)
GetCountByQuestionID(ctx context.Context, questionID string) (int64, error) GetCountByQuestionID(ctx context.Context, questionID string) (int64, error)
GetCountByUserID(ctx context.Context, userID string) (int64, error) GetCountByUserID(ctx context.Context, userID string) (int64, error)

View File

@ -230,15 +230,13 @@ func (as *AnswerService) Insert(ctx context.Context, req *schema.AnswerAddReq) (
} }
func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq) (string, error) { func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq) (string, error) {
//req.NoNeedReview //true 不需要审核
var canUpdate bool var canUpdate bool
_, existUnreviewed, err := as.revisionService.ExistUnreviewedByObjectID(ctx, req.ID) _, existUnreviewed, err := as.revisionService.ExistUnreviewedByObjectID(ctx, req.ID)
if err != nil { if err != nil {
return "", err return "", err
} }
if existUnreviewed { if existUnreviewed {
err = errors.BadRequest(reason.AnswerCannotUpdate) return "", errors.BadRequest(reason.AnswerCannotUpdate)
return "", err
} }
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID) questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID)
@ -254,12 +252,11 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
return "", err return "", err
} }
if !exist { if !exist {
return "", nil return "", errors.BadRequest(reason.AnswerNotFound)
} }
if answerInfo.Status == entity.AnswerStatusDeleted { if answerInfo.Status == entity.AnswerStatusDeleted {
err = errors.BadRequest(reason.AnswerCannotUpdate) return "", errors.BadRequest(reason.AnswerCannotUpdate)
return "", err
} }
//If the content is the same, ignore it //If the content is the same, ignore it
@ -267,15 +264,13 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
return "", nil return "", nil
} }
now := time.Now() insertData := &entity.Answer{}
insertData := new(entity.Answer)
insertData.ID = req.ID insertData.ID = req.ID
insertData.UserID = answerInfo.UserID insertData.UserID = answerInfo.UserID
insertData.QuestionID = req.QuestionID insertData.QuestionID = req.QuestionID
insertData.OriginalText = req.Content insertData.OriginalText = req.Content
insertData.ParsedText = req.HTML insertData.ParsedText = req.HTML
insertData.UpdatedAt = now insertData.UpdatedAt = time.Now()
insertData.LastEditUserID = "0" insertData.LastEditUserID = "0"
if answerInfo.UserID != req.UserID { if answerInfo.UserID != req.UserID {
insertData.LastEditUserID = req.UserID insertData.LastEditUserID = req.UserID
@ -284,7 +279,6 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
revisionDTO := &schema.AddRevisionDTO{ revisionDTO := &schema.AddRevisionDTO{
UserID: req.UserID, UserID: req.UserID,
ObjectID: req.ID, ObjectID: req.ID,
Title: "",
Log: req.EditSummary, Log: req.EditSummary,
} }
@ -314,7 +308,7 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
} }
if canUpdate { if canUpdate {
as.activityQueueService.Send(ctx, &schema.ActivityMsg{ as.activityQueueService.Send(ctx, &schema.ActivityMsg{
UserID: insertData.UserID, UserID: req.UserID,
ObjectID: insertData.ID, ObjectID: insertData.ID,
OriginalObjectID: insertData.ID, OriginalObjectID: insertData.ID,
ActivityTypeKey: constant.ActAnswerEdited, ActivityTypeKey: constant.ActAnswerEdited,
@ -325,47 +319,47 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
return insertData.ID, nil return insertData.ID, nil
} }
// UpdateAccepted // AcceptAnswer accept answer
func (as *AnswerService) UpdateAccepted(ctx context.Context, req *schema.AnswerAcceptedReq) error { func (as *AnswerService) AcceptAnswer(ctx context.Context, req *schema.AcceptAnswerReq) (err error) {
if req.AnswerID == "" { // find question
req.AnswerID = "0"
}
if req.UserID == "" {
return nil
}
newAnswerInfo := &entity.Answer{}
newAnswerInfoexist := false
var err error
if req.AnswerID != "0" {
newAnswerInfo, newAnswerInfoexist, err = as.answerRepo.GetByID(ctx, req.AnswerID)
if err != nil {
return err
}
newAnswerInfo.ID = uid.DeShortID(newAnswerInfo.ID)
if !newAnswerInfoexist {
return errors.BadRequest(reason.AnswerNotFound)
}
}
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID) questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID)
if err != nil { if err != nil {
return err return err
} }
questionInfo.ID = uid.DeShortID(questionInfo.ID)
if !exist { if !exist {
return errors.BadRequest(reason.QuestionNotFound) return errors.BadRequest(reason.QuestionNotFound)
} }
// if questionInfo.UserID != req.UserID { questionInfo.ID = uid.DeShortID(questionInfo.ID)
// return fmt.Errorf("no permission to set answer")
// }
if questionInfo.AcceptedAnswerID == req.AnswerID { if questionInfo.AcceptedAnswerID == req.AnswerID {
return nil return nil
} }
// find answer
var acceptedAnswerInfo *entity.Answer
if len(req.AnswerID) > 1 {
acceptedAnswerInfo, exist, err = as.answerRepo.GetByID(ctx, req.AnswerID)
if err != nil {
return err
}
if !exist {
return errors.BadRequest(reason.AnswerNotFound)
}
acceptedAnswerInfo.ID = uid.DeShortID(acceptedAnswerInfo.ID)
}
// update answers status
if err = as.answerRepo.UpdateAcceptedStatus(ctx, req.AnswerID, req.QuestionID); err != nil {
return err
}
// update question status
err = as.questionCommon.UpdateAccepted(ctx, req.QuestionID, req.AnswerID)
if err != nil {
log.Error("UpdateLastAnswer error", err.Error())
}
var oldAnswerInfo *entity.Answer var oldAnswerInfo *entity.Answer
if len(questionInfo.AcceptedAnswerID) > 0 && questionInfo.AcceptedAnswerID != "0" { if len(questionInfo.AcceptedAnswerID) > 1 {
oldAnswerInfo, _, err = as.answerRepo.GetByID(ctx, questionInfo.AcceptedAnswerID) oldAnswerInfo, _, err = as.answerRepo.GetByID(ctx, questionInfo.AcceptedAnswerID)
if err != nil { if err != nil {
return err return err
@ -373,17 +367,7 @@ func (as *AnswerService) UpdateAccepted(ctx context.Context, req *schema.AnswerA
oldAnswerInfo.ID = uid.DeShortID(oldAnswerInfo.ID) oldAnswerInfo.ID = uid.DeShortID(oldAnswerInfo.ID)
} }
err = as.answerRepo.UpdateAccepted(ctx, req.AnswerID, req.QuestionID) as.updateAnswerRank(ctx, req.UserID, questionInfo, acceptedAnswerInfo, oldAnswerInfo)
if err != nil {
return err
}
err = as.questionCommon.UpdateAccepted(ctx, req.QuestionID, req.AnswerID)
if err != nil {
log.Error("UpdateLastAnswer error", err.Error())
}
as.updateAnswerRank(ctx, req.UserID, questionInfo, newAnswerInfo, oldAnswerInfo)
return nil return nil
} }
@ -398,9 +382,9 @@ func (as *AnswerService) updateAnswerRank(ctx context.Context, userID string,
log.Error(err) log.Error(err)
} }
} }
if newAnswerInfo.ID != "" { if newAnswerInfo != nil {
err := as.answerActivityService.AcceptAnswer(ctx, userID, newAnswerInfo.ID, err := as.answerActivityService.AcceptAnswer(ctx, userID, newAnswerInfo.ID,
questionInfo.ID, questionInfo.UserID, newAnswerInfo.UserID, newAnswerInfo.UserID == userID) questionInfo.ID, questionInfo.UserID, newAnswerInfo.UserID, newAnswerInfo.UserID == questionInfo.UserID)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
} }