add endpoint to query blog article tags

Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
2026-05-24 09:22:51 +02:00
parent 55320829da
commit 996a538704
3 changed files with 92 additions and 0 deletions
+41
View File
@@ -322,3 +322,44 @@ func (h *ApiHandler) ServeBlogPostUnpublish(writer http.ResponseWriter, request
WriteResponse(writer, http.StatusOK, map[string]interface{}{}) WriteResponse(writer, http.StatusOK, map[string]interface{}{})
} }
func (h *ApiHandler) ServeBlogTagsGet(writer http.ResponseWriter, request *http.Request) {
query := request.URL.Query()
var err error
var offset int
var limit int
offsetStr := query.Get("offset")
if offsetStr == "" {
offset = 0
} else {
offset, err = strconv.Atoi(offsetStr)
if err != nil || offset < 0 {
WriteError(writer, http.StatusBadRequest, "invalid offset", nil)
return
}
}
limitStr := query.Get("limit")
if limitStr == "" {
limit = 50
} else {
limit, err = strconv.Atoi(limitStr)
if err != nil || limit <= 0 || limit > 100 {
WriteError(writer, http.StatusBadRequest, "invalid limit", nil)
return
}
}
tags, total, err := h.db.GetBlogTags(IsAuthorized(request), offset, limit)
if err != nil {
WriteError(writer, http.StatusInternalServerError, "failed to query database", err)
return
}
WriteResponse(writer, http.StatusOK, map[string]interface{}{
"tags": tags,
"total": total,
})
}
+48
View File
@@ -322,6 +322,54 @@ func (db *Database) SetBlogArticleStatus(id int64, status ArticleStatus) error {
return nil 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 { func migrate(db *sql.DB, rootPassword string) error {
tx, err := db.Begin() tx, err := db.Begin()
if err != nil { if err != nil {
+3
View File
@@ -50,6 +50,9 @@ func main() {
mux.Handle("POST /api/blog/{id}/unpublish", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) { mux.Handle("POST /api/blog/{id}/unpublish", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
apiHandler.ServeBlogPostUnpublish(writer, request) apiHandler.ServeBlogPostUnpublish(writer, request)
}), true)) }), true))
mux.Handle("GET /api/blog/tags", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
apiHandler.ServeBlogTagsGet(writer, request)
}), false))
mux.HandleFunc("POST /api/login", func(writer http.ResponseWriter, request *http.Request) { mux.HandleFunc("POST /api/login", func(writer http.ResponseWriter, request *http.Request) {
apiHandler.ServeLoginPost(writer, request) apiHandler.ServeLoginPost(writer, request)
}) })