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 (
"database/sql"
"encoding/json"
"errors"
"fmt"
"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 {
_, 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)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (id) DO UPDATE
@ -68,7 +78,7 @@ func (p *Postgres) Update(item item.Item, ts time.Time) error {
body = EXCLUDED.body,
recurrer = EXCLUDED.recurrer,
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 {
return fmt.Errorf("%w: %v", ErrPostgresFailure, err)
}