aboutsummaryrefslogtreecommitdiff
path: root/cmd/airlines
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/airlines')
-rw-r--r--cmd/airlines/main.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/cmd/airlines/main.go b/cmd/airlines/main.go
new file mode 100644
index 0000000..6bf097a
--- /dev/null
+++ b/cmd/airlines/main.go
@@ -0,0 +1,62 @@
+package main
+
+import (
+ "fmt"
+ "os"
+ "time"
+
+ "github.com/joho/godotenv"
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+
+ "airlines/pkg/model"
+)
+
+func main() {
+ // run with MAKE only
+ err := godotenv.Load("../../.env")
+ if err != nil {
+ fmt.Println(err)
+ }
+
+ dsn := fmt.Sprintf("postgres://%s:%s@%s:%s/%s", os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"), os.Getenv("DB_HOST"), os.Getenv("DB_PORT"), os.Getenv("DB_NAME"))
+ db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
+
+ if err != nil {
+ fmt.Println(err)
+ return
+ }
+ // db.Migrator().DropTable(&model.User{}, &model.Card{}, &model.Flight{}) // ;-)
+ if err := db.AutoMigrate(&model.User{}, &model.Card{}, &model.Flight{}); err != nil {
+ return
+ }
+
+ jinzhuFlights := []model.Flight{
+ {
+ Number: "SU1111",
+ From: "North pole",
+ To: "Mars",
+ Departure: time.Now(),
+ Arrival: time.Now().AddDate(0, 0, 162),
+ },
+ }
+ user := model.User{
+ Name: "Jinzhu",
+ Age: 18,
+ Birthday: time.Now(),
+ Flights: jinzhuFlights,
+ Cards: []model.Card{
+ {
+ Prefix: "AA",
+ Number: 123,
+ Bonusprogramm: "AbObA airlines SuPrEmE",
+ Flights: jinzhuFlights,
+ },
+ },
+ }
+
+ // Create a single record
+ result := db.Create(&user)
+ fmt.Println(result.Error)
+
+}