Started working on the frontend
- Added the new post frontend - Hooked up publishing of posts to the site publisher - Added an site exporter as a publishing target
This commit is contained in:
parent
a59008b3e8
commit
e77cac2fd5
40 changed files with 1427 additions and 84 deletions
11
providers/db/errors.go
Normal file
11
providers/db/errors.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"emperror.dev/errors"
|
||||
)
|
||||
|
||||
func ErrorIsNoRows(err error) bool {
|
||||
return errors.Is(err, sql.ErrNoRows)
|
||||
}
|
||||
|
|
@ -47,6 +47,26 @@ func (q *Queries) InsertPost(ctx context.Context, arg InsertPostParams) (int64,
|
|||
return id, err
|
||||
}
|
||||
|
||||
const selectPostByGUID = `-- name: SelectPostByGUID :one
|
||||
SELECT id, site_id, guid, title, body, slug, created_at, published_at FROM posts WHERE guid = ? LIMIT 1
|
||||
`
|
||||
|
||||
func (q *Queries) SelectPostByGUID(ctx context.Context, guid string) (Post, error) {
|
||||
row := q.db.QueryRowContext(ctx, selectPostByGUID, guid)
|
||||
var i Post
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.SiteID,
|
||||
&i.Guid,
|
||||
&i.Title,
|
||||
&i.Body,
|
||||
&i.Slug,
|
||||
&i.CreatedAt,
|
||||
&i.PublishedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const selectPostsOfSite = `-- name: SelectPostsOfSite :many
|
||||
SELECT id, site_id, guid, title, body, slug, created_at, published_at FROM posts WHERE site_id = ? ORDER BY created_at DESC LIMIT 10
|
||||
`
|
||||
|
|
@ -82,3 +102,31 @@ func (q *Queries) SelectPostsOfSite(ctx context.Context, siteID int64) ([]Post,
|
|||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updatePost = `-- name: UpdatePost :exec
|
||||
UPDATE posts SET
|
||||
title = ?,
|
||||
body = ?,
|
||||
slug = ?,
|
||||
published_at = ?
|
||||
WHERE id = ?
|
||||
`
|
||||
|
||||
type UpdatePostParams struct {
|
||||
Title string
|
||||
Body string
|
||||
Slug string
|
||||
PublishedAt int64
|
||||
ID int64
|
||||
}
|
||||
|
||||
func (q *Queries) UpdatePost(ctx context.Context, arg UpdatePostParams) error {
|
||||
_, err := q.db.ExecContext(ctx, updatePost,
|
||||
arg.Title,
|
||||
arg.Body,
|
||||
arg.Slug,
|
||||
arg.PublishedAt,
|
||||
arg.ID,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,20 +16,20 @@ func (db *Provider) SelectPostsOfSite(ctx context.Context, siteID int64) ([]*mod
|
|||
|
||||
posts := make([]*models.Post, len(rows))
|
||||
for i, row := range rows {
|
||||
posts[i] = &models.Post{
|
||||
ID: row.ID,
|
||||
SiteID: row.SiteID,
|
||||
GUID: row.Guid,
|
||||
Title: row.Title,
|
||||
Body: row.Body,
|
||||
Slug: row.Slug,
|
||||
CreatedAt: time.Unix(row.CreatedAt, 0).UTC(),
|
||||
PublishedAt: time.Unix(row.PublishedAt, 0).UTC(),
|
||||
}
|
||||
posts[i] = dbPostToPost(row)
|
||||
}
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
func (db *Provider) SelectPostByGUID(ctx context.Context, guid string) (*models.Post, error) {
|
||||
row, err := db.queries.SelectPostByGUID(ctx, guid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dbPostToPost(row), nil
|
||||
}
|
||||
|
||||
func (db *Provider) SavePost(ctx context.Context, post *models.Post) error {
|
||||
if post.ID == 0 {
|
||||
newID, err := db.queries.InsertPost(ctx, sqlgen.InsertPostParams{
|
||||
|
|
@ -48,6 +48,24 @@ func (db *Provider) SavePost(ctx context.Context, post *models.Post) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// No update query defined in sqlgen yet
|
||||
return nil
|
||||
return db.queries.UpdatePost(ctx, sqlgen.UpdatePostParams{
|
||||
ID: post.ID,
|
||||
Title: post.Title,
|
||||
Body: post.Body,
|
||||
Slug: post.Slug,
|
||||
PublishedAt: post.PublishedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
func dbPostToPost(row sqlgen.Post) *models.Post {
|
||||
return &models.Post{
|
||||
ID: row.ID,
|
||||
SiteID: row.SiteID,
|
||||
GUID: row.Guid,
|
||||
Title: row.Title,
|
||||
Body: row.Body,
|
||||
Slug: row.Slug,
|
||||
CreatedAt: time.Unix(row.CreatedAt, 0).UTC(),
|
||||
PublishedAt: time.Unix(row.PublishedAt, 0).UTC(),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
80
providers/siteexporter/exporter.go
Normal file
80
providers/siteexporter/exporter.go
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
package siteexporter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
"lmika.dev/lmika/weiro/handlers/importexport"
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
type SiteExporter struct {
|
||||
site models.Site
|
||||
baseURL string
|
||||
baseDir string
|
||||
}
|
||||
|
||||
func New(site models.Site, baseURL, baseDir string) *SiteExporter {
|
||||
return &SiteExporter{
|
||||
site: site,
|
||||
baseDir: baseDir,
|
||||
baseURL: baseURL,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SiteExporter) WriteSiteYAML() error {
|
||||
siteYAML := importexport.Site{
|
||||
Title: s.site.Title,
|
||||
Tagline: s.site.Tagline,
|
||||
BaseURL: s.baseURL,
|
||||
}
|
||||
|
||||
bytes, err := yaml.Marshal(siteYAML)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(filepath.Join(s.baseDir, "site.yaml"), bytes, 0644)
|
||||
}
|
||||
|
||||
func (s *SiteExporter) WritePost(post *models.Post) error {
|
||||
postMeta := importexport.Post{
|
||||
ID: post.GUID,
|
||||
Title: post.Title,
|
||||
Date: post.PublishedAt,
|
||||
Slug: post.Slug,
|
||||
}
|
||||
frontMatter, err := yaml.Marshal(postMeta)
|
||||
|
||||
slugBasePath := filepath.Base(post.Slug)
|
||||
postFilename := filepath.Join(s.baseDir, fmt.Sprintf("posts/%04d/%02d/%02d-%s.md",
|
||||
post.PublishedAt.Year(), post.PublishedAt.Month(), post.PublishedAt.Day(), slugBasePath))
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(postFilename), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f, err := os.Create(postFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := f.WriteString("---\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = f.Write(frontMatter); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.WriteString("---\n")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = f.WriteString(post.Body)
|
||||
return err
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
package sitereader
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
type ReadSiteModels struct {
|
||||
Site models.Site
|
||||
Posts []*models.Post
|
||||
}
|
||||
|
||||
type siteMeta struct {
|
||||
Title string `yaml:"title"`
|
||||
Tagline string `yaml:"tagline"`
|
||||
BaseURL string `yaml:"base_url"`
|
||||
}
|
||||
|
||||
type postMeta struct {
|
||||
ID string `yaml:"id"`
|
||||
Title string `yaml:"title"`
|
||||
Date time.Time `yaml:"date"`
|
||||
Tags []string `yaml:"tags"`
|
||||
Slug string `yaml:"slug"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue