refactor: Improve optional filtering in FindMany task query

This commit is contained in:
Erik Winter (aider) 2024-12-29 12:04:42 +01:00
parent 6119d08c8d
commit 0413da4c3d
1 changed files with 11 additions and 3 deletions

View File

@ -64,17 +64,25 @@ WHERE id = ?`, id).Scan(&tsk.ID, &tsk.Title, &dateStr, &timeStr, &durStr, &recur
}
func (t *SqliteTask) FindMany(params storage.TaskListParams) ([]item.Task, error) {
query := `SELECT id, title, date, time, duration, recurrer FROM tasks WHERE 1=1`
query := `SELECT id, title, date, time, duration, recurrer FROM tasks`
args := []interface{}{}
where := []string{}
if params.Recurrer {
query += ` AND recurrer IS NOT NULL AND recurrer != ''`
where = append(where, `recurrer IS NOT NULL AND recurrer != ''`)
}
if !params.Date.IsZero() {
query += ` AND date = ?`
where = append(where, `date = ?`)
args = append(args, params.Date.String())
}
if len(where) > 0 {
query += ` WHERE ` + where[0]
for _, w := range where[1:] {
query += ` AND ` + w
}
}
rows, err := t.db.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)