Have got post creation working.
This commit is contained in:
parent
63b19a249a
commit
8e0ffb6c24
20 changed files with 479 additions and 11 deletions
46
providers/db/posts.go
Normal file
46
providers/db/posts.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"lmika.dev/lmika/hugo-crm/gen/sqlc/dbq"
|
||||
"lmika.dev/lmika/hugo-crm/models"
|
||||
"lmika.dev/pkg/modash/moslice"
|
||||
)
|
||||
|
||||
func (db *DB) ListPostsOfSite(ctx context.Context, siteID int64) ([]models.Post, error) {
|
||||
res, err := db.q.ListPosts(ctx, siteID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return moslice.Map(res, func(p dbq.Post) models.Post {
|
||||
return models.Post{
|
||||
ID: p.ID,
|
||||
SiteID: p.SiteID,
|
||||
Title: p.Title.String,
|
||||
Body: p.Body,
|
||||
State: models.PostState(p.State),
|
||||
PostDate: p.PostDate.Time,
|
||||
CreatedAt: p.CreatedAt.Time,
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (db *DB) InsertPost(ctx context.Context, p *models.Post) error {
|
||||
res, err := db.q.InsertPost(ctx, dbq.InsertPostParams{
|
||||
SiteID: p.SiteID,
|
||||
Title: pgtype.Text{String: p.Title, Valid: p.Title != ""},
|
||||
Body: p.Body,
|
||||
State: dbq.PostState(p.State),
|
||||
Props: []byte(`{}`),
|
||||
PostDate: pgtype.Timestamptz{Time: p.PostDate, Valid: !p.PostDate.IsZero()},
|
||||
CreatedAt: pgtype.Timestamp{Time: p.CreatedAt, Valid: !p.CreatedAt.IsZero()},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.ID = res
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue