Skip to content

Commit

Permalink
feat(articles): single or multiple articles for ingest endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed Feb 14, 2020
1 parent 94a0cf3 commit 14a3740
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/api/articles.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package api

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"

"github.com/ncarlier/readflow/pkg/config"
Expand All @@ -15,11 +18,33 @@ func articles(conf *config.Config) http.Handler {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
articlesForm := []model.ArticleForm{}
articleForm := model.ArticleForm{}

if err := json.NewDecoder(r.Body).Decode(&articlesForm); err != nil {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
body = bytes.TrimSpace(body)
simpleObject := true
switch body[0] {
case byte('['):
simpleObject = false
err = json.Unmarshal(body, &articlesForm)
case byte('{'):
err = json.Unmarshal(body, &articleForm)
default: // ] or }
err = errors.New("Unexpected delimiter")
}

if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if simpleObject {
articlesForm = append(articlesForm, articleForm)
}

articles := service.Lookup().CreateArticles(ctx, articlesForm)

Expand Down

0 comments on commit 14a3740

Please sign in to comment.