diff --git a/api/api.go b/api/api.go index 3801c1314fd490de40fa973c148417282e289455..d6ef8079d9129c6110444e87ec8ddf555e4b1ece 100644 --- a/api/api.go +++ b/api/api.go @@ -39,7 +39,8 @@ func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error { r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.GetMember)).Methods("GET") r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.UpdateMember)).Methods("PUT") r.HandleFunc("/member/{num:[0-9]+}", a.authAdmin(a.DeleteMember)).Methods("DELETE") - r.HandleFunc("/member/{num:[0-9]+}/purchase", a.authAdmin(a.GetMemberTransactions)).Methods("GET") + r.HandleFunc("/member/{num:[0-9]+}/transactions", a.authAdmin(a.GetMemberTransactions)).Methods("GET") + r.HandleFunc("/member/{num:[0-9]+}/purchase", a.authAdmin(a.AddMemberPurchase)).Methods("POST") r.HandleFunc("/product", a.auth(a.ListProducts)).Methods("GET") r.HandleFunc("/product", a.authAdmin(a.AddProduct)).Methods("POST") diff --git a/api/purchase.go b/api/purchase.go index bf8cc9c7039154caee1d5058877f7ded0767ddce..932bcabd9916afbb51c26a07dc70b4eb8d72251a 100644 --- a/api/purchase.go +++ b/api/purchase.go @@ -5,8 +5,10 @@ import ( "errors" "log" "net/http" + "strconv" "0xacab.org/meskio/cicer/api/db" + "github.com/gorilla/mux" ) func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) { @@ -40,3 +42,9 @@ func (a *api) AddPurchase(num int, w http.ResponseWriter, req *http.Request) { } } + +func (a *api) AddMemberPurchase(w http.ResponseWriter, req *http.Request) { + vars := mux.Vars(req) + num, _ := strconv.Atoi(vars["num"]) + a.AddPurchase(num, w, req) +} diff --git a/api/purchase_test.go b/api/purchase_test.go index 53a3cdb440f72d9ec0799427d4045c098a553291..322b4ce904090fde3e7a2ff5d28abdd23a5f069a 100644 --- a/api/purchase_test.go +++ b/api/purchase_test.go @@ -1,6 +1,7 @@ package api import ( + "fmt" "net/http" "testing" @@ -63,3 +64,60 @@ func TestPurchaseAddListMine(t *testing.T) { t.Error("Wrong product balance:", member.Balance) } } + +func TestMemberPurchase(t *testing.T) { + tapi := newTestAPI(t) + defer tapi.close() + tapi.addTestMember() + tapi.addTestProducts() + + products := []db.Purchase{ + { + ProductCode: testProduct.Code, + Amount: 5, + }, + } + resp := tapi.doAdmin("POST", fmt.Sprintf("/member/%d/purchase", testMember.Num), products, nil) + if resp.StatusCode != http.StatusCreated { + t.Fatal("Can't create purchase:", resp.Status) + } + var transactions []db.Transaction + resp = tapi.do("GET", "/transaction/mine", nil, &transactions) + if resp.StatusCode != http.StatusOK { + t.Fatal("Can't get transactions:", resp.Status) + } + + if len(transactions) != 1 { + t.Fatal("Wrong number of transactions", len(transactions), transactions) + } + if transactions[0].Total != -testProduct.Price*products[0].Amount { + t.Error("Wrong total:", transactions[0].Total) + } + if len(transactions[0].Purchase) != 1 { + t.Fatal("Wrong number of products", len(transactions[0].Purchase), transactions[0].Purchase) + } + if transactions[0].Purchase[0].ProductCode != testProduct.Code { + t.Error("Wrong product code:", transactions[0].Purchase[0].ProductCode) + } + if transactions[0].Purchase[0].Price != testProduct.Price { + t.Error("Wrong product price:", transactions[0].Purchase[0].Price) + } + + var product db.Product + resp = tapi.do("GET", "/product/234", nil, &product) + if resp.StatusCode != http.StatusOK { + t.Error("Can't find the product:", resp.Status) + } + if product.Stock != testProduct.Stock-products[0].Amount { + t.Error("Wrong product stock:", product) + } + + var member db.Member + resp = tapi.do("GET", "/member/me", nil, &member) + if resp.StatusCode != http.StatusOK { + t.Error("Can't find the member:", resp.Status) + } + if member.Balance != testMember.Balance-(testProduct.Price*products[0].Amount) { + t.Error("Wrong product balance:", member.Balance) + } +}