add endpoints to get single blog article
Signed-off-by: Tobias Erbshäußer <tobias@tesoft.dev>
This commit is contained in:
+48
-4
@@ -32,8 +32,8 @@ type ArticleProperties struct {
|
||||
|
||||
type Article struct {
|
||||
ArticleProperties
|
||||
Content string
|
||||
Files []ArticleFile
|
||||
Content string `json:"content"`
|
||||
Files []ArticleFile `json:"-"`
|
||||
}
|
||||
|
||||
type ArticleFile struct {
|
||||
@@ -230,9 +230,53 @@ func (h *ApiHandler) ServeBlogPut(writer http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeBlogGetSingle(writer http.ResponseWriter, request *http.Request) {
|
||||
// TODO
|
||||
idStr := request.PathValue("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
WriteError(writer, http.StatusBadRequest, "invalid id", err)
|
||||
return
|
||||
}
|
||||
|
||||
article, err := h.db.GetBlogArticle(IsAuthorized(request), id)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
WriteError(writer, http.StatusNotFound, "article not found", nil)
|
||||
} else {
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to query database", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
WriteResponse(writer, http.StatusOK, article)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ServeBlogFileGetSingle(writer http.ResponseWriter, request *http.Request) {
|
||||
// TODO
|
||||
idStr := request.PathValue("articleId")
|
||||
articleId, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
WriteError(writer, http.StatusBadRequest, "invalid article id", err)
|
||||
return
|
||||
}
|
||||
|
||||
idStr = request.PathValue("fileId")
|
||||
fileId, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
WriteError(writer, http.StatusBadRequest, "invalid file id", err)
|
||||
return
|
||||
}
|
||||
|
||||
file, err := h.db.GetBlogArticleFile(IsAuthorized(request), articleId, fileId)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
WriteError(writer, http.StatusNotFound, "article file not found", nil)
|
||||
} else {
|
||||
WriteError(writer, http.StatusInternalServerError, "failed to query database", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
_, err = writer.Write(file.Data)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user