Skip to content

Commit d15d55a

Browse files
committed
fix(answer): update QuestionID handling in answer update process
1 parent 2b64983 commit d15d55a

3 files changed

Lines changed: 9 additions & 12 deletions

File tree

internal/controller/answer_controller.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ func (ac *AnswerController) UpdateAnswer(ctx *gin.Context) {
318318
handler.HandleResponse(ctx, err, nil)
319319
return
320320
}
321-
req.QuestionID = uid.DeShortID(req.QuestionID)
322321
linkUrlLimitUser := canList[2]
323322
isAdmin := middleware.GetUserIsAdminModerator(ctx)
324323
if !isAdmin || !linkUrlLimitUser {

internal/schema/answer_schema.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ type GetAnswerInfoResp struct {
7878

7979
type AnswerUpdateReq struct {
8080
ID string `json:"id"`
81-
QuestionID string `json:"question_id"`
8281
Title string `json:"title"`
8382
Content string `validate:"required,notblank,gte=6,lte=65535" json:"content"`
8483
EditSummary string `validate:"omitempty" json:"edit_summary"`

internal/service/content/answer_service.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,24 +346,23 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
346346
return "", errors.BadRequest(reason.AnswerCannotUpdate)
347347
}
348348

349-
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, req.QuestionID)
349+
answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.ID)
350350
if err != nil {
351351
return "", err
352352
}
353353
if !exist {
354-
return "", errors.BadRequest(reason.QuestionNotFound)
354+
return "", errors.BadRequest(reason.AnswerNotFound)
355+
}
356+
if answerInfo.Status == entity.AnswerStatusDeleted {
357+
return "", errors.BadRequest(reason.AnswerCannotUpdate)
355358
}
356359

357-
answerInfo, exist, err := as.answerRepo.GetByID(ctx, req.ID)
360+
questionInfo, exist, err := as.questionRepo.GetQuestion(ctx, answerInfo.QuestionID)
358361
if err != nil {
359362
return "", err
360363
}
361364
if !exist {
362-
return "", errors.BadRequest(reason.AnswerNotFound)
363-
}
364-
365-
if answerInfo.Status == entity.AnswerStatusDeleted {
366-
return "", errors.BadRequest(reason.AnswerCannotUpdate)
365+
return "", errors.BadRequest(reason.QuestionNotFound)
367366
}
368367

369368
//If the content is the same, ignore it
@@ -374,7 +373,7 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
374373
insertData := &entity.Answer{}
375374
insertData.ID = req.ID
376375
insertData.UserID = answerInfo.UserID
377-
insertData.QuestionID = req.QuestionID
376+
insertData.QuestionID = questionInfo.ID
378377
insertData.OriginalText = req.Content
379378
insertData.ParsedText = req.HTML
380379
insertData.UpdatedAt = time.Now()
@@ -403,7 +402,7 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq
403402
if err = as.answerRepo.UpdateAnswer(ctx, insertData, []string{"original_text", "parsed_text", "updated_at", "last_edit_user_id"}); err != nil {
404403
return "", err
405404
}
406-
err = as.questionCommon.UpdatePostTime(ctx, req.QuestionID)
405+
err = as.questionCommon.UpdatePostTime(ctx, questionInfo.ID)
407406
if err != nil {
408407
return insertData.ID, err
409408
}

0 commit comments

Comments
 (0)