second commit

This commit is contained in:
christalib 2019-09-03 23:05:07 +02:00
parent 50062d668f
commit 1b90da8304
9 changed files with 168 additions and 0 deletions

2
.gitignore vendored
View file

@ -0,0 +1,2 @@
paddockpass
test.db

71
api/main.go Normal file
View file

@ -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)
}
}

5
config/main.go Normal file
View file

@ -0,0 +1,5 @@
package config
const (
apiURL = ""
)

18
db/main.go Normal file
View file

@ -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{})
}

46
db/types.go Normal file
View file

@ -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

11
db/user.go Normal file
View file

@ -0,0 +1,11 @@
package db
import (
"github.com/jinzhu/gorm"
)
// User describe the user model
type User struct {
gorm.Model
Username string
}

15
main.go Normal file
View file

@ -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()
}

Binary file not shown.

BIN
test.db

Binary file not shown.