Have got first run working and publishing to Netlify
This commit is contained in:
parent
b7e0269e9d
commit
30d99eeb9e
22 changed files with 472 additions and 47 deletions
|
|
@ -29,14 +29,16 @@ type PublishTarget struct {
|
|||
}
|
||||
|
||||
type Site struct {
|
||||
ID int64
|
||||
OwnerID int64
|
||||
Title string
|
||||
Tagline string
|
||||
ID int64
|
||||
OwnerID int64
|
||||
Title string
|
||||
Tagline string
|
||||
CreatedAt int64
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int64
|
||||
Username string
|
||||
Password string
|
||||
ID int64
|
||||
Username string
|
||||
Password string
|
||||
CreatedAt int64
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,32 +7,51 @@ package sqlgen
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
const hasUsersAndSites = `-- name: HasUsersAndSites :one
|
||||
SELECT (SELECT COUNT(*) FROM users) > 0 AND (SELECT COUNT(*) FROM sites) > 0 AS has_users_and_sites
|
||||
`
|
||||
|
||||
func (q *Queries) HasUsersAndSites(ctx context.Context) (sql.NullBool, error) {
|
||||
row := q.db.QueryRowContext(ctx, hasUsersAndSites)
|
||||
var has_users_and_sites sql.NullBool
|
||||
err := row.Scan(&has_users_and_sites)
|
||||
return has_users_and_sites, err
|
||||
}
|
||||
|
||||
const insertSite = `-- name: InsertSite :one
|
||||
INSERT INTO sites (
|
||||
owner_id,
|
||||
title,
|
||||
tagline
|
||||
) VALUES (?, ?, ?)
|
||||
tagline,
|
||||
created_at
|
||||
) VALUES (?, ?, ?, ?)
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type InsertSiteParams struct {
|
||||
OwnerID int64
|
||||
Title string
|
||||
Tagline string
|
||||
OwnerID int64
|
||||
Title string
|
||||
Tagline string
|
||||
CreatedAt int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertSite(ctx context.Context, arg InsertSiteParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertSite, arg.OwnerID, arg.Title, arg.Tagline)
|
||||
row := q.db.QueryRowContext(ctx, insertSite,
|
||||
arg.OwnerID,
|
||||
arg.Title,
|
||||
arg.Tagline,
|
||||
arg.CreatedAt,
|
||||
)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const selectSiteByID = `-- name: SelectSiteByID :one
|
||||
SELECT id, owner_id, title, tagline FROM sites WHERE id = ?
|
||||
SELECT id, owner_id, title, tagline, created_at FROM sites WHERE id = ?
|
||||
`
|
||||
|
||||
func (q *Queries) SelectSiteByID(ctx context.Context, id int64) (Site, error) {
|
||||
|
|
@ -43,12 +62,13 @@ func (q *Queries) SelectSiteByID(ctx context.Context, id int64) (Site, error) {
|
|||
&i.OwnerID,
|
||||
&i.Title,
|
||||
&i.Tagline,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const selectSitesOwnedByUser = `-- name: SelectSitesOwnedByUser :many
|
||||
SELECT id, owner_id, title, tagline FROM sites WHERE owner_id = ?
|
||||
SELECT id, owner_id, title, tagline, created_at FROM sites WHERE owner_id = ? ORDER BY title ASC
|
||||
`
|
||||
|
||||
func (q *Queries) SelectSitesOwnedByUser(ctx context.Context, ownerID int64) ([]Site, error) {
|
||||
|
|
@ -65,6 +85,7 @@ func (q *Queries) SelectSitesOwnedByUser(ctx context.Context, ownerID int64) ([]
|
|||
&i.OwnerID,
|
||||
&i.Title,
|
||||
&i.Tagline,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,40 +10,51 @@ import (
|
|||
)
|
||||
|
||||
const insertUser = `-- name: InsertUser :one
|
||||
INSERT INTO users (username, password) VALUES (?, ?) RETURNING id
|
||||
INSERT INTO users (username, password, created_at) VALUES (?, ?, ?) RETURNING id
|
||||
`
|
||||
|
||||
type InsertUserParams struct {
|
||||
Username string
|
||||
Password string
|
||||
Username string
|
||||
Password string
|
||||
CreatedAt int64
|
||||
}
|
||||
|
||||
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (int64, error) {
|
||||
row := q.db.QueryRowContext(ctx, insertUser, arg.Username, arg.Password)
|
||||
row := q.db.QueryRowContext(ctx, insertUser, arg.Username, arg.Password, arg.CreatedAt)
|
||||
var id int64
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const selectUserByID = `-- name: SelectUserByID :one
|
||||
SELECT id, username, password FROM users WHERE id = ? LIMIT 1
|
||||
SELECT id, username, password, created_at FROM users WHERE id = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) SelectUserByID(ctx context.Context, id int64) (User, error) {
|
||||
row := q.db.QueryRowContext(ctx, selectUserByID, id)
|
||||
var i User
|
||||
err := row.Scan(&i.ID, &i.Username, &i.Password)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const selectUserByUsername = `-- name: SelectUserByUsername :one
|
||||
SELECT id, username, password FROM users WHERE username = ? LIMIT 1
|
||||
SELECT id, username, password, created_at FROM users WHERE username = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) SelectUserByUsername(ctx context.Context, username string) (User, error) {
|
||||
row := q.db.QueryRowContext(ctx, selectUserByUsername, username)
|
||||
var i User
|
||||
err := row.Scan(&i.ID, &i.Username, &i.Password)
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.Password,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package db
|
|||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/providers/db/gen/sqlgen"
|
||||
|
|
@ -18,6 +19,7 @@ func (db *Provider) SelectSiteByID(ctx context.Context, id int64) (models.Site,
|
|||
OwnerID: row.OwnerID,
|
||||
Title: row.Title,
|
||||
Tagline: row.Tagline,
|
||||
Created: time.Unix(row.CreatedAt, 0).UTC(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -34,6 +36,7 @@ func (db *Provider) SelectSitesOwnedByUser(ctx context.Context, ownerID int64) (
|
|||
OwnerID: row.OwnerID,
|
||||
Title: row.Title,
|
||||
Tagline: row.Tagline,
|
||||
Created: time.Unix(row.CreatedAt, 0).UTC(),
|
||||
}
|
||||
}
|
||||
return sites, nil
|
||||
|
|
@ -42,9 +45,10 @@ func (db *Provider) SelectSitesOwnedByUser(ctx context.Context, ownerID int64) (
|
|||
func (db *Provider) SaveSite(ctx context.Context, site *models.Site) error {
|
||||
if site.ID == 0 {
|
||||
newID, err := db.queries.InsertSite(ctx, sqlgen.InsertSiteParams{
|
||||
OwnerID: site.OwnerID,
|
||||
Title: site.Title,
|
||||
Tagline: site.Tagline,
|
||||
OwnerID: site.OwnerID,
|
||||
Title: site.Title,
|
||||
Tagline: site.Tagline,
|
||||
CreatedAt: timeToInt(site.Created),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -56,3 +60,11 @@ func (db *Provider) SaveSite(ctx context.Context, site *models.Site) error {
|
|||
// No update query defined in sqlgen yet
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Provider) HasUsersAndSites(ctx context.Context) (bool, error) {
|
||||
nullBool, err := db.queries.HasUsersAndSites(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return nullBool.Valid && nullBool.Bool, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package db
|
|||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/providers/db/gen/sqlgen"
|
||||
|
|
@ -31,8 +32,9 @@ func (db *Provider) SaveUser(ctx context.Context, user *models.User) error {
|
|||
|
||||
if user.ID == 0 {
|
||||
newID, err := db.queries.InsertUser(ctx, sqlgen.InsertUserParams{
|
||||
Username: user.Username,
|
||||
Password: hashedPassword,
|
||||
Username: user.Username,
|
||||
Password: hashedPassword,
|
||||
CreatedAt: timeToInt(user.Created),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -58,5 +60,6 @@ func dbUserToUser(res sqlgen.User) (models.User, error) {
|
|||
ID: res.ID,
|
||||
Username: res.Username,
|
||||
PasswordHashed: pwdBytes,
|
||||
Created: time.Unix(res.CreatedAt, 0).UTC(),
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue