Skip to content

Commit

Permalink
using count instead of find to improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
hgaol committed Dec 4, 2023

Verified

This commit was signed with the committer’s verified signature.
coryan Carlos O'Ryan
1 parent adf5657 commit 8e65774
Showing 4 changed files with 13 additions and 18 deletions.
5 changes: 3 additions & 2 deletions internal/controller/answer_controller.go
Original file line number Diff line number Diff line change
@@ -245,15 +245,16 @@ func (ac *AnswerController) Add(ctx *gin.Context) {
write, err := ac.siteInfoCommonService.GetSiteWrite(ctx)
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
if write.RestrictAnswer {
// check if there's already an answer by this user
resp, err := ac.answerService.GetByUserIDQuestionID(ctx, req.UserID, req.QuestionID)
count, err := ac.answerService.GetCountByUserIDQuestionID(ctx, req.UserID, req.QuestionID)
if err != nil {
handler.HandleResponse(ctx, err, nil)
return
}
if len(resp) >= 1 {
if count >= 1 {
handler.HandleResponse(ctx, errors.Forbidden(reason.AnswerRestrictAnswer), nil)
return
}
16 changes: 5 additions & 11 deletions internal/repo/answer/answer_repo.go
Original file line number Diff line number Diff line change
@@ -289,20 +289,14 @@ func (ar *answerRepo) GetCountByUserID(ctx context.Context, userID string) (int6
return count, nil
}

func (ar *answerRepo) GetByUserIDQuestionID(ctx context.Context, userID string, questionID string) ([]*entity.Answer, error) {
func (ar *answerRepo) GetCountByUserIDQuestionID(ctx context.Context, userID string, questionID string) (count int64, err error) {
questionID = uid.DeShortID(questionID)
var resp = make([]*entity.Answer, 0)
err := ar.data.DB.Context(ctx).Where("question_id =? and user_id = ? and status = ?", questionID, userID, entity.AnswerStatusAvailable).Find(&resp)
var resp = new(entity.Answer)
count, err = ar.data.DB.Context(ctx).Where("question_id =? and user_id = ? and status = ?", questionID, userID, entity.AnswerStatusAvailable).Count(resp)
if err != nil {
return resp, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
if handler.GetEnableShortID(ctx) {
for _, item := range resp {
item.ID = uid.EnShortID(item.ID)
item.QuestionID = uid.EnShortID(item.QuestionID)
}
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return resp, nil
return count, nil
}

// SearchList
6 changes: 3 additions & 3 deletions internal/service/answer_common/answer.go
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ type AnswerRepo interface {
GetByID(ctx context.Context, answerID string) (*entity.Answer, bool, error)
GetCountByQuestionID(ctx context.Context, questionID string) (int64, error)
GetCountByUserID(ctx context.Context, userID string) (int64, error)
GetByUserIDQuestionID(ctx context.Context, userID string, questionID string) ([]*entity.Answer, error)
GetCountByUserIDQuestionID(ctx context.Context, userID string, questionID string) (count int64, err error)
SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)
AdminSearchList(ctx context.Context, search *schema.AdminAnswerPageReq) ([]*entity.Answer, int64, error)
UpdateAnswerStatus(ctx context.Context, answerID string, status int) (err error)
@@ -61,11 +61,11 @@ func NewAnswerCommon(answerRepo AnswerRepo) *AnswerCommon {
}

func (as *AnswerCommon) SearchAnswered(ctx context.Context, userID, questionID string) (bool, error) {
resp, err := as.answerRepo.GetByUserIDQuestionID(ctx, userID, questionID)
count, err := as.answerRepo.GetCountByUserIDQuestionID(ctx, userID, questionID)
if err != nil {
return false, err
}
return len(resp) > 0, nil
return count > 0, nil
}

func (as *AnswerCommon) AdminSearchList(ctx context.Context, req *schema.AdminAnswerPageReq) (
4 changes: 2 additions & 2 deletions internal/service/answer_service.go
Original file line number Diff line number Diff line change
@@ -495,8 +495,8 @@ func (as *AnswerService) Get(ctx context.Context, answerID, loginUserID string)
return info, questionInfo, has, nil
}

func (as *AnswerService) GetByUserIDQuestionID(ctx context.Context, userId string, questionId string) ([]*entity.Answer, error) {
return as.answerRepo.GetByUserIDQuestionID(ctx, userId, questionId)
func (as *AnswerService) GetCountByUserIDQuestionID(ctx context.Context, userId string, questionId string) (answerCount int64, err error) {
return as.answerRepo.GetCountByUserIDQuestionID(ctx, userId, questionId)
}

func (as *AnswerService) AdminSetAnswerStatus(ctx context.Context, req *schema.AdminUpdateAnswerStatusReq) error {

0 comments on commit 8e65774

Please sign in to comment.