add endpoints to update article status
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
@@ -280,3 +280,45 @@ func (h *ApiHandler) ServeBlogFileGetSingle(writer http.ResponseWriter, request
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeBlogPostPublish(writer http.ResponseWriter, request *http.Request) {
|
||||
idStr := request.PathValue("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
WriteError(writer, http.StatusBadRequest, "invalid id", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.db.SetBlogArticleStatus(id, ArticleStatusPublished)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
WriteError(writer, http.StatusNotFound, "article not found", nil)
|
||||
} else {
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to update database", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
WriteResponse(writer, http.StatusOK, map[string]interface{}{})
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeBlogPostUnpublish(writer http.ResponseWriter, request *http.Request) {
|
||||
idStr := request.PathValue("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
WriteError(writer, http.StatusBadRequest, "invalid id", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = h.db.SetBlogArticleStatus(id, ArticleStatusOffline)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
WriteError(writer, http.StatusNotFound, "article not found", nil)
|
||||
} else {
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to update database", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
WriteResponse(writer, http.StatusOK, map[string]interface{}{})
|
||||
}
|
||||
|
||||
@@ -304,6 +304,24 @@ func (db *Database) GetBlogArticleFile(showAll bool, articleId int64, fileId int
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (db *Database) SetBlogArticleStatus(id int64, status ArticleStatus) error {
|
||||
res, err := db.db.Exec("UPDATE blog_article SET status = ? WHERE id = ?", status, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
affected, err := res.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if affected == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrate(db *sql.DB, rootPassword string) error {
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
|
||||
@@ -44,6 +44,12 @@ func main() {
|
||||
mux.Handle("GET /api/blog/{articleId}/file/{fileId}", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogFileGetSingle(writer, request)
|
||||
}), false))
|
||||
mux.Handle("POST /api/blog/{id}/publish", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogPostPublish(writer, request)
|
||||
}), true))
|
||||
mux.Handle("POST /api/blog/{id}/unpublish", apiHandler.ProcessAuth(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeBlogPostUnpublish(writer, request)
|
||||
}), true))
|
||||
mux.HandleFunc("POST /api/login", func(writer http.ResponseWriter, request *http.Request) {
|
||||
apiHandler.ServeLoginPost(writer, request)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user