Added user authentication

This commit is contained in:
Leon Mika 2025-02-01 09:42:32 +11:00
parent d7e7af5a10
commit cb54057305
40 changed files with 710 additions and 218 deletions

View file

@ -156,10 +156,17 @@ type PublishTarget struct {
}
type Site struct {
ID int64
Name string
Title string
Url string
Theme string
Props []byte
ID int64
OwnerUserID int64
Name string
Title string
Url string
Theme string
Props []byte
}
type User struct {
ID int64
Email string
Password string
}

View file

@ -11,8 +11,17 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const deletePost = `-- name: DeletePost :exec
DELETE FROM posts WHERE id = $1
`
func (q *Queries) DeletePost(ctx context.Context, id int64) error {
_, err := q.db.Exec(ctx, deletePost, id)
return err
}
const getPostWithID = `-- name: GetPostWithID :one
SELECT id, site_id, title, body, state, props, post_date, created_at FROM post WHERE id = $1 LIMIT 1
SELECT id, site_id, title, body, state, props, post_date, created_at FROM posts WHERE id = $1 LIMIT 1
`
func (q *Queries) GetPostWithID(ctx context.Context, id int64) (Post, error) {
@ -32,7 +41,7 @@ func (q *Queries) GetPostWithID(ctx context.Context, id int64) (Post, error) {
}
const insertPost = `-- name: InsertPost :one
INSERT INTO post (
INSERT INTO posts (
site_id,
title,
body,
@ -70,7 +79,7 @@ func (q *Queries) InsertPost(ctx context.Context, arg InsertPostParams) (int64,
}
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
SELECT id, site_id, title, body, state, props, post_date, created_at FROM posts WHERE site_id = $1 ORDER BY post_date DESC LIMIT 25
`
func (q *Queries) ListPosts(ctx context.Context, siteID int64) ([]Post, error) {
@ -104,7 +113,7 @@ func (q *Queries) ListPosts(ctx context.Context, siteID int64) ([]Post, error) {
const listPublishablePosts = `-- name: ListPublishablePosts :many
SELECT id, site_id, title, body, state, props, post_date, created_at
FROM post
FROM posts
WHERE id > $1 AND site_id = $2 AND state = 'published' AND post_date <= $3
ORDER BY id LIMIT 100
`
@ -145,7 +154,7 @@ func (q *Queries) ListPublishablePosts(ctx context.Context, arg ListPublishableP
}
const updatePost = `-- name: UpdatePost :exec
UPDATE post SET
UPDATE posts SET
site_id = $2,
title = $3,
body = $4,

View file

@ -10,7 +10,7 @@ import (
)
const getSiteWithID = `-- name: GetSiteWithID :one
SELECT id, name, title, url, theme, props FROM site WHERE id = $1 LIMIT 1
SELECT id, owner_user_id, name, title, url, theme, props FROM sites WHERE id = $1 LIMIT 1
`
func (q *Queries) GetSiteWithID(ctx context.Context, id int64) (Site, error) {
@ -18,6 +18,7 @@ func (q *Queries) GetSiteWithID(ctx context.Context, id int64) (Site, error) {
var i Site
err := row.Scan(
&i.ID,
&i.OwnerUserID,
&i.Name,
&i.Title,
&i.Url,
@ -28,7 +29,7 @@ func (q *Queries) GetSiteWithID(ctx context.Context, id int64) (Site, error) {
}
const listSites = `-- name: ListSites :one
SELECT id, name, title, url, theme, props FROM site
SELECT id, owner_user_id, name, title, url, theme, props FROM sites
`
func (q *Queries) ListSites(ctx context.Context) (Site, error) {
@ -36,6 +37,7 @@ func (q *Queries) ListSites(ctx context.Context) (Site, error) {
var i Site
err := row.Scan(
&i.ID,
&i.OwnerUserID,
&i.Name,
&i.Title,
&i.Url,
@ -46,27 +48,30 @@ func (q *Queries) ListSites(ctx context.Context) (Site, error) {
}
const newSite = `-- name: NewSite :one
INSERT INTO site (
INSERT INTO sites (
name,
owner_user_id,
title,
url,
theme,
props
) VALUES ($1, $2, $3, $4, $5)
) VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id
`
type NewSiteParams struct {
Name string
Title string
Url string
Theme string
Props []byte
Name string
OwnerUserID int64
Title string
Url string
Theme string
Props []byte
}
func (q *Queries) NewSite(ctx context.Context, arg NewSiteParams) (int64, error) {
row := q.db.QueryRow(ctx, newSite,
arg.Name,
arg.OwnerUserID,
arg.Title,
arg.Url,
arg.Theme,

View file

@ -10,7 +10,7 @@ import (
)
const insertPublishTarget = `-- name: InsertPublishTarget :one
INSERT INTO publish_target (
INSERT INTO publish_targets (
site_id,
role,
target_type,
@ -42,7 +42,7 @@ func (q *Queries) InsertPublishTarget(ctx context.Context, arg InsertPublishTarg
}
const listPublishTargetsOfRole = `-- name: ListPublishTargetsOfRole :many
SELECT id, site_id, role, target_type, url, target_ref FROM publish_target WHERE site_id = $1 AND role = 'production'
SELECT id, site_id, role, target_type, url, target_ref FROM publish_targets WHERE site_id = $1 AND role = 'production'
`
func (q *Queries) ListPublishTargetsOfRole(ctx context.Context, siteID int64) ([]PublishTarget, error) {

53
gen/sqlc/dbq/users.sql.go Normal file
View file

@ -0,0 +1,53 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// source: users.sql
package dbq
import (
"context"
)
const addUser = `-- name: AddUser :one
INSERT INTO users (
email,
password
) VALUES ($1, $2)
ON CONFLICT (email) DO UPDATE SET password = $2
RETURNING id
`
type AddUserParams struct {
Email string
Password string
}
func (q *Queries) AddUser(ctx context.Context, arg AddUserParams) (int64, error) {
row := q.db.QueryRow(ctx, addUser, arg.Email, arg.Password)
var id int64
err := row.Scan(&id)
return id, err
}
const getUserByEmail = `-- name: GetUserByEmail :one
SELECT id, email, password FROM users WHERE email = $1 LIMIT 1
`
func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
row := q.db.QueryRow(ctx, getUserByEmail, email)
var i User
err := row.Scan(&i.ID, &i.Email, &i.Password)
return i, err
}
const getUserByID = `-- name: GetUserByID :one
SELECT id, email, password FROM users WHERE id = $1 LIMIT 1
`
func (q *Queries) GetUserByID(ctx context.Context, id int64) (User, error) {
row := q.db.QueryRow(ctx, getUserByID, id)
var i User
err := row.Scan(&i.ID, &i.Email, &i.Password)
return i, err
}