84 lines
2 KiB
Go
84 lines
2 KiB
Go
|
package controller
|
||
|
|
||
|
import (
|
||
|
"arimelody-web/model"
|
||
|
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
)
|
||
|
|
||
|
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)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
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"
|
||
|
}
|
||
|
|
||
|
var err error
|
||
|
if limit < 0 {
|
||
|
err = db.Select(&blogs, query)
|
||
|
} else {
|
||
|
err = db.Select(&blogs, 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)
|
||
|
// }
|
||
|
|
||
|
return blogs, nil
|
||
|
}
|
||
|
|
||
|
func CreateBlogPost(db *sqlx.DB, post *model.BlogPost) error {
|
||
|
_, 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,
|
||
|
)
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func UpdateBlogPost(db *sqlx.DB, postID string, post *model.BlogPost) error {
|
||
|
_, 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 " +
|
||
|
"WHERE id=$1",
|
||
|
postID,
|
||
|
post.ID,
|
||
|
post.Title,
|
||
|
post.Description,
|
||
|
post.Visible,
|
||
|
post.AuthorID,
|
||
|
post.Markdown,
|
||
|
post.HTML,
|
||
|
post.BlueskyActorID,
|
||
|
post.BlueskyPostID,
|
||
|
)
|
||
|
|
||
|
return err
|
||
|
}
|