Added api endpoints and tests
This commit is contained in:
@@ -2,59 +2,77 @@ package api
|
||||
|
||||
import (
|
||||
"TheAdversary/database"
|
||||
"TheAdversary/schema"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Search(w http.ResponseWriter, r *http.Request) {
|
||||
var err error
|
||||
var q string
|
||||
var from, to time.Time
|
||||
var limit int64
|
||||
|
||||
query := r.URL.Query()
|
||||
q = query.Get("q")
|
||||
if f := query.Get("from"); f != "" {
|
||||
from, err = time.Parse(time.RFC3339, f)
|
||||
request := database.GetDB().Table("article")
|
||||
|
||||
if query.Has("q") {
|
||||
request.Where("LOWER(title) LIKE ?", "%"+query.Get("q")+"%")
|
||||
}
|
||||
if query.Has("from") {
|
||||
from, err := strconv.ParseInt(query.Get("from"), 10, 64)
|
||||
if err != nil {
|
||||
ApiError{"could not parse 'from' parameter as RFC3339 time", http.StatusUnprocessableEntity}.Send(w)
|
||||
ApiError{"invalid 'from' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
request.Where("created >= ?", from).Or("modified >= ?", from)
|
||||
}
|
||||
if t := query.Get("to"); t != "" {
|
||||
to, err = time.Parse(time.RFC3339, t)
|
||||
if query.Has("to") {
|
||||
to, err := strconv.ParseInt(query.Get("to"), 10, 64)
|
||||
if err != nil {
|
||||
ApiError{"could not parse 'to' parameter as RFC3339 time", http.StatusUnprocessableEntity}.Send(w)
|
||||
ApiError{"invalid 'to' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
request.Where("created <= ?", to).Or("modified <= ?", to)
|
||||
}
|
||||
if l := query.Get("limit"); l != "" {
|
||||
limit, err = strconv.ParseInt(l, 10, 64)
|
||||
if query.Has("authors") {
|
||||
var authorIds []int
|
||||
if err := json.NewDecoder(strings.NewReader(query.Get("authors"))).Decode(&authorIds); err != nil {
|
||||
ApiError{"could not parse 'authors' parameter as array of integers / numbers", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
request.Where("id IN (?)", database.GetDB().Table("article_author").Select("article_id").Where("author_id IN (?)", authorIds))
|
||||
}
|
||||
if query.Has("tags") {
|
||||
var tags []string
|
||||
if err := json.NewDecoder(strings.NewReader(query.Get("tags"))).Decode(&tags); err != nil {
|
||||
ApiError{"could not parse 'tags' parameter as array of strings", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
authorRequest := database.GetDB().Table("article_tag").Select("article_id").Where("tag IN ?", tags)
|
||||
request.Where("id IN (?)", authorRequest)
|
||||
}
|
||||
limit := 20
|
||||
if query.Has("limit") {
|
||||
var err error
|
||||
limit, err = strconv.Atoi(query.Get("limit"))
|
||||
if err != nil {
|
||||
ApiError{"invalid 'limit' parameter", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
} else if limit > 100 {
|
||||
ApiError{"'limit' parameter must not be over 100", http.StatusUnprocessableEntity}.Send(w)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
limit = 20
|
||||
}
|
||||
request.Limit(limit)
|
||||
|
||||
articles, err := database.GetDB().GetArticles(q, database.ArticleQueryOptions{
|
||||
Title: true,
|
||||
Summary: true,
|
||||
From: from.Unix(),
|
||||
To: to.Unix(),
|
||||
Limit: int(limit),
|
||||
})
|
||||
var articleSummaries []schema.ArticleSummary
|
||||
request.Find(&articleSummaries)
|
||||
|
||||
var articleSummaries []database.ArticleSummary
|
||||
for _, article := range articles {
|
||||
articleSummaries = append(articleSummaries, article.ToArticleSummary())
|
||||
for i, summary := range articleSummaries {
|
||||
database.GetDB().Table("author").Where("id IN (?)", database.GetDB().Table("article_author").Select("author_id").Where("article_id = ?", summary.Id)).Find(&summary.Authors)
|
||||
summary.Tags = []string{}
|
||||
database.GetDB().Table("article_tag").Select("tag").Where("article_id = ?", summary.Id).Find(&summary.Tags)
|
||||
articleSummaries[i] = summary
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(struct {
|
||||
Articles []database.ArticleSummary `json:"articles"`
|
||||
}{articleSummaries})
|
||||
json.NewEncoder(w).Encode(articleSummaries)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user