Made some changes to how index pages are made

This commit is contained in:
Leon Mika 2025-02-16 14:06:45 +11:00
parent ba12398d2f
commit 573517565d
14 changed files with 259 additions and 56 deletions

View file

@ -28,6 +28,41 @@ func (q *Queries) GetBundleWithID(ctx context.Context, id int64) (Bundle, error)
return i, err
}
const getSiteBundleInfo = `-- name: GetSiteBundleInfo :many
WITH page_counts AS (
SELECT b.bundle_id, count(*) AS page_count FROM pages b WHERE b.site_id = $1 GROUP BY bundle_id
), index_pages AS (
SELECT p.id AS index_page_id, p.bundle_id FROM pages p WHERE p.site_id = $1 AND p.role = 'index'
)
SELECT b.bundle_id, b.page_count, p.index_page_id FROM page_counts b LEFT OUTER JOIN index_pages p ON b.bundle_id = p.bundle_id
`
type GetSiteBundleInfoRow struct {
BundleID int64
PageCount int64
IndexPageID pgtype.Int8
}
func (q *Queries) GetSiteBundleInfo(ctx context.Context, siteID int64) ([]GetSiteBundleInfoRow, error) {
rows, err := q.db.Query(ctx, getSiteBundleInfo, siteID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetSiteBundleInfoRow
for rows.Next() {
var i GetSiteBundleInfoRow
if err := rows.Scan(&i.BundleID, &i.PageCount, &i.IndexPageID); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertBundle = `-- name: InsertBundle :one
INSERT INTO bundles (
site_id,

View file

@ -54,6 +54,47 @@ func (ns NullPageNameProvenance) Value() (driver.Value, error) {
return string(ns.PageNameProvenance), nil
}
type PageRole string
const (
PageRoleIndex PageRole = "index"
)
func (e *PageRole) Scan(src interface{}) error {
switch s := src.(type) {
case []byte:
*e = PageRole(s)
case string:
*e = PageRole(s)
default:
return fmt.Errorf("unsupported scan type for PageRole: %T", src)
}
return nil
}
type NullPageRole struct {
PageRole PageRole
Valid bool // Valid is true if PageRole is not NULL
}
// Scan implements the Scanner interface.
func (ns *NullPageRole) Scan(value interface{}) error {
if value == nil {
ns.PageRole, ns.Valid = "", false
return nil
}
ns.Valid = true
return ns.PageRole.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullPageRole) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return string(ns.PageRole), nil
}
type PostState string
const (
@ -193,10 +234,11 @@ type Page struct {
Name string
NameProvenance PageNameProvenance
Title pgtype.Text
Role pgtype.Int8
PostTypeID pgtype.Int8
Body string
State PostState
Props []byte
Role NullPageRole
PublishDate pgtype.Timestamptz
CreatedAt pgtype.Timestamp
UpdatedAt pgtype.Timestamp
@ -206,7 +248,7 @@ type Post struct {
ID int64
SiteID int64
Title pgtype.Text
Role pgtype.Int8
PostTypeID pgtype.Int8
Body string
State PostState
Props []byte
@ -215,7 +257,7 @@ type Post struct {
UpdatedAt pgtype.Timestamp
}
type PostRole struct {
type PostType struct {
ID int64
SiteID int64
LayoutName string

View file

@ -21,7 +21,7 @@ func (q *Queries) DeletePageWithID(ctx context.Context, id int64) error {
}
const getPageWithID = `-- name: GetPageWithID :one
SELECT id, site_id, bundle_id, name, name_provenance, title, role, body, state, props, publish_date, created_at, updated_at FROM pages WHERE id = $1
SELECT id, site_id, bundle_id, name, name_provenance, title, post_type_id, body, state, props, role, publish_date, created_at, updated_at FROM pages WHERE id = $1
`
func (q *Queries) GetPageWithID(ctx context.Context, id int64) (Page, error) {
@ -34,10 +34,11 @@ func (q *Queries) GetPageWithID(ctx context.Context, id int64) (Page, error) {
&i.Name,
&i.NameProvenance,
&i.Title,
&i.Role,
&i.PostTypeID,
&i.Body,
&i.State,
&i.Props,
&i.Role,
&i.PublishDate,
&i.CreatedAt,
&i.UpdatedAt,
@ -52,14 +53,15 @@ INSERT INTO pages (
name,
name_provenance,
title,
role,
post_type_id,
body,
state,
props,
role,
publish_date,
created_at,
updated_at
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $11)
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $12)
RETURNING id
`
@ -69,10 +71,11 @@ type InsertPageParams struct {
Name string
NameProvenance PageNameProvenance
Title pgtype.Text
Role pgtype.Int8
PostTypeID pgtype.Int8
Body string
State PostState
Props []byte
Role NullPageRole
PublishDate pgtype.Timestamptz
CreatedAt pgtype.Timestamp
}
@ -84,10 +87,11 @@ func (q *Queries) InsertPage(ctx context.Context, arg InsertPageParams) (int64,
arg.Name,
arg.NameProvenance,
arg.Title,
arg.Role,
arg.PostTypeID,
arg.Body,
arg.State,
arg.Props,
arg.Role,
arg.PublishDate,
arg.CreatedAt,
)
@ -97,7 +101,7 @@ func (q *Queries) InsertPage(ctx context.Context, arg InsertPageParams) (int64,
}
const listPages = `-- name: ListPages :many
SELECT id, site_id, bundle_id, name, name_provenance, title, role, body, state, props, publish_date, created_at, updated_at FROM pages WHERE site_id = $1 ORDER BY name ASC LIMIT 25
SELECT id, site_id, bundle_id, name, name_provenance, title, post_type_id, body, state, props, role, publish_date, created_at, updated_at FROM pages WHERE site_id = $1 ORDER BY name ASC LIMIT 25
`
func (q *Queries) ListPages(ctx context.Context, siteID int64) ([]Page, error) {
@ -116,10 +120,11 @@ func (q *Queries) ListPages(ctx context.Context, siteID int64) ([]Page, error) {
&i.Name,
&i.NameProvenance,
&i.Title,
&i.Role,
&i.PostTypeID,
&i.Body,
&i.State,
&i.Props,
&i.Role,
&i.PublishDate,
&i.CreatedAt,
&i.UpdatedAt,
@ -135,7 +140,7 @@ func (q *Queries) ListPages(ctx context.Context, siteID int64) ([]Page, error) {
}
const listPublishablePages = `-- name: ListPublishablePages :many
SELECT id, site_id, bundle_id, name, name_provenance, title, role, body, state, props, publish_date, created_at, updated_at
SELECT id, site_id, bundle_id, name, name_provenance, title, post_type_id, body, state, props, role, publish_date, created_at, updated_at
FROM pages
WHERE id > $1 AND site_id = $2 AND state = 'published'
ORDER BY id LIMIT 100
@ -162,10 +167,11 @@ func (q *Queries) ListPublishablePages(ctx context.Context, arg ListPublishableP
&i.Name,
&i.NameProvenance,
&i.Title,
&i.Role,
&i.PostTypeID,
&i.Body,
&i.State,
&i.Props,
&i.Role,
&i.PublishDate,
&i.CreatedAt,
&i.UpdatedAt,
@ -187,13 +193,14 @@ UPDATE pages SET
name = $4,
name_provenance = $5,
title = $6,
role = $7,
body = $8,
state = $9,
props = $10,
publish_date = $11,
created_at = $12,
updated_at = $13
post_type_id = $7,
role = $8,
body = $9,
state = $10,
props = $11,
publish_date = $12,
created_at = $13,
updated_at = $14
WHERE id = $1
`
@ -204,7 +211,8 @@ type UpdatePageParams struct {
Name string
NameProvenance PageNameProvenance
Title pgtype.Text
Role pgtype.Int8
PostTypeID pgtype.Int8
Role NullPageRole
Body string
State PostState
Props []byte
@ -221,6 +229,7 @@ func (q *Queries) UpdatePage(ctx context.Context, arg UpdatePageParams) error {
arg.Name,
arg.NameProvenance,
arg.Title,
arg.PostTypeID,
arg.Role,
arg.Body,
arg.State,

View file

@ -21,7 +21,7 @@ func (q *Queries) DeletePost(ctx context.Context, id int64) error {
}
const getPostWithID = `-- name: GetPostWithID :one
SELECT id, site_id, title, role, body, state, props, publish_date, created_at, updated_at FROM posts WHERE id = $1 LIMIT 1
SELECT id, site_id, title, post_type_id, body, state, props, publish_date, created_at, updated_at FROM posts WHERE id = $1 LIMIT 1
`
func (q *Queries) GetPostWithID(ctx context.Context, id int64) (Post, error) {
@ -31,7 +31,7 @@ func (q *Queries) GetPostWithID(ctx context.Context, id int64) (Post, error) {
&i.ID,
&i.SiteID,
&i.Title,
&i.Role,
&i.PostTypeID,
&i.Body,
&i.State,
&i.Props,
@ -84,7 +84,7 @@ func (q *Queries) InsertPost(ctx context.Context, arg InsertPostParams) (int64,
}
const listPosts = `-- name: ListPosts :many
SELECT id, site_id, title, role, body, state, props, publish_date, created_at, updated_at FROM posts WHERE site_id = $1 ORDER BY publish_date DESC LIMIT 25
SELECT id, site_id, title, post_type_id, body, state, props, publish_date, created_at, updated_at FROM posts WHERE site_id = $1 ORDER BY publish_date DESC LIMIT 25
`
func (q *Queries) ListPosts(ctx context.Context, siteID int64) ([]Post, error) {
@ -100,7 +100,7 @@ func (q *Queries) ListPosts(ctx context.Context, siteID int64) ([]Post, error) {
&i.ID,
&i.SiteID,
&i.Title,
&i.Role,
&i.PostTypeID,
&i.Body,
&i.State,
&i.Props,
@ -119,7 +119,7 @@ func (q *Queries) ListPosts(ctx context.Context, siteID int64) ([]Post, error) {
}
const listPublishablePosts = `-- name: ListPublishablePosts :many
SELECT id, site_id, title, role, body, state, props, publish_date, created_at, updated_at
SELECT id, site_id, title, post_type_id, body, state, props, publish_date, created_at, updated_at
FROM posts
WHERE id > $1 AND site_id = $2 AND state = 'published' AND publish_date <= $3
ORDER BY id LIMIT 100
@ -144,7 +144,7 @@ func (q *Queries) ListPublishablePosts(ctx context.Context, arg ListPublishableP
&i.ID,
&i.SiteID,
&i.Title,
&i.Role,
&i.PostTypeID,
&i.Body,
&i.State,
&i.Props,