feat: Implement Recurrer interface methods for Postgres service
This commit is contained in:
parent
66613ff6ea
commit
860ba6cc4a
|
@ -157,6 +157,58 @@ func (p *Postgres) migrate(wanted []string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (p *Postgres) RecursBefore(date time.Time) ([]item.Item, error) {
|
||||
query := `
|
||||
SELECT id, kind, updated, deleted, body, recurrer, recur_next
|
||||
FROM items
|
||||
WHERE recur_next <= $1 AND recurrer IS NOT NULL`
|
||||
|
||||
rows, err := p.db.Query(query, date)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make([]item.Item, 0)
|
||||
for rows.Next() {
|
||||
var item item.Item
|
||||
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &item.Recurrer, &item.RecurNext); err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
result = append(result, item)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (p *Postgres) RecursNext(id string, date time.Time) error {
|
||||
var item item.Item
|
||||
err := p.db.QueryRow(`
|
||||
SELECT id, kind, updated, deleted, body, recurrer, recur_next
|
||||
FROM items
|
||||
WHERE id = $1`, id).Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &item.Recurrer, &item.RecurNext)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return ErrNotFound
|
||||
}
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
if item.Recurrer == nil {
|
||||
return ErrNotARecurrer
|
||||
}
|
||||
|
||||
_, err = p.db.Exec(`
|
||||
UPDATE items
|
||||
SET recur_next = $1
|
||||
WHERE id = $2`, date, id)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func compareMigrations(wanted, existing []string) ([]string, error) {
|
||||
var needed []string
|
||||
if len(wanted) < len(existing) {
|
||||
|
|
Loading…
Reference in New Issue