fix v13
This commit is contained in:
@@ -30,6 +30,12 @@ type QuestionsStore interface {
|
||||
GetByUserID(ctx context.Context, userID uint, opts GetQuestionsByUserIDOptions) ([]*Question, error)
|
||||
GetByAskUserID(ctx context.Context, userID uint, opts GetQuestionsByAskUserIDOptions) ([]*Question, error)
|
||||
AnswerByID(ctx context.Context, id uint, answer string) error
|
||||
// AnswerByIDPrivate 写入回答的同时把题目置为私密(站方 L2「收窄公开」)。
|
||||
//
|
||||
// 做成单条 UPDATE 而不是「先 SetPrivate 再 AnswerByID」:
|
||||
// - 两条语句之间存在「公开 + 已作答」的窗口,而 L2 的全部意义就是消灭这个窗口;
|
||||
// - 若第二条失败,题目会变成「私密 + 无回答」,箱主等于凭空丢了一条内容。
|
||||
AnswerByIDPrivate(ctx context.Context, id uint, answer string) error
|
||||
DeleteByID(ctx context.Context, id uint) error
|
||||
UpdateCensor(ctx context.Context, id uint, opts UpdateQuestionCensorOptions) error
|
||||
Count(ctx context.Context, userID uint, opts GetQuestionsCountOptions) (int64, error)
|
||||
@@ -371,6 +377,23 @@ func (db *questions) getBy(ctx context.Context, cursor *dbutil.Cursor, whereQuer
|
||||
return questions, nil
|
||||
}
|
||||
|
||||
// AnswerByIDPrivate 回答并强制私密。见接口注释:必须是一条 UPDATE。
|
||||
func (db *questions) AnswerByIDPrivate(ctx context.Context, id uint, answer string) error {
|
||||
var question Question
|
||||
if err := db.WithContext(ctx).First(&question, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return ErrQuestionNotExist
|
||||
}
|
||||
return errors.Wrap(err, "get question by ID")
|
||||
}
|
||||
|
||||
if err := db.WithContext(ctx).Model(&question).Where("id = ?", id).
|
||||
Updates(map[string]interface{}{"answer": answer, "is_read": true, "is_private": true}).Error; err != nil {
|
||||
return errors.Wrap(err, "update question answer as private")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *questions) AnswerByID(ctx context.Context, id uint, answer string) error {
|
||||
var question Question
|
||||
if err := db.WithContext(ctx).First(&question, id).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user