change some method GetMapQuestions

This commit is contained in:
Pavel 2024-04-16 14:35:25 +03:00
parent 6e6e28d492
commit c29d16cc12

@ -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
}