diff options
Diffstat (limited to 'internal/store')
| -rw-r--r-- | internal/store/posts.go | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/internal/store/posts.go b/internal/store/posts.go index 94bc856..569640b 100644 --- a/internal/store/posts.go +++ b/internal/store/posts.go @@ -108,10 +108,15 @@ func (bs *BlogStore) PublishedPostBySlug(ctx context.Context, pageID int64, slug WHERE p.page_id=$1 AND p.slug=$2 AND p.published`, pageID, slug)) } +// CreatePost inserts the post; a zero CreatedAt means now. The post date is +// the blogger's to choose, so it is a plain column rather than a fixed stamp. func (bs *BlogStore) CreatePost(ctx context.Context, p *Post) (*Post, error) { + if p.CreatedAt.IsZero() { + p.CreatedAt = time.Now() + } var id int64 - err := bs.db.QueryRow(ctx, `INSERT INTO posts (page_id, slug, title, body_md, body_html, published) VALUES ($1,$2,$3,$4,$5,$6) RETURNING id`, - p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published).Scan(&id) + err := bs.db.QueryRow(ctx, `INSERT INTO posts (page_id, slug, title, body_md, body_html, published, created_at) VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING id`, + p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published, p.CreatedAt).Scan(&id) if err != nil { return nil, wrap(err) } @@ -119,8 +124,8 @@ func (bs *BlogStore) CreatePost(ctx context.Context, p *Post) (*Post, error) { } func (bs *BlogStore) UpdatePost(ctx context.Context, p *Post) error { - _, err := bs.db.Exec(ctx, `UPDATE posts SET page_id=$2, slug=$3, title=$4, body_md=$5, body_html=$6, published=$7, updated_at=now() WHERE id=$1`, - p.ID, p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published) + _, err := bs.db.Exec(ctx, `UPDATE posts SET page_id=$2, slug=$3, title=$4, body_md=$5, body_html=$6, published=$7, created_at=$8, updated_at=now() WHERE id=$1`, + p.ID, p.PageID, p.Slug, p.Title, p.BodyMD, p.BodyHTML, p.Published, p.CreatedAt) return wrap(err) } |
