From c428429a99354935a194e76238ebe63380892059 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Thu, 30 Mar 2017 19:05:33 +0200 Subject: [PATCH] [TASK] add review route by product id --- http/main.go | 2 +- http/review.go | 17 ++++++++++++++++- http/review_test.go | 27 ++++++++++++++++++++------- http/status_test.go | 2 ++ test/http.go | 11 ++++++++++- 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/http/main.go b/http/main.go index c793174..d58b990 100644 --- a/http/main.go +++ b/http/main.go @@ -7,5 +7,5 @@ import ( func BindAPI(router *goji.Mux) { router.HandleFunc(pat.Get("/api/status"), status) - router.HandleFunc(pat.Get("/api/reviews"), listReview) + router.HandleFunc(pat.Get("/api/review/:productid"), listReview) } diff --git a/http/review.go b/http/review.go index 94ea9d5..ce1fbee 100644 --- a/http/review.go +++ b/http/review.go @@ -2,6 +2,9 @@ package http import ( "net/http" + "strconv" + + "goji.io/pat" "github.com/genofire/hs_master-kss-monolith/lib/database" lib "github.com/genofire/hs_master-kss-monolith/lib/http" @@ -11,8 +14,20 @@ import ( func listReview(w http.ResponseWriter, r *http.Request) { log := logger.HTTP(r) + id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) + if err != nil { + log.Warn("wrong productid format") + http.Error(w, "wrong productid", http.StatusNotAcceptable) + return + } + log.WithField("productid", id) var list []*models.Review - database.Read.Find(&list) + result := database.Read.Where("product_id = ?", id).Find(&list) + if result.RowsAffected == 0 { + log.Warn("no reviews found") + http.Error(w, "no reviews found", http.StatusNotFound) + return + } lib.Write(w, list) log.Info("done") } diff --git a/http/review_test.go b/http/review_test.go index dd64844..a1bd9fb 100644 --- a/http/review_test.go +++ b/http/review_test.go @@ -5,21 +5,34 @@ import ( "testing" "github.com/genofire/hs_master-kss-monolith/lib/database" + "github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/test" ) func TestReview(t *testing.T) { - database.Open(database.Config{ - Type: "sqlite3", - Connection: ":memory:", - }) - database.Write.LogMode(true) assertion, router := test.Init(t) + BindAPI(router) session := test.NewSession(router) - result, w := session.JSONRequest("GET", "/api/reviews", nil) + result, w := session.JSONRequest("GET", "/api/review/a", nil) + assertion.Equal(http.StatusNotAcceptable, w.StatusCode) + + result, w = session.JSONRequest("GET", "/api/review/1", nil) + assertion.Equal(http.StatusNotFound, w.StatusCode) + + database.Write.Create(&models.Review{ + ProductID: 3, + FirstName: "Max", + LastName: "Mustmann", + RatingStars: 3, + Text: "blub", + }) + + result, w = session.JSONRequest("GET", "/api/review/3", nil) assertion.Equal(http.StatusOK, w.StatusCode) - assertion.Equal([]interface{}{}, result) + assertion.Len(result, 1) + + test.Close() } diff --git a/http/status_test.go b/http/status_test.go index 01db01d..0ec61db 100644 --- a/http/status_test.go +++ b/http/status_test.go @@ -9,6 +9,7 @@ import ( func TestStatus(t *testing.T) { assertion, router := test.Init(t) + BindAPI(router) session := test.NewSession(router) @@ -18,4 +19,5 @@ func TestStatus(t *testing.T) { assertion.Equal("running", result["status"]) assertion.Equal(float64(0), result["review_count"]) + test.Close() } diff --git a/test/http.go b/test/http.go index a79237f..bbca9b3 100644 --- a/test/http.go +++ b/test/http.go @@ -8,6 +8,7 @@ import ( "net/http/httptest" "testing" + "github.com/genofire/hs_master-kss-monolith/lib/database" "github.com/stretchr/testify/assert" goji "goji.io" @@ -16,11 +17,19 @@ import ( //Init to initialisieren a API func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { assertion = assert.New(t) - + database.Open(database.Config{ + Type: "sqlite3", + Connection: ":memory:", + }) + database.Write.LogMode(true) router = goji.NewMux() return } +func Close() { + database.Close() +} + type Request struct { req *http.Request cookies []*http.Cookie