add endpoint to query blog article tags
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
@@ -322,6 +322,54 @@ func (db *Database) SetBlogArticleStatus(id int64, status ArticleStatus) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Database) GetBlogTags(showAll bool, offset int, limit int) ([]string, int64, error) {
|
||||
filter := ""
|
||||
filterArgs := make([]interface{}, 0)
|
||||
|
||||
if !showAll {
|
||||
filter = " WHERE blog_article.status = ?"
|
||||
filterArgs = append(filterArgs, ArticleStatusPublished)
|
||||
}
|
||||
|
||||
args := make([]interface{}, 0, len(filterArgs)*2+2)
|
||||
args = append(args, filterArgs...)
|
||||
args = append(args, filterArgs...)
|
||||
args = append(args, limit)
|
||||
args = append(args, offset)
|
||||
|
||||
joins := " INNER JOIN blog_article_to_tag ON blog_article_to_tag.tag_id = blog_tag.id" +
|
||||
" INNER JOIN blog_article ON blog_article.id = blog_article_to_tag.article_id"
|
||||
|
||||
rows, err := db.db.Query(
|
||||
"SELECT blog_tag.name, (SELECT COUNT(*) FROM blog_tag"+joins+filter+") FROM blog_tag"+joins+filter+" LIMIT ? OFFSET ?",
|
||||
args...,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
tags := make([]string, 0)
|
||||
total := int64(0)
|
||||
|
||||
for rows.Next() {
|
||||
var name string
|
||||
err := rows.Scan(&name, &total)
|
||||
if err != nil {
|
||||
_ = rows.Close()
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
tags = append(tags, name)
|
||||
}
|
||||
|
||||
err = rows.Close()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return tags, total, nil
|
||||
}
|
||||
|
||||
func migrate(db *sql.DB, rootPassword string) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user