Initial release
This commit is contained in:
39
api/recent.go
Normal file
39
api/recent.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func Recent(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var limit int64
|
||||
|
||||
query := r.URL.Query()
|
||||
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)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
limit = 20
|
||||
}
|
||||
|
||||
articles, err := database.GetDB().GetArticles("", database.ArticleQueryOptions{
|
||||
Limit: int(limit),
|
||||
})
|
||||
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []schema.ArticleSummary
|
||||
}{articleSummaries})
|
||||
}
|
||||
Reference in New Issue
Block a user