Fixed Route Bug with $ Char
This commit is contained in:
parent
dd8d914b33
commit
18e2d9a359
@ -31,7 +31,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
//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) {
|
||||
fmt.Println("In Arbeit: reserve")
|
||||
})
|
||||
|
||||
@ -7,7 +7,6 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
@ -16,7 +15,6 @@ import (
|
||||
)
|
||||
|
||||
type DatabaseWriter struct {
|
||||
mu sync.Mutex
|
||||
log logger.Logger
|
||||
db *sql.DB
|
||||
}
|
||||
@ -93,11 +91,11 @@ func (d *DatabaseWriter) createReservationTableIfNotExist() error {
|
||||
CONSTRAINT fk_delivery_id
|
||||
FOREIGN KEY (deliveryId) REFERENCES deliverytimes (id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE,
|
||||
ON UPDATE RESTRICT,
|
||||
CONSTRAINT fk_warehouse_id
|
||||
FOREIGN KEY (warehouseId) REFERENCES warehouseproducts (id)
|
||||
ON DELETE CASCADE
|
||||
ON UPDATE CASCADE
|
||||
ON UPDATE RESTRICT
|
||||
) ENGINE=InnoDB;`)
|
||||
return err
|
||||
}
|
||||
@ -159,17 +157,18 @@ func (d *DatabaseWriter) UpdateOrInsertDelivery(fromcountry string, tocountry st
|
||||
}
|
||||
|
||||
type ProductDelivery struct {
|
||||
Id int
|
||||
WhId int
|
||||
Warehouse string
|
||||
Amount int
|
||||
DeliveryDays int
|
||||
DeliveryId int
|
||||
}
|
||||
|
||||
func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, delivery_country, delivery_country_state string) ([]ProductDelivery, error) {
|
||||
stmt := `
|
||||
SELECT whp.id, warehouse, amount, d.delivery
|
||||
SELECT whp.id, warehouse, amount, d.delivery, d.id deliveryId
|
||||
FROM warehouseproducts whp
|
||||
left join deliverytimes d
|
||||
join deliverytimes d
|
||||
on whp.warehouse = d.fromcountry
|
||||
where productid = ?
|
||||
and d.tocountry = ?
|
||||
@ -182,17 +181,14 @@ func (d *DatabaseReader) GetProductByProductIdDeliveryCountryAndState(prod_id, d
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var products []ProductDelivery
|
||||
|
||||
for rows.Next() {
|
||||
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
|
||||
}
|
||||
products = append(products, pd)
|
||||
}
|
||||
|
||||
return products, nil
|
||||
}
|
||||
|
||||
@ -214,23 +210,24 @@ func (d *DatabaseReader) GetReservationStateById(productId UUID) (string, error)
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (d *DatabaseWriter) updateReservationState(productId UUID, status string) error {
|
||||
_, err := d.db.Exec("UPDATE reservations SET Status=? WHERE id=?", status, productId)
|
||||
func (d *DatabaseWriter) updateReservationState(Id UUID, status string) error {
|
||||
_, err := d.db.Exec("UPDATE reservations SET Status=? WHERE id=?", status, Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DatabaseWriter) AbortReservation(productId UUID) error {
|
||||
return d.updateReservationState(productId, "ABORTED")
|
||||
func (d *DatabaseWriter) AbortReservation(Id UUID) error {
|
||||
return d.updateReservationState(Id, "ABORTED")
|
||||
}
|
||||
|
||||
func (d *DatabaseWriter) ConfirmReservation(productId UUID) error {
|
||||
return d.updateReservationState(productId, "CONFIRMED")
|
||||
func (d *DatabaseWriter) ConfirmReservation(Id UUID) error {
|
||||
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 {
|
||||
|
||||
@ -2,6 +2,7 @@ package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"strings"
|
||||
"testing"
|
||||
@ -14,7 +15,11 @@ const (
|
||||
)
|
||||
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@ -30,6 +35,10 @@ func Setup(dbr *DatabaseReader) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = dbr.DatabaseWriter.createReservationTableIfNotExist()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
statements := strings.Split(CreateInserts, ";")
|
||||
statementsDlvry := strings.Split(CreateDeliveryInserts, ";")
|
||||
@ -93,17 +102,27 @@ func TestSelectStatement(t *testing.T) {
|
||||
var dbr = NewDatabaseReader(connectionString)
|
||||
data, err := dbr.GetProductByProductIdDeliveryCountryAndState("A6053", "EU", "")
|
||||
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 {
|
||||
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 = strings.Trim(dbResultStr, "\n")
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
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…
x
Reference in New Issue
Block a user