refactor: Optimize task filtering by moving to SQL query
This commit is contained in:
parent
6664b24a23
commit
6119d08c8d
|
@ -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) {
|
func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) {
|
||||||
rows, err := t.db.Query(`
|
query := `SELECT id, title, date, time, duration, recurrer FROM tasks WHERE 1=1`
|
||||||
SELECT id, title, date, time, duration, recurrer
|
args := []interface{}{}
|
||||||
FROM tasks`)
|
|
||||||
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
||||||
}
|
}
|
||||||
|
@ -87,10 +96,8 @@ FROM tasks`)
|
||||||
tsk.Duration = dur
|
tsk.Duration = dur
|
||||||
tsk.Recurrer = item.NewRecurrer(recurStr)
|
tsk.Recurrer = item.NewRecurrer(recurStr)
|
||||||
|
|
||||||
if storage.Match(tsk, params) {
|
|
||||||
tasks = append(tasks, tsk)
|
tasks = append(tasks, tsk)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return tasks, nil
|
return tasks, nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue