Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
78 changes: 46 additions & 32 deletions internal/storage/ydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
Loading