From c29d16cc12c14f047743610669ae8edced100d22 Mon Sep 17 00:00:00 2001 From: Pavel Date: Tue, 16 Apr 2024 14:35:25 +0300 Subject: [PATCH] change some method GetMapQuestions --- repository/question/question.go | 49 ++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/repository/question/question.go b/repository/question/question.go index 9e4cf1c..88415a2 100644 --- a/repository/question/question.go +++ b/repository/question/question.go @@ -8,6 +8,7 @@ import ( "github.com/lib/pq" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/dal/sqlcgen" "penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model" + "sort" "strings" "sync" "time" @@ -360,29 +361,57 @@ func (r *QuestionRepository) QuestionHistory(ctx context.Context, id, limit, off return result, nil } -func (r *QuestionRepository) GetMapQuestions(ctx context.Context, allAnswers []model.ResultAnswer) (map[uint64]string, error) { +func (r *QuestionRepository) GetMapQuestions(ctx context.Context, allAnswers []model.ResultAnswer) (map[uint64]string, []model.ResultAnswer, error) { questionMap := make(map[uint64]string) + var questions []QueTitleResp for _, answer := range allAnswers { - - title, questionType, err := r.GetQuestionTitleByID(ctx, answer.QuestionID) + questionData, err := r.GetQuestionTitleByID(ctx, answer.QuestionID) if err != nil { - return nil, err + return nil, []model.ResultAnswer{}, err } - if questionType != model.TypeResult { - questionMap[answer.AnswerID] = title + if questionData.Type != model.TypeResult { + questions = append(questions, questionData) + questionMap[answer.AnswerID] = questionData.Title + questionData.AnswerID = answer.AnswerID + } + } + sort.Slice(questions, func(i, j int) bool { + return questions[i].Page < questions[j].Page + }) + + // TODO O2 REFACTOR + var sortedAllAnswers []model.ResultAnswer + for _, que := range questions { + for _, answer := range allAnswers { + if que.AnswerID == answer.AnswerID { + sortedAllAnswers = append(sortedAllAnswers, answer) + } } } - return questionMap, nil + return questionMap, sortedAllAnswers, nil +} + +type QueTitleResp struct { + Title string + Type string + Page int + AnswerID uint64 } // test + -func (r *QuestionRepository) GetQuestionTitleByID(ctx context.Context, questionID uint64) (string, string, error) { +func (r *QuestionRepository) GetQuestionTitleByID(ctx context.Context, questionID uint64) (QueTitleResp, error) { row, err := r.queries.GetQuestionTitle(ctx, int64(questionID)) if err != nil { - return "", "", err + return QueTitleResp{}, err } - return row.Title, string(row.Questiontype.([]byte)), nil + resp := QueTitleResp{ + Title: row.Title, + Type: string(row.Questiontype.([]byte)), + Page: int(row.Page.Int16), + } + + return resp, nil }