paddockpass/api/race.go

141 lines
3 KiB
Go
Raw Normal View History

2019-09-03 23:05:07 +02:00
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
2019-09-06 13:39:35 +02:00
"strings"
2019-09-03 23:05:07 +02:00
"time"
"framagit.org/pksl/paddockpass/db"
"github.com/jinzhu/gorm"
2019-09-03 23:05:07 +02:00
)
2019-09-06 13:39:35 +02:00
// Location represents the details of a circuit.
2019-09-03 23:05:07 +02:00
type Location struct {
Country string `json:"country"`
Latitude string `json:"lat"`
Locatlity string `json:"locality"`
Longitude string `json:"long"`
}
2019-09-06 13:39:35 +02:00
// Circuit represents the details of a circuit.
2019-09-03 23:05:07 +02:00
type Circuit struct {
2019-09-06 13:39:35 +02:00
CircuitID string `json:"circuitID"`
URL string `json:"url"`
Name string `json:"circuitName"`
Location Location `json:"Location"`
2019-09-03 23:05:07 +02:00
}
2019-09-06 13:39:35 +02:00
// Race represents the details of a race.
2019-09-03 23:05:07 +02:00
type Race struct {
2019-09-06 13:39:35 +02:00
Season string `json:"season"`
Round string `json:"round"`
RaceName string `json:"raceName"`
Circuit Circuit `json:"Circuit"`
Date Date `json:"date"`
Time string `json:"time"`
URL string `json:"url"`
2019-09-03 23:05:07 +02:00
}
2019-09-06 13:39:35 +02:00
// Races is the list of races.
2019-09-03 23:05:07 +02:00
type Races struct {
2019-09-06 13:39:35 +02:00
Races []Race `json:"Races"`
Season string `json:"season"`
2019-09-03 23:05:07 +02:00
}
2019-09-06 13:39:35 +02:00
// RaceTable is the list of races.
2019-09-03 23:05:07 +02:00
type RaceTable struct {
2019-09-06 13:39:35 +02:00
RaceTable Races `json:"RaceTable"`
2019-09-03 23:05:07 +02:00
}
2019-09-06 13:39:35 +02:00
// MRData is the general wrapper from Eargast API.
2019-09-03 23:05:07 +02:00
type MRData struct {
2019-09-06 13:39:35 +02:00
MRData RaceTable `json:"MRData"`
}
// Date is the date struct.
type Date struct{ time.Time }
// UnmarshalJSON is something.
func (d *Date) UnmarshalJSON(data []byte) error {
t, err := time.Parse("2006-01-02", strings.Trim(string(data), "\""))
if err != nil {
return err
}
d.Time = t
return nil
}
func (d Date) String() string {
return d.Format(time.RFC3339)
2019-09-03 23:05:07 +02:00
}
// GetSeasonFromErgast download the data from the API.
func GetSeasonFromErgast(year string) {
resp, err := http.Get("http://ergast.com/api/f1/" + year + ".json")
2019-09-03 23:05:07 +02:00
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
2019-09-06 13:39:35 +02:00
var jsonData MRData
err = json.Unmarshal(body, &jsonData)
2019-09-03 23:05:07 +02:00
if err != nil {
panic(err.Error())
}
races := jsonData.MRData.RaceTable.Races
tx, err := gorm.Open("sqlite3", "test.db")
if err != nil {
fmt.Println(err)
}
for _, race := range races {
circuit := db.Circuit{
CircuitID: race.Circuit.CircuitID,
Name: race.Circuit.Name,
URL: race.Circuit.URL,
}
race := db.Race{
Circuit: circuit,
RaceName: race.RaceName,
Round: race.Round,
Season: race.Season,
URL: race.URL,
}
tx.Assign(circuit).FirstOrCreate(&circuit)
tx.Assign(race).FirstOrCreate(&race)
}
defer tx.Close()
2019-09-03 23:05:07 +02:00
}
// // CheckIfEntryExists returns a boolean if the entry is in the DB.
// func CheckIfEntryExists(model interface{}) (bool, error) {
// db, err := gorm.Open("sqlite3", "test.db")
// if err != nil {
// panic(err.Error())
// }
// defer db.Close()
// var user User
// if err := db.Where("name = ?", "xxxx").First(&user).Error; err != nil {
// // error handling...
// if gorm.IsRecordNotFoundError(err) {
// db.Create(&newUser) // newUser not user
// } else {
// db.Model(&user).Where("id = ?", 3333).Update("name", "nick")
// }
// }
// }