refactor: extract helpers to reduce funlen in command_palette
This commit is contained in:
@@ -9,9 +9,7 @@ func (q *Queries) GetCommandPaletteContinueWatching(ctx context.Context, userID
|
||||
if userID == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 5
|
||||
}
|
||||
limit = commandPaletteLimit(limit)
|
||||
|
||||
needle, pattern := commandPalettePattern(query)
|
||||
rows, err := q.db.QueryContext(ctx, `
|
||||
@@ -85,9 +83,7 @@ func (q *Queries) GetCommandPaletteWatchlist(ctx context.Context, userID string,
|
||||
if userID == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if limit <= 0 {
|
||||
limit = 5
|
||||
}
|
||||
limit = commandPaletteLimit(limit)
|
||||
|
||||
needle, pattern := commandPalettePattern(query)
|
||||
rows, err := q.db.QueryContext(ctx, `
|
||||
@@ -132,8 +128,22 @@ LIMIT ?`, userID, needle, pattern, pattern, pattern, pattern, limit)
|
||||
|
||||
items := make([]GetUserWatchListRow, 0, int(limit))
|
||||
for rows.Next() {
|
||||
item, err := scanWatchListEntry(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func scanWatchListEntry(rows scanner) (GetUserWatchListRow, error) {
|
||||
var item GetUserWatchListRow
|
||||
if err := rows.Scan(
|
||||
err := rows.Scan(
|
||||
&item.ID,
|
||||
&item.UserID,
|
||||
&item.AnimeID,
|
||||
@@ -148,16 +158,8 @@ LIMIT ?`, userID, needle, pattern, pattern, pattern, pattern, limit)
|
||||
&item.TitleJapanese,
|
||||
&item.ImageUrl,
|
||||
&item.Airing,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return items, nil
|
||||
)
|
||||
return item, err
|
||||
}
|
||||
|
||||
func commandPalettePattern(query string) (string, string) {
|
||||
@@ -165,6 +167,14 @@ func commandPalettePattern(query string) (string, string) {
|
||||
return needle, "%" + needle + "%"
|
||||
}
|
||||
|
||||
func commandPaletteLimit(limit int64) int64 {
|
||||
if limit <= 0 {
|
||||
return 5
|
||||
}
|
||||
|
||||
return limit
|
||||
}
|
||||
|
||||
type scanner interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user