diff --git a/api/api.go b/api/api.go index 8a29074d3771099fe4111f1916c74089757e9cec..2c852c5c82d578ca8b57f4689f798f89f05c0303 100644 --- a/api/api.go +++ b/api/api.go @@ -48,6 +48,9 @@ func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error { r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.UpdateProduct)).Methods("PUT") r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.DeleteProduct)).Methods("DELETE") + r.HandleFunc("/supplier", a.auth(a.ListSuppliers)).Methods("GET") + r.HandleFunc("/supplier", a.authAdmin(a.AddSupplier)).Methods("POST") + r.HandleFunc("/transaction", a.authAdmin(a.ListTransactions)).Methods("GET") r.HandleFunc("/transaction/{id:[0-9]+}", a.authNumRole(a.GetTransaction)).Methods("GET") r.HandleFunc("/transaction/mine", a.authNum(a.getTransactionsByMember)).Methods("GET") diff --git a/api/db/db.go b/api/db/db.go index 6c0cd164f8602aca28b76e1ae14a0aad5cfb4742..694ca20bcbcfc6fe4096afd3e833425d43bcc099 100644 --- a/api/db/db.go +++ b/api/db/db.go @@ -16,6 +16,6 @@ func Init(dbPath string) (*DB, error) { } db.AutoMigrate(&Member{}, &Product{}, &Purchase{}, &Topup{}, &Transaction{}, - &OrderPurchase{}, &Order{}, &PasswordReset{}) + &OrderPurchase{}, &Order{}, &PasswordReset{}, &Supplier{}) return &DB{db}, err } diff --git a/api/db/inventary.go b/api/db/inventary.go new file mode 100644 index 0000000000000000000000000000000000000000..9489f4fb27fae3c4dd86ebc70822d3d61364fd34 --- /dev/null +++ b/api/db/inventary.go @@ -0,0 +1,15 @@ +package db + +type Supplier struct { + gorm.Model + Name string `json:"name"` +} + +func (d *DB) AddSupplier(supplier Supplier) error { + return d.db.Create(&supplier).Error +} + +func (d *DB) ListSuppliers() (suppliers []Supplier, err error) { + err = d.db.Find(&suppliers).Error + return +} diff --git a/api/inventary.go b/api/inventary.go new file mode 100644 index 0000000000000000000000000000000000000000..48e826ab00c4c3a1ed49691ef0f989e3663f9701 --- /dev/null +++ b/api/inventary.go @@ -0,0 +1,51 @@ +package api + +import ( + "encoding/json" + "log" + "net/http" + + "0xacab.org/meskio/cicer/api/db" +) + +func (a *api) AddSupplier(w http.ResponseWriter, req *http.Request) { + var supplier db.Supplier + err := json.NewDecoder(req.Body).Decode(&supplier) + if err != nil { + log.Printf("Can't decode supplier: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + err = a.db.AddSupplier(supplier) + if err != nil { + log.Printf("Can't create supplier: %v\n%v", err, supplier) + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + err = json.NewEncoder(w).Encode(supplier) + if err != nil { + log.Printf("Can't encode added supplier: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } +} + +func (a *api) ListSuppliers(w http.ResponseWriter, req *http.Request) { + suppliers, err := a.db.ListSuppliers() + if err != nil { + log.Printf("Can't list suppliers: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(suppliers) + if err != nil { + log.Printf("Can't encode suppliers: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } +} diff --git a/api/inventary_test.go b/api/inventary_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0d7ecc88c8fc9fdf8b1d344c69620e561788f46b --- /dev/null +++ b/api/inventary_test.go @@ -0,0 +1,38 @@ +package api + +import ( + "net/http" + "testing" + + "0xacab.org/meskio/cicer/api/db" +) + +var testSupplier = db.Supplier{ + Name: "Aceites Geronimo", +} + +func TestSupplierAddList(t *testing.T) { + tapi := newTestAPI(t) + defer tapi.close() + tapi.addTestSuppliers() + + var suppliers []db.Supplier + resp := tapi.do("GET", "/supplier", nil, &suppliers) + if resp.StatusCode != http.StatusOK { + t.Fatal("Can't get suppliers:", resp.Status) + } + + if len(suppliers) != 1 { + t.Fatal("Wrong number of suppliers", len(suppliers), suppliers) + } + if suppliers[0].Name != testSupplier.Name { + t.Error("Wrong name:", suppliers[0].Name) + } +} + +func (tapi *testAPI) addTestSuppliers() { + resp := tapi.doAdmin("POST", "/supplier", testSupplier, nil) + if resp.StatusCode != http.StatusCreated { + tapi.t.Fatal("Can't create supplier:", resp.Status) + } +}