From cb50f841d6ed7278530310e0b05e4b67f4498fa3 Mon Sep 17 00:00:00 2001
From: meskio <meskio@sindominio.net>
Date: Sat, 20 Feb 2021 12:50:45 +0100
Subject: [PATCH] Add suppliers

---
 api/api.go            |  3 +++
 api/db/db.go          |  2 +-
 api/db/inventary.go   | 15 +++++++++++++
 api/inventary.go      | 51 +++++++++++++++++++++++++++++++++++++++++++
 api/inventary_test.go | 38 ++++++++++++++++++++++++++++++++
 5 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 api/db/inventary.go
 create mode 100644 api/inventary.go
 create mode 100644 api/inventary_test.go

diff --git a/api/api.go b/api/api.go
index 8a29074..2c852c5 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 6c0cd16..694ca20 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 0000000..9489f4f
--- /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 0000000..48e826a
--- /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 0000000..0d7ecc8
--- /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)
+	}
+}
-- 
GitLab