huge blog refactor
tidying up data structures; improvements to blog admin UI/UX, etc.
This commit is contained in:
parent
eaa2f6587d
commit
0c2aaa0b38
18 changed files with 432 additions and 239 deletions
|
|
@ -2,6 +2,7 @@ package controller
|
|||
|
||||
import (
|
||||
"arimelody-web/model"
|
||||
"database/sql"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
|
@ -9,37 +10,96 @@ import (
|
|||
func GetBlogPost(db *sqlx.DB, id string) (*model.BlogPost, error) {
|
||||
var blog = model.BlogPost{}
|
||||
|
||||
err := db.Get(&blog, "SELECT * FROM blogpost WHERE id=$1", id)
|
||||
rows, err := db.Query(
|
||||
"SELECT post.id,post.title,post.description,post.visible," +
|
||||
"post.publish_date,post.author,post.markdown," +
|
||||
"post.bluesky_actor,post.bluesky_record," +
|
||||
"post.fediverse_account,post.fediverse_status," +
|
||||
"author.id,author.username,author.avatar_url " +
|
||||
"FROM blogpost AS post " +
|
||||
"JOIN account AS author ON post.author=author.id " +
|
||||
"WHERE post.id=$1",
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blueskyActor := sql.NullString{}
|
||||
blueskyRecord := sql.NullString{}
|
||||
fediverseAccount := sql.NullString{}
|
||||
fediverseStatus := sql.NullString{}
|
||||
|
||||
if !rows.Next() {
|
||||
return nil, nil
|
||||
}
|
||||
err = rows.Scan(
|
||||
&blog.ID, &blog.Title, &blog.Description, &blog.Visible,
|
||||
&blog.PublishDate, &blog.Author.ID, &blog.Markdown,
|
||||
&blueskyActor, &blueskyRecord,
|
||||
&fediverseAccount, &fediverseStatus,
|
||||
&blog.Author.ID, &blog.Author.DisplayName, &blog.Author.AvatarURL,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if blueskyActor.Valid && blueskyRecord.Valid {
|
||||
blog.Bluesky = &model.BlueskyRecord{
|
||||
ActorDID: blueskyActor.String,
|
||||
RecordID: blueskyRecord.String,
|
||||
}
|
||||
}
|
||||
if fediverseAccount.Valid && fediverseStatus.Valid {
|
||||
blog.Fediverse = &model.FediverseActivity{
|
||||
AccountID: fediverseAccount.String,
|
||||
StatusID: fediverseStatus.String,
|
||||
}
|
||||
}
|
||||
|
||||
return &blog, nil
|
||||
}
|
||||
|
||||
func GetBlogPosts(db *sqlx.DB, onlyVisible bool, limit int, offset int) ([]*model.BlogPost, error) {
|
||||
var blogs = []*model.BlogPost{}
|
||||
|
||||
query := "SELECT * FROM blogpost ORDER BY created_at"
|
||||
if onlyVisible {
|
||||
query = "SELECT * FROM blogpost WHERE visible=true ORDER BY created_at"
|
||||
}
|
||||
query := "SELECT post.id,post.title,post.publish_date," +
|
||||
"post.description,post.visible," +
|
||||
"author.id,author.username,author.avatar_url " +
|
||||
"FROM blogpost AS post " +
|
||||
"JOIN account AS author ON post.author=author.id"
|
||||
if onlyVisible { query += " WHERE visible=true" }
|
||||
query += " ORDER BY publish_date DESC"
|
||||
|
||||
var rows *sql.Rows
|
||||
|
||||
var err error
|
||||
if limit < 0 {
|
||||
err = db.Select(&blogs, query)
|
||||
rows, err = db.Query(query)
|
||||
} else {
|
||||
err = db.Select(&blogs, query + " LIMIT $1 OFFSET $2", limit, offset)
|
||||
rows, err = db.Query(query + " LIMIT $1 OFFSET $2", limit, offset)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// for range 4 {
|
||||
// blog := *blogs[len(blogs)-1]
|
||||
// blog.CreatedAt = blog.CreatedAt.Add(time.Hour * -5000)
|
||||
// blogs = append(blogs, &blog)
|
||||
// }
|
||||
for rows.Next() {
|
||||
blog := model.BlogPost{}
|
||||
err = rows.Scan(
|
||||
&blog.ID,
|
||||
&blog.Title,
|
||||
&blog.PublishDate,
|
||||
&blog.Description,
|
||||
&blog.Visible,
|
||||
&blog.Author.ID,
|
||||
&blog.Author.DisplayName,
|
||||
&blog.Author.AvatarURL,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blogs = append(blogs, &blog)
|
||||
}
|
||||
|
||||
return blogs, nil
|
||||
}
|
||||
|
|
@ -57,54 +117,77 @@ func GetBlogPostCount(db *sqlx.DB, onlyVisible bool) (int, error) {
|
|||
}
|
||||
|
||||
func CreateBlogPost(db *sqlx.DB, post *model.BlogPost) error {
|
||||
var blueskyActor *string
|
||||
var blueskyRecord *string
|
||||
if post.Bluesky != nil {
|
||||
blueskyActor = &post.Bluesky.ActorDID
|
||||
blueskyRecord = &post.Bluesky.RecordID
|
||||
}
|
||||
|
||||
var fediverseAccount *string
|
||||
var fediverseStatus *string
|
||||
if post.Fediverse != nil {
|
||||
fediverseAccount = &post.Fediverse.AccountID
|
||||
fediverseStatus = &post.Fediverse.StatusID
|
||||
}
|
||||
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO blogpost (id,title,description,visible,author,markdown,html,bluesky_actor,bluesky_post) " +
|
||||
"VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)",
|
||||
post.ID,
|
||||
post.Title,
|
||||
post.Description,
|
||||
post.Visible,
|
||||
post.AuthorID,
|
||||
post.Markdown,
|
||||
post.HTML,
|
||||
post.BlueskyActorID,
|
||||
post.BlueskyPostID,
|
||||
"INSERT INTO blogpost (" +
|
||||
"id,title,description,visible," +
|
||||
"publish_date,author,markdown," +
|
||||
"bluesky_actor,bluesky_record," +
|
||||
"fediverse_account,fediverse_status) " +
|
||||
"VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)",
|
||||
post.ID, post.Title, post.Description, post.Visible,
|
||||
post.PublishDate, post.Author.ID, post.Markdown,
|
||||
blueskyActor, blueskyRecord,
|
||||
fediverseAccount, fediverseStatus,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func UpdateBlogPost(db *sqlx.DB, postID string, post *model.BlogPost) error {
|
||||
var blueskyActor string
|
||||
var blueskyRecord string
|
||||
if post.Bluesky != nil {
|
||||
blueskyActor = post.Bluesky.ActorDID
|
||||
blueskyRecord = post.Bluesky.RecordID
|
||||
}
|
||||
|
||||
var fediverseAccount string
|
||||
var fediverseStatus string
|
||||
if post.Fediverse != nil {
|
||||
fediverseAccount = post.Fediverse.AccountID
|
||||
fediverseStatus = post.Fediverse.StatusID
|
||||
}
|
||||
|
||||
_, err := db.Exec(
|
||||
"UPDATE blogpost SET " +
|
||||
"id=$2,title=$3,description=$4,visible=$5,author=$6,markdown=$7,html=$8,bluesky_actor=$9,bluesky_post=$10,modified_at=CURRENT_TIMESTAMP " +
|
||||
"id=$2,title=$3,description=$4,visible=$5," +
|
||||
"publish_date=$6,author=$7,markdown=$8," +
|
||||
"bluesky_actor=$9,bluesky_record=$10," +
|
||||
"fediverse_account=$11,fediverse_status=$12 " +
|
||||
"WHERE id=$1",
|
||||
postID,
|
||||
post.ID,
|
||||
post.Title,
|
||||
post.Description,
|
||||
post.Visible,
|
||||
post.AuthorID,
|
||||
post.Markdown,
|
||||
post.HTML,
|
||||
post.BlueskyActorID,
|
||||
post.BlueskyPostID,
|
||||
post.ID, post.Title, post.Description, post.Visible,
|
||||
post.PublishDate, post.Author.ID, post.Markdown,
|
||||
blueskyActor, blueskyRecord,
|
||||
fediverseAccount, fediverseStatus,
|
||||
)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func DeleteBlogPost(db *sqlx.DB, postID string) (int64, error) {
|
||||
result, err := db.Exec(
|
||||
func DeleteBlogPost(db *sqlx.DB, postID string) error {
|
||||
_, err := db.Exec(
|
||||
"DELETE FROM blogpost "+
|
||||
"WHERE id=$1",
|
||||
postID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
return err
|
||||
}
|
||||
|
||||
rowsAffected, _ := result.RowsAffected()
|
||||
|
||||
return rowsAffected, nil
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue