Have got post creation working.
This commit is contained in:
parent
63b19a249a
commit
8e0ffb6c24
20 changed files with 479 additions and 11 deletions
|
|
@ -4,6 +4,66 @@
|
|||
|
||||
package dbq
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type PostState string
|
||||
|
||||
const (
|
||||
PostStateDraft PostState = "draft"
|
||||
PostStatePublished PostState = "published"
|
||||
)
|
||||
|
||||
func (e *PostState) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = PostState(s)
|
||||
case string:
|
||||
*e = PostState(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for PostState: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullPostState struct {
|
||||
PostState PostState
|
||||
Valid bool // Valid is true if PostState is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullPostState) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.PostState, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.PostState.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullPostState) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.PostState), nil
|
||||
}
|
||||
|
||||
type Post struct {
|
||||
ID int64
|
||||
SiteID int64
|
||||
Title pgtype.Text
|
||||
Body string
|
||||
State PostState
|
||||
Props []byte
|
||||
PostDate pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamp
|
||||
}
|
||||
|
||||
type Site struct {
|
||||
ID int64
|
||||
Name string
|
||||
|
|
|
|||
83
gen/sqlc/dbq/posts.sql.go
Normal file
83
gen/sqlc/dbq/posts.sql.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.28.0
|
||||
// source: posts.sql
|
||||
|
||||
package dbq
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const insertPost = `-- name: InsertPost :one
|
||||
INSERT INTO post (
|
||||
site_id,
|
||||
title,
|
||||
body,
|
||||
state,
|
||||
props,
|
||||
post_date,
|
||||
created_at
|
||||
) VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type InsertPostParams struct {
|
||||
SiteID int64
|
||||
Title pgtype.Text
|
||||
Body string
|
||||
State PostState
|
||||
Props []byte
|
||||
PostDate pgtype.Timestamptz
|
||||
CreatedAt pgtype.Timestamp
|
||||
}
|
||||
|
||||
func (q *Queries) InsertPost(ctx context.Context, arg InsertPostParams) (int64, error) {
|
||||
row := q.db.QueryRow(ctx, insertPost,
|
||||
arg.SiteID,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.State,
|
||||
arg.Props,
|
||||
arg.PostDate,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const listPosts = `-- name: ListPosts :many
|
||||
SELECT id, site_id, title, body, state, props, post_date, created_at FROM post WHERE site_id = $1 ORDER BY post_date DESC LIMIT 25
|
||||
`
|
||||
|
||||
func (q *Queries) ListPosts(ctx context.Context, siteID int64) ([]Post, error) {
|
||||
rows, err := q.db.Query(ctx, listPosts, siteID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Post
|
||||
for rows.Next() {
|
||||
var i Post
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.SiteID,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.State,
|
||||
&i.Props,
|
||||
&i.PostDate,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue