Moved away from local files to complete database driven articles
This commit is contained in:
@@ -2,7 +2,6 @@ package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -20,21 +19,21 @@ func Search(w http.ResponseWriter, r *http.Request) {
|
||||
if f := query.Get("from"); f != "" {
|
||||
from, err = time.Parse(time.RFC3339, f)
|
||||
if err != nil {
|
||||
ApiError{"could not parse 'from' parameter as RFC3339 time", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
ApiError{"could not parse 'from' parameter as RFC3339 time", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
if t := query.Get("to"); t != "" {
|
||||
to, err = time.Parse(time.RFC3339, t)
|
||||
if err != nil {
|
||||
ApiError{"could not parse 'to' parameter as RFC3339 time", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
ApiError{"could not parse 'to' parameter as RFC3339 time", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
}
|
||||
if l := query.Get("limit"); l != "" {
|
||||
limit, err = strconv.ParseInt(l, 10, 64)
|
||||
if err != nil {
|
||||
ApiError{"invalid 'limit' parameter", err, http.StatusUnprocessableEntity}.Send(w)
|
||||
ApiError{"invalid 'limit' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
@@ -49,13 +48,13 @@ func Search(w http.ResponseWriter, r *http.Request) {
|
||||
Limit: int(limit),
|
||||
})
|
||||
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
var articleSummaries []database.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []schema.ArticleSummary
|
||||
Articles []database.ArticleSummary `json:"articles"`
|
||||
}{articleSummaries})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user