refactor: Optimize task filtering by moving to SQL query

This commit is contained in:
Erik Winter (aider) 2024-12-29 12:03:57 +01:00
parent 6664b24a23
commit 6119d08c8d
1 changed files with 13 additions and 6 deletions

View File

@ -64,9 +64,18 @@ WHERE id = ?`, id).Scan(&tsk.ID, &tsk.Title, &dateStr, &timeStr, &durStr, &recur
}
func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) {
rows, err := t.db.Query(`
SELECT id, title, date, time, duration, recurrer
FROM tasks`)
query := `SELECT id, title, date, time, duration, recurrer FROM tasks WHERE 1=1`
args := []interface{}{}
if params.Recurrer {
query += ` AND recurrer IS NOT NULL AND recurrer != ''`
}
if !params.Date.IsZero() {
query += ` AND date = ?`
args = append(args, params.Date.String())
}
rows, err := t.db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
}
@ -87,9 +96,7 @@ FROM tasks`)
tsk.Duration = dur
tsk.Recurrer = item.NewRecurrer(recurStr)
if storage.Match(tsk, params) {
tasks = append(tasks, tsk)
}
tasks = append(tasks, tsk)
}
return tasks, nil