commit dcddf5796323d746d80bbe0fc18ec4e810b4abc1
parent 697d1575d40bd8b83d714963b27ed798edb07765
Author: MTRNord <mtrnord1@gmail.com>
Date: Mon, 19 Feb 2018 17:13:53 +0100
Simplify Code
Diffstat:
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/api/webserver.go b/api/webserver.go
@@ -52,6 +52,7 @@ type Author struct {
}
func Papers(w http.ResponseWriter, r *http.Request) {
+ var data []byte
switch r.Method {
case "GET":
papers, err := getPapers()
@@ -59,10 +60,7 @@ func Papers(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
- w.Header().Set("Content-Type", "application/json")
- w.Header().Set("API-VERSION", "0.0.0")
- w.WriteHeader(http.StatusOK)
- w.Write(papers)
+ data = papers
case "POST":
decoder := json.NewDecoder(r.Body)
var t []Paper
@@ -76,12 +74,12 @@ func Papers(w http.ResponseWriter, r *http.Request) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
-
- w.Header().Set("Content-Type", "application/json")
- w.Header().Set("API-VERSION", "0.0.0")
- w.WriteHeader(http.StatusOK)
- w.Write(papers)
+ data = papers
}
+ w.Header().Set("Content-Type", "application/json")
+ w.Header().Set("API-VERSION", "0.0.0")
+ w.WriteHeader(http.StatusOK)
+ w.Write(data)
}
func addPapers(data []Paper) ([]byte, error) {