-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
54 lines (48 loc) · 1.28 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"database/sql"
"log"
"time"
"github.com/phedoreanu/journeys/model"
)
// FindByDriverID returns a array of journeys available
// for the given driverID
func FindByDriverID(driverID string, db *sql.DB) []*model.Journey {
return queryJourneys(db, `SELECT
j.*
FROM
journeys as j,
drivers as d
WHERE
d.id = ?
AND j.departure_time >= d.available_from
AND j.arrival_time <= d.available_till`, driverID)
}
// FindByLocationAndTime returns a array of journeys
// from the given location and overlap with the given time frame
func FindByLocationAndTime(location string, start, end time.Time, db *sql.DB) []*model.Journey {
return queryJourneys(db, `SELECT
*
FROM
journeys
WHERE
departure_location = ?
AND departure_time >= ?
AND arrival_time <= ?`, location, start, end)
}
func queryJourneys(db *sql.DB, query string, args ...interface{}) (journeys []*model.Journey) {
rows, err := db.Query(query, args...)
if err != nil {
log.Println(err)
}
defer rows.Close()
journeys = make([]*model.Journey, 0)
for rows.Next() {
j := new(model.Journey)
if err := rows.Scan(&j.ID, &j.DepartureTime, &j.ArrivalTime, &j.DepartureLocation, &j.ArrivalLocation); err != nil {
log.Println(err)
}
journeys = append(journeys, j)
}
return
}