fog/internal/store/postgres/posts.go

53 lines
864 B
Go
Raw Normal View History

2024-12-30 10:02:01 +00:00
package postgres
import (
"context"
"database/sql"
"time"
"github.com/lib/pq"
"midnadimple.com/fog/internal/store"
)
type PostsStore struct {
DB *sql.DB
}
func (s *PostsStore) Create(ctx context.Context, post *store.Post) error {
query := `
INSERT INTO posts (content, title, user_id, tags, reply_ids)
VALUES ($1, $2, $3, $4) RETURNING id, created_at, updated_at
`
var createdAtStr, updatedAtStr string
err := s.DB.QueryRowContext(
ctx,
query,
post.Content,
post.Title,
post.UserID,
pq.Array(post.Tags),
pq.Array(post.ReplyIDs),
).Scan(
&post.ID,
&createdAtStr,
&updatedAtStr,
)
if err != nil {
return err
}
post.CreatedAt, err = pq.ParseTimestamp(time.UTC, createdAtStr)
if err != nil {
return err
}
post.UpdatedAt, err = pq.ParseTimestamp(time.UTC, updatedAtStr)
if err != nil {
return err
}
return nil
}