diff --git a/.gitignore b/.gitignore index e69de29..494b38b 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,2 @@ +paddockpass +test.db diff --git a/api/main.go b/api/main.go new file mode 100644 index 0000000..58c35cf --- /dev/null +++ b/api/main.go @@ -0,0 +1,71 @@ +package api + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "time" +) + +type Location struct { + Country string `json:"country"` + Latitude string `json:"lat"` + Locatlity string `json:"locality"` + Longitude string `json:"long"` +} + +type Circuit struct { + CircuitID string `json:"circuitID"` + URL string `json:"url"` + Name string `json:"circuitName"` + Location map[string]Location `json:"Location"` +} + +type Race struct { + Season string `json:"season"` + Round string `json:"round"` + RaceName string `json:"raceName"` + Circuit map[string]Circuit `json:"Circuit"` + Date time.Time `json:"date"` + Time time.Time `json:"time"` + ULR string `json:"url"` +} + +type Races struct { + Races map[string]Race `json:"Race"` + Season string `json:"season"` +} + +type RaceTable struct { + RaceTable map[string]Races `json:"Races"` +} + +type MRData struct { + MRData map[string]RaceTable `json:"RaceTable"` +} + +// GetDataFromErgast download the data from the API. +func GetDataFromErgast() { + resp, err := http.Get("http://ergast.com/api/f1/2019.json") + if err != nil { + panic(err.Error()) + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + panic(err.Error()) + } + defer resp.Body.Close() + + var jsonData map[string]MRData + // var jsonData map[string]RaceTable + err = json.Unmarshal([]byte(body), &jsonData) + if err != nil { + panic(err.Error()) + } + + for races := range jsonData { + fmt.Println(races) + } +} diff --git a/config/main.go b/config/main.go new file mode 100644 index 0000000..c39d1da --- /dev/null +++ b/config/main.go @@ -0,0 +1,5 @@ +package config + +const ( + apiURL = "" +) diff --git a/db/main.go b/db/main.go new file mode 100644 index 0000000..7ed9abb --- /dev/null +++ b/db/main.go @@ -0,0 +1,18 @@ +package db + +import ( + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + +// InitDB load the database and the models +func InitDB() { + db, err := gorm.Open("sqlite3", "test.db") + if err != nil { + panic(err.Error()) + } + defer db.Close() + + db.AutoMigrate(&User{}) + +} diff --git a/db/types.go b/db/types.go new file mode 100644 index 0000000..fb8721e --- /dev/null +++ b/db/types.go @@ -0,0 +1,46 @@ +package db + +import ( + "time" + + "github.com/jinzhu/gorm" +) + +// Race model describe race. +type Race struct { + gorm.Model + Season string + Round string + RaceName string + Circuit Circuit + Date time.Time +} + +// Circuit model describe a circuit. +type Circuit struct { + gorm.Model + Name string + Location string +} + +// Races contains the list of all races for a season. +type Races struct { + gorm.Model + Season string + Races []Race +} + +// GORM DOC + +// // Create +// db.Create(&Product{Code: "L1212", Price: 1000}) + +// // Read +// var product Product +// db.First(&product, 1) // find product with id 1 +// db.First(&product, "code = ?", "L1212") // find product with code l1212 + +// // Update - update product's price to 2000 +// db.Model(&product).Update("Price", 2000) + +// Delete - delete p diff --git a/db/user.go b/db/user.go new file mode 100644 index 0000000..87d4f77 --- /dev/null +++ b/db/user.go @@ -0,0 +1,11 @@ +package db + +import ( + "github.com/jinzhu/gorm" +) + +// User describe the user model +type User struct { + gorm.Model + Username string +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..037c20f --- /dev/null +++ b/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + + "framagit.org/pksl/paddockpass/api" + "framagit.org/pksl/paddockpass/db" + _ "github.com/go-sql-driver/mysql" +) + +func main() { + fmt.Println("Hello there!") + db.InitDB() + api.GetDataFromErgast() +} diff --git a/paddockpass b/paddockpass deleted file mode 100755 index 5475b4a..0000000 Binary files a/paddockpass and /dev/null differ diff --git a/test.db b/test.db deleted file mode 100644 index 2621425..0000000 Binary files a/test.db and /dev/null differ