diff --git a/internal/common/common.go b/internal/common/common.go index bf246a5..021c5a8 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -19,6 +19,8 @@ const ( notSet ) +const callbackDataMarshalErrorMessage = "Got error on marshalling callback data:" + type inlineButton struct { Text string `json:"text"` URL string `json:"url,omitempty"` @@ -86,7 +88,7 @@ func (task *BotLeetCodeTask) GetInlineKeyboard() string { for i := range task.Hints { callbackData, err := GetMarshalledCallbackData(task.DateID, i, HintReuqest) if err != nil { - fmt.Println("Got error on marshalling callback data:", err) + fmt.Println(callbackDataMarshalErrorMessage, err) } // 5 hints in the row @@ -114,7 +116,7 @@ func (task *BotLeetCodeTask) GetInlineKeyboard() string { // Append difficulty hint to task inline keyboard getDifficultyCallbackData, err := GetMarshalledCallbackData(task.DateID, 0, DifficultyRequest) if err != nil { - fmt.Println("Got error on marshalling callback data:", err) + fmt.Println(callbackDataMarshalErrorMessage, err) } listOfHints = append( listOfHints, @@ -128,7 +130,7 @@ func (task *BotLeetCodeTask) GetInlineKeyboard() string { getTopicTagsCallbackData, err := GetMarshalledCallbackData(task.DateID, 0, TopicTagsRequest) if err != nil { - fmt.Println("Got error on marshalling callback data:", err) + fmt.Println(callbackDataMarshalErrorMessage, err) } listOfHints = append( listOfHints, diff --git a/internal/storage/ydb.go b/internal/storage/ydb.go index 3b868a6..6b9cf32 100644 --- a/internal/storage/ydb.go +++ b/internal/storage/ydb.go @@ -99,6 +99,16 @@ type ydbStorage struct { ydbExecuter queryExecuter } +type databaseTaskRow struct { + title *string + content *string + questionID *uint64 + titleSlug *string + hints *string + difficulty *uint8 + topicTags *string +} + var initializedExecuter *ydbQueryExecuter var executerInitOnce sync.Once @@ -186,47 +196,18 @@ func (y *ydbStorage) getTask(ctx context.Context, dateID uint64) (common.BotLeet return common.BotLeetCodeTask{}, ErrNoSuchTask } - var ( - title *string - content *string - questionID *uint64 - titleSlug *string - hints *string - difficulty *uint8 - topicTags *string - ) - returnValue := common.BotLeetCodeTask{DateID: dateID} - for res.NextResultSet(ctx, "title", "content", "questionId", "titleSlug", "hints", "difficulty", "topicTags") { for res.NextRow() { - err = res.Scan( - &title, - &content, - &questionID, - &titleSlug, - &hints, - &difficulty, - &topicTags, - ) + row := databaseTaskRow{} + err = res.Scan(row.scanDestinations()...) if err != nil { break } - returnValue.Title = *title - returnValue.Content = *content - returnValue.QuestionID = *questionID - returnValue.TitleSlug = *titleSlug - returnValue.SetDifficultyFromNum(*difficulty) - err = json.Unmarshal([]byte(*hints), &returnValue.Hints) + returnValue, err = row.toBotLeetCodeTask(dateID) if err != nil { break } - if topicTags != nil && *topicTags != "" { - err = json.Unmarshal([]byte(*topicTags), &returnValue.TopicTags) - if err != nil { - break - } - } } if err != nil { return common.BotLeetCodeTask{}, err @@ -235,6 +216,39 @@ func (y *ydbStorage) getTask(ctx context.Context, dateID uint64) (common.BotLeet return returnValue, res.Err() } +func (row *databaseTaskRow) scanDestinations() []interface{} { + return []interface{}{ + &row.title, + &row.content, + &row.questionID, + &row.titleSlug, + &row.hints, + &row.difficulty, + &row.topicTags, + } +} + +func (row *databaseTaskRow) toBotLeetCodeTask(dateID uint64) (common.BotLeetCodeTask, error) { + task := common.BotLeetCodeTask{DateID: dateID} + task.Title = *row.title + task.Content = *row.content + task.QuestionID = *row.questionID + task.TitleSlug = *row.titleSlug + task.SetDifficultyFromNum(*row.difficulty) + + err := json.Unmarshal([]byte(*row.hints), &task.Hints) + if err != nil { + return common.BotLeetCodeTask{}, err + } + if row.topicTags != nil && *row.topicTags != "" { + err = json.Unmarshal([]byte(*row.topicTags), &task.TopicTags) + if err != nil { + return common.BotLeetCodeTask{}, err + } + } + return task, nil +} + func (y *ydbStorage) saveTask(ctx context.Context, task common.BotLeetCodeTask) error { marshalledHints, err := json.Marshal(task.Hints) if err != nil {