genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] add review route by product id

This commit is contained in:
Martin Geno 2017-03-30 19:05:33 +02:00
parent 07eb1e384b
commit c428429a99
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
5 changed files with 49 additions and 10 deletions

View File

@ -7,5 +7,5 @@ import (
func BindAPI(router *goji.Mux) { func BindAPI(router *goji.Mux) {
router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/status"), status)
router.HandleFunc(pat.Get("/api/reviews"), listReview) router.HandleFunc(pat.Get("/api/review/:productid"), listReview)
} }

View File

@ -2,6 +2,9 @@ package http
import ( import (
"net/http" "net/http"
"strconv"
"goji.io/pat"
"github.com/genofire/hs_master-kss-monolith/lib/database" "github.com/genofire/hs_master-kss-monolith/lib/database"
lib "github.com/genofire/hs_master-kss-monolith/lib/http" lib "github.com/genofire/hs_master-kss-monolith/lib/http"
@ -11,8 +14,20 @@ import (
func listReview(w http.ResponseWriter, r *http.Request) { func listReview(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) 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 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) lib.Write(w, list)
log.Info("done") log.Info("done")
} }

View File

@ -5,21 +5,34 @@ import (
"testing" "testing"
"github.com/genofire/hs_master-kss-monolith/lib/database" "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" "github.com/genofire/hs_master-kss-monolith/test"
) )
func TestReview(t *testing.T) { func TestReview(t *testing.T) {
database.Open(database.Config{
Type: "sqlite3",
Connection: ":memory:",
})
database.Write.LogMode(true)
assertion, router := test.Init(t) assertion, router := test.Init(t)
BindAPI(router) BindAPI(router)
session := test.NewSession(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(http.StatusOK, w.StatusCode)
assertion.Equal([]interface{}{}, result) assertion.Len(result, 1)
test.Close()
} }

View File

@ -9,6 +9,7 @@ import (
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
assertion, router := test.Init(t) assertion, router := test.Init(t)
BindAPI(router) BindAPI(router)
session := test.NewSession(router) session := test.NewSession(router)
@ -18,4 +19,5 @@ func TestStatus(t *testing.T) {
assertion.Equal("running", result["status"]) assertion.Equal("running", result["status"])
assertion.Equal(float64(0), result["review_count"]) assertion.Equal(float64(0), result["review_count"])
test.Close()
} }

View File

@ -8,6 +8,7 @@ import (
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/genofire/hs_master-kss-monolith/lib/database"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
goji "goji.io" goji "goji.io"
@ -16,11 +17,19 @@ import (
//Init to initialisieren a API //Init to initialisieren a API
func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) {
assertion = assert.New(t) assertion = assert.New(t)
database.Open(database.Config{
Type: "sqlite3",
Connection: ":memory:",
})
database.Write.LogMode(true)
router = goji.NewMux() router = goji.NewMux()
return return
} }
func Close() {
database.Close()
}
type Request struct { type Request struct {
req *http.Request req *http.Request
cookies []*http.Cookie cookies []*http.Cookie