simple parcel service finder
This commit is contained in:
98
main.go
98
main.go
@@ -14,89 +14,25 @@ In this project i play around with go. This is my first time writing go, and im
|
||||
import (
|
||||
"database/sql"
|
||||
"log"
|
||||
"errors"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func executeQuery(db *sql.DB, query string) {
|
||||
_, err := db.Exec(query)
|
||||
if err != nil {
|
||||
log.Printf("%q: %s\n", err, query)
|
||||
return
|
||||
|
||||
func findParcelProvider(code string, postal_code string)(string, error){
|
||||
providers := [4]string{"express", "parcel-nl", "ecommerce", "ecommerce-europe"}
|
||||
for _, v := range providers {
|
||||
// do http api reqiest
|
||||
success := false
|
||||
|
||||
if (success){
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
|
||||
func createTables(db *sql.DB) {
|
||||
executeQuery(db, `
|
||||
CREATE TABLE shipments (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
tracking_number TEXT NOT NULL UNIQUE,
|
||||
service TEXT NOT NULL,
|
||||
sender_name TEXT,
|
||||
receiver_name TEXT,
|
||||
origin_country TEXT,
|
||||
origin_city TEXT,
|
||||
origin_postal TEXT,
|
||||
origin_address TEXT,
|
||||
destination_country TEXT,
|
||||
destination_city TEXT,
|
||||
destination_postal TEXT,
|
||||
destination_address TEXT,
|
||||
pickup_date TEXT,
|
||||
estimated_delivery_from TEXT,
|
||||
estimated_delivery_to TEXT,
|
||||
current_status TEXT,
|
||||
last_update TEXT DEFAULT (datetime('now')),
|
||||
created_at TEXT DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
-- Status Events: history of status updates
|
||||
CREATE TABLE shipment_events (
|
||||
id INTEGER PRIMARY KEY,
|
||||
shipment_id INTEGER NOT NULL,
|
||||
event_time TEXT NOT NULL,
|
||||
status_code TEXT NOT NULL,
|
||||
status_desc TEXT,
|
||||
description TEXT,
|
||||
remark TEXT,
|
||||
next_steps TEXT,
|
||||
location_country TEXT,
|
||||
location_city TEXT,
|
||||
location_postal TEXT,
|
||||
FOREIGN KEY (shipment_id) REFERENCES shipments(id)
|
||||
);
|
||||
CREATE INDEX idx_events_shipment_time ON shipment_events(shipment_id, event_time);
|
||||
|
||||
CREATE TABLE shipment_updates (
|
||||
id INTEGER PRIMARY KEY,
|
||||
shipment_id INTEGER NOT NULL,
|
||||
update_time TEXT DEFAULT (datetime('now')),
|
||||
service TEXT,
|
||||
location_country TEXT,
|
||||
location_city TEXT,
|
||||
status_code TEXT,
|
||||
status_desc TEXT,
|
||||
estimated_from TEXT,
|
||||
estimated_to TEXT,
|
||||
FOREIGN KEY (shipment_id) REFERENCES shipments(id)
|
||||
);
|
||||
CREATE INDEX idx_updates_shipment_time ON shipment_updates(shipment_id, update_time);
|
||||
`)
|
||||
|
||||
log.Printf("[Database] tables created")
|
||||
|
||||
return "", errors.New("parcel not found!")
|
||||
}
|
||||
|
||||
func dropTables(db *sql.DB) {
|
||||
executeQuery(db, `
|
||||
DROP TABLE IF EXISTS shipments;
|
||||
DROP TABLE IF EXISTS shipment_events;
|
||||
DROP TABLE IF EXISTS shipment_updates;
|
||||
`)
|
||||
|
||||
log.Printf("[Database] tables dropped")
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
db, err := sql.Open("sqlite", "./dhl.db")
|
||||
@@ -105,6 +41,14 @@ func main() {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
dropTables(db) // temporarely for debugging
|
||||
createTables(db)
|
||||
code := "blablbla"
|
||||
postal := "1234ab"
|
||||
provider, err := findParcelProvider(code, postal)
|
||||
if (err != nil){
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
log.Printf("Code: %s, Postal Code: %s, Tracking Service: %s", code, postal, provider)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user