streamline response handling and logging
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
+11
-25
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"archive/tar"
|
||||
"backend/md"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -63,7 +62,7 @@ func ParseArticle(reader io.Reader, filePrefix string) (*Article, error) {
|
||||
|
||||
readmeBytes, found := tarFiles["README.md"]
|
||||
if !found {
|
||||
return nil, errors.New("README.md not found")
|
||||
return nil, errors.New("file 'README.md' not found")
|
||||
}
|
||||
|
||||
usedFiles := make(map[string]ArticleFile)
|
||||
@@ -163,7 +162,7 @@ func (h *ApiHandler) ServeBlogGet(writer http.ResponseWriter, request *http.Requ
|
||||
} else {
|
||||
offset, err = strconv.Atoi(offsetStr)
|
||||
if err != nil || offset < 0 {
|
||||
http.Error(writer, "invalid offset", http.StatusBadRequest)
|
||||
WriteError(writer, http.StatusBadRequest, "invalid offset", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -174,69 +173,56 @@ func (h *ApiHandler) ServeBlogGet(writer http.ResponseWriter, request *http.Requ
|
||||
} else {
|
||||
limit, err = strconv.Atoi(limitStr)
|
||||
if err != nil || limit <= 0 || limit > 100 {
|
||||
http.Error(writer, "invalid limit", http.StatusBadRequest)
|
||||
WriteError(writer, http.StatusBadRequest, "invalid limit", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
articles, err := h.db.GetBlogArticles(IsAuthorized(request), offset, limit)
|
||||
if err != nil {
|
||||
log.Println("Error getting articles:", err)
|
||||
http.Error(writer, "failed to query database", http.StatusInternalServerError)
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to query database", err)
|
||||
return
|
||||
}
|
||||
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
err = json.NewEncoder(writer).Encode(articles)
|
||||
if err != nil {
|
||||
http.Error(writer, "failed to serialize results", http.StatusInternalServerError)
|
||||
}
|
||||
WriteResponse(writer, http.StatusOK, articles)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeBlogPut(writer http.ResponseWriter, request *http.Request) {
|
||||
err := request.ParseMultipartForm(10 * 1024 * 1024)
|
||||
if err != nil {
|
||||
http.Error(writer, "failed to parse multipart form", http.StatusBadRequest)
|
||||
WriteError(writer, http.StatusBadRequest, "failed to parse multipart form", nil)
|
||||
return
|
||||
}
|
||||
|
||||
file, _, err := request.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(writer, "failed to parse file", http.StatusBadRequest)
|
||||
WriteError(writer, http.StatusBadRequest, "failed to parse file", nil)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
log.Println("Creating new blog article")
|
||||
log.Println("creating new blog article ...")
|
||||
|
||||
id, commit, err := h.db.CreateBlogArticle()
|
||||
if err != nil {
|
||||
log.Println("Error creating new blog article:", err)
|
||||
http.Error(writer, "failed to add article to database", http.StatusInternalServerError)
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to write database", err)
|
||||
return
|
||||
}
|
||||
|
||||
article, err := ParseArticle(file, "/api/blog/"+strconv.FormatInt(id, 10)+"/file/")
|
||||
if err != nil {
|
||||
_ = commit(nil)
|
||||
log.Println("Error creating new blog article:", err)
|
||||
http.Error(writer, "failed to add article to database", http.StatusInternalServerError)
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to parse article: "+err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
err = commit(article)
|
||||
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
err = json.NewEncoder(writer).Encode(map[string]interface{}{
|
||||
WriteResponse(writer, http.StatusOK, map[string]interface{}{
|
||||
"id": id,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(writer, "failed to serialize results", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeBlogGetSingle(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user