Commit 17302ec1 authored by dagal's avatar dagal
Browse files

Lines API

Terribly inefficient but somehow still good enough
parent f61b5030
......@@ -61,6 +61,31 @@ func GetStop(c *gin.Context) {
}
}
func GetAllLines(c *gin.Context) {
fc := geojson.NewFeatureCollection()
for _, l := range lines {
if f := l.ToGeoJSON(); f != nil {
fc.AddFeature(f)
}
}
returnGeoJSONFeatureCollection(c, fc)
}
func GetLine(c *gin.Context) {
id := c.Param("id")
s := lines[id].ToGeoJSON()
if s == nil {
c.JSON(404, gin.H{
"message": "Line not found",
})
} else {
returnGeoJSONFeature(c, s)
}
}
func ShowIndex(c *gin.Context) {
c.HTML(200, "index.html", gin.H{})
}
......
......@@ -14,6 +14,16 @@ func ReadStops(db *gorm.DB) {
}
}
func ReadLines(db *gorm.DB) {
lineSlice := []Line{}
db.Find(&lineSlice)
for _, l := range lineSlice {
db.Model(l).Order("seq").Association("Stops").Find(&l.Stops)
lines[l.ID] = l
}
}
func ReadTime(db *gorm.DB) {
// this toy ORM can't return min(date) without breaking...
var journey Journey
......
......@@ -9,6 +9,7 @@ import (
)
var stops = map[uint32]Stop{}
var lines = map[string]Line{}
var start_time = time.Now()
var end_time = time.Now()
......@@ -19,9 +20,11 @@ func main() {
db := connectDB()
defer db.Close()
// db.LogMode(true)
db.SingularTable(true)
db.AutoMigrate(&Stop{}, &Journey{})
db.AutoMigrate(&Stop{}, &Line{}, &Journey{})
ReadStops(db)
ReadLines(db)
ReadTime(db)
CTRLoad()
defer CTRFree()
......@@ -35,6 +38,8 @@ func main() {
router.GET("/time", TimeRange)
router.GET("/stops", GetAllStops)
router.GET("/stops/:id", GetStop)
router.GET("/lines", GetAllLines)
router.GET("/lines/:id", GetLine)
router.GET("/index", ShowIndex)
router.GET("/", ShowIndex)
router.GET("/stop_stats/:id", GetStopStats)
......
......@@ -13,6 +13,12 @@ type Stop struct {
Lon float64
}
type Line struct {
ID string
Name string
Stops []Stop `gorm:"many2many:line_stop;association_foreignkey:ctr_id;jointable_foreignkey:line_id;association_jointable_foreignkey:stop_ctr_id;"`
}
type Journey struct {
//gorm.Model
LineId string `gorm:"primary_key"`
......@@ -30,3 +36,19 @@ func (s Stop) ToGeoJSON() *geojson.Feature {
f.Properties["name"] = s.Name
return f
}
func (l Line) ToGeoJSON() *geojson.Feature {
coords := make([][]float64, len(l.Stops))
stopIds := make([]uint32, len(l.Stops))
for i, s := range l.Stops {
coords[i] = []float64{s.Lon, s.Lat}
stopIds[i] = s.ID
}
f := geojson.NewLineStringFeature(coords)
f.Properties["id"] = l.ID
f.Properties["name"] = l.Name
f.Properties["stops"] = stopIds
return f
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment