refactor: Unmarshal recurrer JSON in Updated and RecursBefore methods

This commit is contained in:
Erik Winter (aider) 2024-12-20 15:32:47 +01:00
parent f4ca723102
commit 7e094d04cc
1 changed files with 18 additions and 2 deletions

View File

@ -111,9 +111,17 @@ func (p *Postgres) Updated(ks []item.Kind, t time.Time) ([]item.Item, error) {
for rows.Next() {
var item item.Item
var recurNext sql.NullTime
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &item.Recurrer, &recurNext); err != nil {
var recurrerJSON []byte
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &recurrerJSON, &recurNext); err != nil {
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
}
if len(recurrerJSON) > 0 {
var recurrer item.Recur
if err := json.Unmarshal(recurrerJSON, &recurrer); err != nil {
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
}
item.Recurrer = &recurrer
}
if recurNext.Valid {
item.RecurNext = recurNext.Time
}
@ -139,9 +147,17 @@ func (p *Postgres) RecursBefore(date time.Time) ([]item.Item, error) {
for rows.Next() {
var item item.Item
var recurNext sql.NullTime
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &item.Recurrer, &recurNext); err != nil {
var recurrerJSON []byte
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Body, &recurrerJSON, &recurNext); err != nil {
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
}
if len(recurrerJSON) > 0 {
var recurrer item.Recur
if err := json.Unmarshal(recurrerJSON, &recurrer); err != nil {
return nil, fmt.Errorf("%w: %v", ErrPostgresFailure, err)
}
item.Recurrer = &recurrer
}
if recurNext.Valid {
item.RecurNext = recurNext.Time
}