fix: Marshal Recurrer to JSON before inserting into Postgres database

This commit is contained in:
Erik Winter (aider) 2024-12-20 15:31:57 +01:00
parent 55fe158f79
commit f4ca723102
1 changed files with 12 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package main
import ( import (
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -58,7 +59,16 @@ func NewPostgres(host, port, dbname, user, password string) (*Postgres, error) {
} }
func (p *Postgres) Update(item item.Item, ts time.Time) error { func (p *Postgres) Update(item item.Item, ts time.Time) error {
_, err := p.db.Exec(` var recurrerJSON []byte
var err error
if item.Recurrer != nil {
recurrerJSON, err = json.Marshal(item.Recurrer)
if err != nil {
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
}
}
_, err = p.db.Exec(`
INSERT INTO items (id, kind, updated, deleted, body, recurrer, recur_next) INSERT INTO items (id, kind, updated, deleted, body, recurrer, recur_next)
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (id) DO UPDATE ON CONFLICT (id) DO UPDATE
@ -68,7 +78,7 @@ func (p *Postgres) Update(item item.Item, ts time.Time) error {
body = EXCLUDED.body, body = EXCLUDED.body,
recurrer = EXCLUDED.recurrer, recurrer = EXCLUDED.recurrer,
recur_next = EXCLUDED.recur_next`, recur_next = EXCLUDED.recur_next`,
item.ID, item.Kind, ts, item.Deleted, item.Body, item.Recurrer, item.RecurNext) item.ID, item.Kind, ts, item.Deleted, item.Body, recurrerJSON, item.RecurNext)
if err != nil { if err != nil {
return fmt.Errorf("%w: %v", ErrPostgresFailure, err) return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
} }