Skip to content
Snippets Groups Projects
Commit cb50f841 authored by meskio's avatar meskio :tent:
Browse files

Add suppliers

parent 8fee840e
No related branches found
No related tags found
No related merge requests found
...@@ -48,6 +48,9 @@ func Init(dbPath string, signKey string, mail *Mail, r *mux.Router) error { ...@@ -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.UpdateProduct)).Methods("PUT")
r.HandleFunc("/product/{code:[0-9]+}", a.authAdmin(a.DeleteProduct)).Methods("DELETE") 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", a.authAdmin(a.ListTransactions)).Methods("GET")
r.HandleFunc("/transaction/{id:[0-9]+}", a.authNumRole(a.GetTransaction)).Methods("GET") r.HandleFunc("/transaction/{id:[0-9]+}", a.authNumRole(a.GetTransaction)).Methods("GET")
r.HandleFunc("/transaction/mine", a.authNum(a.getTransactionsByMember)).Methods("GET") r.HandleFunc("/transaction/mine", a.authNum(a.getTransactionsByMember)).Methods("GET")
......
...@@ -16,6 +16,6 @@ func Init(dbPath string) (*DB, error) { ...@@ -16,6 +16,6 @@ func Init(dbPath string) (*DB, error) {
} }
db.AutoMigrate(&Member{}, &Product{}, &Purchase{}, &Topup{}, &Transaction{}, db.AutoMigrate(&Member{}, &Product{}, &Purchase{}, &Topup{}, &Transaction{},
&OrderPurchase{}, &Order{}, &PasswordReset{}) &OrderPurchase{}, &Order{}, &PasswordReset{}, &Supplier{})
return &DB{db}, err return &DB{db}, err
} }
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
}
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
}
}
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)
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment