Skip to content

Commit

Permalink
support alert for api
Browse files Browse the repository at this point in the history
  • Loading branch information
zealotnt committed Dec 23, 2016
1 parent 5670d51 commit 63ff631
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
2 changes: 2 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ dependencies:
test:
pre:
- go get bitbucket.org/zealotnt/goose/cmd/goose
- go get github.com/onsi/ginkgo/ginkgo
- go install github.com/onsi/ginkgo/ginkgo
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
override:
- cd $FULL_IMPORT_PATH && goose up
- cd $FULL_IMPORT_PATH && ./testcoverage.sh
6 changes: 2 additions & 4 deletions handlers/cart_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ func DeleteCartItemHandler(app *App) HandlerFunc {

func CheckoutCartHandler(app *App) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, c Context) {
var checkout CheckoutReturn

// Forward it to carts service
resp, body, err := RequestForwarder(r, app.CART_SERVICE, &checkout)
resp, body, err := RequestForwarder(r, app.CART_SERVICE, nil)
if err != nil {
if resp == nil {
JSON(w, err)
Expand All @@ -125,7 +123,7 @@ func CheckoutCartHandler(app *App) HandlerFunc {
}

// Temporary not support field selection
JSON(w, checkout)
WriteBody(w, body)
}
}

Expand Down
30 changes: 14 additions & 16 deletions handlers/products_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,37 @@ package handlers
import (
. "github.com/o0khoiclub0o/piflab-store-api-go/lib"
. "github.com/o0khoiclub0o/piflab-store-api-go/models"
. "github.com/o0khoiclub0o/piflab-store-api-go/models/form"
. "github.com/o0khoiclub0o/piflab-store-api-go/models/repository"

"net/http"
)

func GetProductsDetailHandler(app *App) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, c Context) {
// the purpose of form is to get form.Fields, so don't care about Binding errors
form := new(GetProductForm)
Bind(form, r)

product, err := (ProductRepository{app}).FindById(c.ID())
// Forward it to service
resp, body, err := RequestForwarder(r, app.PRODUCT_SERVICE, nil)
if err != nil {
JSON(w, err, 404)
if resp == nil {
JSON(w, err)
return
}
JSON(w, err, resp.StatusCode)
return
}

maps, err := FieldSelection(product, form.Fields)
if err != nil {
JSON(w, err)
if resp.Status != "200 OK" {
JSON(w, ParseError(body), resp.StatusCode)
return
}
JSON(w, maps)

// Temporary not support field selection
WriteBody(w, body)
}
}

func GetProductsHandler(app *App) HandlerFunc {
return func(w http.ResponseWriter, r *http.Request, c Context) {
var products_by_pages ProductPage

// Forward it to service
resp, body, err := RequestForwarder(r, app.PRODUCT_SERVICE, &products_by_pages)
resp, body, err := RequestForwarder(r, app.PRODUCT_SERVICE, nil)
if err != nil {
if resp == nil {
JSON(w, err)
Expand All @@ -50,7 +48,7 @@ func GetProductsHandler(app *App) HandlerFunc {
}

// Temporary not support field selection
JSON(w, products_by_pages)
WriteBody(w, body)
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"image"
"image/png"
"io"
"net/http"
"regexp"
)
Expand All @@ -15,6 +16,11 @@ func ValidateEmail(email string) bool {
return Re.MatchString(email)
}

func WriteBody(w http.ResponseWriter, body string) {
setHTTPStatus(w, nil)
io.WriteString(w, body)
}

func JSON(w http.ResponseWriter, params ...interface{}) {
setHTTPStatus(w, params)

Expand Down
10 changes: 8 additions & 2 deletions models/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ type CheckoutReturn struct {
type CheckoutReturnSlice []CheckoutReturn
type OrderSlice []Order

type Alert struct {
Type string
Message string
}

type Order struct {
Id uint `json:"-"`
AccessToken string `json:"access_token,omitempty"`
Status string `json:"status"`
Status string `json:"status,omitempty"`
StatusUpdated bool `json:"-" sql:"-"`

Items []OrderItem `json:"items" sql:"order_items"`
Expand All @@ -50,7 +55,8 @@ type Order struct {
OrderCodeRet string `json:"id,omitempty" sql:"-"`
OrderInfo `json:"-"`

Amounts Amount `json:"amounts" sql:"-"`
Amounts Amount `json:"amounts" sql:"-"`
Alerts []Alert `json:"alerts,omitempty" sql:"-"`

CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Expand Down
2 changes: 1 addition & 1 deletion testcoverage.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash -e

export TERM=linux
export TERM=xterm

echo "mode: atomic" > piflab-store-api-go.coverprofile

Expand Down

0 comments on commit 63ff631

Please sign in to comment.