Multiple fixes and added article get endpoint for authors
This commit is contained in:
@@ -12,21 +12,75 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Article(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
assetsGet(w, r)
|
||||
case http.MethodPost:
|
||||
assetsPost(w, r)
|
||||
articlePost(w, r)
|
||||
case http.MethodDelete:
|
||||
assetsDelete(w, r)
|
||||
articleDelete(w, r)
|
||||
case http.MethodPatch:
|
||||
articlePatch(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
type getResponse struct {
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
Authors []int `json:"authors"`
|
||||
Image string `json:"image"`
|
||||
Tags []string `json:"tags"`
|
||||
Link string `json:"link"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
func articleGet(w http.ResponseWriter, r *http.Request) {
|
||||
authorId, ok := authorizedSession(r)
|
||||
if !ok {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
rawId := r.URL.Query().Get("id")
|
||||
|
||||
if rawId == "" {
|
||||
ApiError{Message: "no id was given", Code: http.StatusBadRequest}.Send(w)
|
||||
return
|
||||
}
|
||||
id, err := strconv.Atoi(rawId)
|
||||
if err != nil {
|
||||
ApiError{"invalid 'id' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
|
||||
if !database.Exists(database.GetDB().Table("article_author"), "author_id=?", authorId) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
var article database.Article
|
||||
if database.GetDB().Table("article").First(&article, id).RowsAffected == 0 {
|
||||
ApiError{Message: "no such id", Code: http.StatusNotFound}.Send(w)
|
||||
return
|
||||
}
|
||||
|
||||
resp := getResponse{
|
||||
Title: article.Title,
|
||||
Summary: article.Summary,
|
||||
Image: article.Image,
|
||||
Link: article.Link,
|
||||
Content: base64.StdEncoding.EncodeToString([]byte(article.Markdown)),
|
||||
}
|
||||
database.GetDB().Table("article_author").Select("author_id").Where("article_id", article.Id).Find(&resp.Authors)
|
||||
database.GetDB().Table("article_tag").Select("tag").Where("article_id", article.Id).Find(&resp.Tags)
|
||||
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
type uploadPayload struct {
|
||||
Title string `json:"title"`
|
||||
Summary string `json:"summary"`
|
||||
@@ -102,7 +156,7 @@ func articlePost(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
articleSummary.Tags = []string{}
|
||||
}
|
||||
articleSummary.Link = path.Join(config.Path, "article", url.PathEscape(articleSummary.Link))
|
||||
articleSummary.Link = path.Join("/", config.SubPath, "article", url.PathEscape(articleSummary.Link))
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(articleSummary)
|
||||
@@ -216,7 +270,7 @@ func articlePatch(w http.ResponseWriter, r *http.Request) {
|
||||
} else {
|
||||
articleSummary.Tags = []string{}
|
||||
}
|
||||
articleSummary.Link = path.Join(config.Path, "article", url.PathEscape(articleSummary.Link))
|
||||
articleSummary.Link = path.Join("/", config.SubPath, "article", url.PathEscape(articleSummary.Link))
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(articleSummary)
|
||||
|
||||
Reference in New Issue
Block a user