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