From 5f9fc2a8667a9438b6336d75026f9a455fc9c4ce Mon Sep 17 00:00:00 2001 From: grm Date: Mon, 14 Sep 2026 22:58:08 +0300 Subject: Let the blogger set a post's date, when writing it and afterwards The post date is posts.created_at: it already drives ordering, the archive, the feed and the displayed dates, so making it editable is a form field rather than a new column. The datetime-local input degrades to a text box on old browsers, so the server also accepts the value typed by hand; blank keeps the current date. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Sd8UPWrvyYCLj97JexNw3A --- internal/store/posts.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'internal/store/posts.go') 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) } -- cgit v1.2.3