Fixed Route Bug with $ Char
This commit is contained in:
parent
dd8d914b33
commit
18e2d9a359
|
|
@ -31,7 +31,7 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//Dependency Injection
|
//Dependency Injection
|
||||||
http.HandleFunc("/api/products$", api.GetProductApiHandleFunc(nps))
|
http.HandleFunc("/api/products", api.GetProductApiHandleFunc(nps))
|
||||||
http.HandleFunc("/api/products/reserve", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/products/reserve", func(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Println("In Arbeit: reserve")
|
fmt.Println("In Arbeit: reserve")
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
|
@ -16,7 +15,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DatabaseWriter struct {
|
type DatabaseWriter struct {
|
||||||
mu sync.Mutex
|
|
||||||
log logger.Logger
|
log logger.Logger
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
@ -93,11 +91,11 @@ func (d *DatabaseWriter) createReservationTableIfNotExist() error {
|
||||||
CONSTRAINT fk_delivery_id
|
CONSTRAINT fk_delivery_id
|
||||||
FOREIGN KEY (deliveryId) REFERENCES deliverytimes (id)
|
FOREIGN KEY (deliveryId) REFERENCES deliverytimes (id)
|
||||||
ON DELETE CASCADE
|
ON DELETE CASCADE
|
||||||
ON UPDATE CASCADE,
|
ON UPDATE RESTRICT,
|
||||||
CONSTRAINT fk_warehouse_id
|
CONSTRAINT fk_warehouse_id
|
||||||
FOREIGN KEY (warehouseId) REFERENCES warehouseproducts (id)
|
FOREIGN KEY (warehouseId) REFERENCES warehouseproducts (id)
|
||||||
ON DELETE CASCADE
|
ON DELETE CASCADE
|
||||||
ON UPDATE CASCADE
|
ON UPDATE RESTRICT
|
||||||
) ENGINE=InnoDB;`)
|
) ENGINE=InnoDB;`)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -159,17 +157,18 @@ func (d *DatabaseWriter) UpdateOrInsertDelivery(fromcountry string, tocountry st
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProductDelivery struct {
|
type ProductDelivery struct {
|
||||||
Id int
|
WhId int
|
||||||
Warehouse string
|
Warehouse string
|
||||||
Amount int
|
Amount int
|
||||||
DeliveryDays int
|
DeliveryDays int
|
||||||
|
DeliveryId int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, delivery_country, delivery_country_state string) ([]ProductDelivery, error) {
|
func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, delivery_country, delivery_country_state string) ([]ProductDelivery, error) {
|
||||||
stmt := `
|
stmt := `
|
||||||
SELECT whp.id, warehouse, amount, d.delivery
|
SELECT whp.id, warehouse, amount, d.delivery, d.id deliveryId
|
||||||
FROM warehouseproducts whp
|
FROM warehouseproducts whp
|
||||||
left join deliverytimes d
|
join deliverytimes d
|
||||||
on whp.warehouse = d.fromcountry
|
on whp.warehouse = d.fromcountry
|
||||||
where productid = ?
|
where productid = ?
|
||||||
and d.tocountry = ?
|
and d.tocountry = ?
|
||||||
|
|
@ -182,17 +181,14 @@ func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, d
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
var products []ProductDelivery
|
var products []ProductDelivery
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var pd ProductDelivery
|
var pd ProductDelivery
|
||||||
if err := rows.Scan(&pd.Id, &pd.Warehouse, &pd.Amount, &pd.DeliveryDays); err != nil {
|
if err := rows.Scan(&pd.WhId, &pd.Warehouse, &pd.Amount, &pd.DeliveryDays, &pd.DeliveryId); err != nil {
|
||||||
return products, err
|
return products, err
|
||||||
}
|
}
|
||||||
products = append(products, pd)
|
products = append(products, pd)
|
||||||
}
|
}
|
||||||
|
|
||||||
return products, nil
|
return products, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,23 +210,24 @@ func (d *DatabaseReader) GetReservationStateById(productId UUID) (string, error)
|
||||||
return status, nil
|
return status, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DatabaseWriter) updateReservationState(productId UUID, status string) error {
|
func (d *DatabaseWriter) updateReservationState(Id UUID, status string) error {
|
||||||
_, err := d.db.Exec("UPDATE reservations SET Status=? WHERE id=?", status, productId)
|
_, err := d.db.Exec("UPDATE reservations SET Status=? WHERE id=?", status, Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DatabaseWriter) AbortReservation(productId UUID) error {
|
func (d *DatabaseWriter) AbortReservation(Id UUID) error {
|
||||||
return d.updateReservationState(productId, "ABORTED")
|
return d.updateReservationState(Id, "ABORTED")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DatabaseWriter) ConfirmReservation(productId UUID) error {
|
func (d *DatabaseWriter) ConfirmReservation(Id UUID) error {
|
||||||
return d.updateReservationState(productId, "CONFIRMED")
|
return d.updateReservationState(Id, "CONFIRMED")
|
||||||
}
|
}
|
||||||
func (d *DatabaseWriter) ReleaseReservation(productId UUID) error {
|
|
||||||
return d.updateReservationState(productId, "RELEASED")
|
func (d *DatabaseWriter) ReleaseReservation(Id UUID) error {
|
||||||
|
return d.updateReservationState(Id, "RELEASED")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DatabaseWriter) ReserveReservation(deliveryId, warehouseId, amount int) error {
|
func (d *DatabaseWriter) ReserveReservation(deliveryId, warehouseId, amount int) error {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -14,7 +15,11 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Setup(dbr *DatabaseReader) error {
|
func Setup(dbr *DatabaseReader) error {
|
||||||
_, err := dbr.DatabaseWriter.db.Exec("drop table if exists elio_test.warehouseproducts")
|
_, err := dbr.DatabaseWriter.db.Exec("drop table if exists elio_test.reservations")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = dbr.DatabaseWriter.db.Exec("drop table if exists elio_test.warehouseproducts")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -30,6 +35,10 @@ func Setup(dbr *DatabaseReader) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = dbr.DatabaseWriter.createReservationTableIfNotExist()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
statements := strings.Split(CreateInserts, ";")
|
statements := strings.Split(CreateInserts, ";")
|
||||||
statementsDlvry := strings.Split(CreateDeliveryInserts, ";")
|
statementsDlvry := strings.Split(CreateDeliveryInserts, ";")
|
||||||
|
|
@ -93,17 +102,27 @@ func TestSelectStatement(t *testing.T) {
|
||||||
var dbr = NewDatabaseReader(connectionString)
|
var dbr = NewDatabaseReader(connectionString)
|
||||||
data, err := dbr.GetProductByProductIdDeliveryCountryAndState("A6053", "EU", "")
|
data, err := dbr.GetProductByProductIdDeliveryCountryAndState("A6053", "EU", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Es sollten keine Datensätze auffinbdar sein! %s", err)
|
t.Errorf("Es sollten Datensätze auffinbdar sein! %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(data) != 3 {
|
if len(data) != 3 {
|
||||||
t.Errorf("Es müssen drei Datensätze vorhanden sein. Stattdessen sind es %d", len(data))
|
t.Errorf("Es müssen drei Datensätze vorhanden sein. Stattdessen sind es %d", len(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
compareStr := "[{7507 DE 23 2} {500 EU 1 2} {8508 AT 4 4}]"
|
compareStr := "[{7507 DE 23 2 34} {500 EU 1 2 60} {8508 AT 4 4 64}]"
|
||||||
dbResultStr := fmt.Sprintln(data)
|
dbResultStr := fmt.Sprintln(data)
|
||||||
dbResultStr = strings.Trim(dbResultStr, "\n")
|
dbResultStr = strings.Trim(dbResultStr, "\n")
|
||||||
if strings.Compare(compareStr, dbResultStr) != 0 {
|
if strings.Compare(compareStr, dbResultStr) != 0 {
|
||||||
t.Errorf("Error: die Datensätze sind ungleich. \nSoll sein :\n%s\n ist aber: \n%s", compareStr, dbResultStr)
|
t.Errorf("Error: die Datensätze sind ungleich. \nSoll sein :\n%s\n ist aber: \n%s", compareStr, dbResultStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInsertStatement(t *testing.T) {
|
||||||
|
|
||||||
|
var dbr = NewDatabaseReader(connectionString)
|
||||||
|
data, err := dbr.GetProductByProductIdDeliveryCountryAndState("A6053", "EU", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Es sollten Datensätze auffinbdar sein! %s", err)
|
||||||
|
}
|
||||||
|
log.Println(data)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue