genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] add create good to api

This commit is contained in:
Martin Geno 2017-04-28 12:05:58 +02:00
parent 4801c0421b
commit 48150ba6a2
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
5 changed files with 93 additions and 52 deletions

View File

@ -4,6 +4,9 @@ package http
import ( import (
goji "goji.io" goji "goji.io"
"goji.io/pat" "goji.io/pat"
"github.com/genofire/hs_master-kss-monolith/lib/http"
"github.com/genofire/hs_master-kss-monolith/runtime"
) )
// bind all API routes to webserver // bind all API routes to webserver
@ -11,4 +14,5 @@ func BindAPI(router *goji.Mux) {
router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/status"), status)
router.HandleFunc(pat.Get("/api/good/:productid"), listGoods) router.HandleFunc(pat.Get("/api/good/:productid"), listGoods)
router.HandleFunc(pat.Get("/api/good/availablity/:productid"), getGoodAvailablity) router.HandleFunc(pat.Get("/api/good/availablity/:productid"), getGoodAvailablity)
router.HandleFunc(pat.Post("/api/good/:productid"), http.PermissionHandler(addGood, runtime.HasPermission, runtime.PermissionCreateGood))
} }

View File

@ -4,17 +4,15 @@ import (
"net/http" "net/http"
"strconv" "strconv"
logrus "github.com/Sirupsen/logrus"
"goji.io/pat" "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"
logger "github.com/genofire/hs_master-kss-monolith/lib/log" logger "github.com/genofire/hs_master-kss-monolith/lib/log"
"github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/models"
"github.com/genofire/hs_master-kss-monolith/runtime"
) )
func listGoods(w http.ResponseWriter, r *http.Request) { func addGood(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64)
if err != nil { if err != nil {
@ -23,53 +21,17 @@ func listGoods(w http.ResponseWriter, r *http.Request) {
return return
} }
log = log.WithField("productid", id) log = log.WithField("productid", id)
var list []*models.Good var obj *models.Good
result := database.Read.Where("product_id = ?", id).Find(&list) lib.Read(r, obj)
if result.RowsAffected == 0 {
log.Warn("no goods found") obj.ProductID = id
http.Error(w, "no goods found", http.StatusNotFound)
return db := database.Write.Create(obj)
} if db.Error != nil {
lib.Write(w, list) log.Error("database could not write", db.Error)
log.Info("done") http.Error(w, "was not possible to write", http.StatusInternalServerError)
}
func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logrus.Entry) {
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 -1, log
}
log = log.WithField("productid", id)
ok, err := runtime.ProductExists(id)
if err != nil {
log.Warn("product could not verified on other microservice")
http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout)
return -1, log
}
if !ok {
log.Warn("product did not exists anymore")
http.Error(w, "product did not exists anymore", http.StatusNotFound)
return -1, log
}
var count float64
(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", id)).Count(&count)
return int(count), log
}
func getGoodAvailablity(w http.ResponseWriter, r *http.Request) {
count, log := getGoodAvailablityCount(w, r)
if count < 0 {
return
}
log = log.WithField("type", r.Header.Get("Content-Type"))
switch r.Header.Get("Content-Type") {
case "application/json":
lib.Write(w, count)
default:
getGoodAvailablitySVG(w, count)
} }
lib.Write(w, obj)
log.Info("done") log.Info("done")
} }

75
http/good_show.go Normal file
View File

@ -0,0 +1,75 @@
package http
import (
"net/http"
"strconv"
logrus "github.com/Sirupsen/logrus"
"goji.io/pat"
"github.com/genofire/hs_master-kss-monolith/lib/database"
lib "github.com/genofire/hs_master-kss-monolith/lib/http"
logger "github.com/genofire/hs_master-kss-monolith/lib/log"
"github.com/genofire/hs_master-kss-monolith/models"
"github.com/genofire/hs_master-kss-monolith/runtime"
)
func listGoods(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 = log.WithField("productid", id)
var list []*models.Good
result := database.Read.Where("product_id = ?", id).Find(&list)
if result.RowsAffected == 0 {
log.Warn("no goods found")
http.Error(w, "no goods found", http.StatusNotFound)
return
}
lib.Write(w, list)
log.Info("done")
}
func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logrus.Entry) {
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 -1, log
}
log = log.WithField("productid", id)
ok, err := runtime.ProductExists(id)
if err != nil {
log.Warn("product could not verified on other microservice")
http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout)
return -1, log
}
if !ok {
log.Warn("product did not exists anymore")
http.Error(w, "product did not exists anymore", http.StatusNotFound)
return -1, log
}
var count float64
(&models.Good{}).FilterAvailable(database.Read.Where("product_id = ?", id)).Count(&count)
return int(count), log
}
func getGoodAvailablity(w http.ResponseWriter, r *http.Request) {
count, log := getGoodAvailablityCount(w, r)
if count < 0 {
return
}
log = log.WithField("type", r.Header.Get("Content-Type"))
switch r.Header.Get("Content-Type") {
case "application/json":
lib.Write(w, count)
default:
getGoodAvailablitySVG(w, count)
}
log.Info("done")
}

View File

@ -57,7 +57,7 @@ func init() {
} }
// check if the client with the session string has a permissions (see Permission) // check if the client with the session string has a permissions (see Permission)
func HasPermission(session string, p Permission) (bool, error) { func HasPermission(session string, p int) (bool, error) {
_, ok := permissionCache[session] _, ok := permissionCache[session]
if !ok { if !ok {
permissionCache[session] = &permissionMicroServiceCache{ permissionCache[session] = &permissionMicroServiceCache{
@ -66,5 +66,5 @@ func HasPermission(session string, p Permission) (bool, error) {
permissions: make(map[Permission]boolMicroServiceCache), permissions: make(map[Permission]boolMicroServiceCache),
} }
} }
return permissionCache[session].HasPermission(p) return permissionCache[session].HasPermission(Permission(p))
} }