Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
219 changes: 144 additions & 75 deletions api/proto/racing/racing.pb.go

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions api/proto/racing/racing.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions api/proto/racing/racing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ service Racing {
rpc ListRaces(ListRacesRequest) returns (ListRacesResponse) {
option (google.api.http) = { post: "/v1/list-races", body: "*" };
}

// GetRace will return a specific Race by its ID.
rpc GetRace(GetRaceRequest) returns (Race) {
option (google.api.http) = { get: "/v1/races/{id}" };
}
}

/* Requests/Responses */

message GetRaceRequest {
int64 id = 1;
}

// Request for ListRaces call.
message ListRacesRequest {
ListRacesRequestFilter filter = 1;
Expand Down
38 changes: 38 additions & 0 deletions api/proto/racing/racing_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions racing/db/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package db

const (
racesList = "list"
racesGet = "get"
)

func getRaceQueries() map[string]string {
Expand All @@ -16,5 +17,16 @@ func getRaceQueries() map[string]string {
advertised_start_time
FROM races
`,
racesGet: `
SELECT
id,
meeting_id,
name,
number,
visible,
advertised_start_time
FROM races
WHERE id = ?
`,
}
}
52 changes: 52 additions & 0 deletions racing/db/races.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
)

var advertised_start_time = "advertised_start_time"

// Add more in the future as we want to order by other fields
var validOrderByFields = []string{advertised_start_time}

// RacesRepo provides repository access to races.
Expand All @@ -24,6 +26,9 @@ type RacesRepo interface {

// List will return a list of races.
List(filter *racing.ListRacesRequestFilter, orderBy string) ([]*racing.Race, error)

// Get will return a specific Race by its ID.
Get(id int64) (*racing.Race, error)
}

type racesRepo struct {
Expand Down Expand Up @@ -80,6 +85,30 @@ func (r *racesRepo) List(filter *racing.ListRacesRequestFilter, orderBy string)
return races, nil
}

func (r *racesRepo) Get(id int64) (*racing.Race, error) {
var (
query string
args []interface{}
)

query = getRaceQueries()[racesGet]

args = append(args, id)

row := r.db.QueryRow(query, args...)

race, err := r.scanRace(row)

if err != nil {
return nil, err
}

r.addStatusToRace(race)

return race, nil

}

func (r *racesRepo) applyFilter(query string, filter *racing.ListRacesRequestFilter) (string, []interface{}) {
var (
clauses []string
Expand Down Expand Up @@ -204,3 +233,26 @@ func (m *racesRepo) scanRaces(

return races, nil
}

func (m *racesRepo) scanRace(
row *sql.Row,
) (*racing.Race, error) {
var race racing.Race
var advertisedStart time.Time

if err := row.Scan(&race.Id, &race.MeetingId, &race.Name, &race.Number, &race.Visible, &advertisedStart); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}

ts, err := ptypes.TimestampProto(advertisedStart)
if err != nil {
return nil, err
}

race.AdvertisedStartTime = ts

return &race, nil
}
9 changes: 9 additions & 0 deletions racing/errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package errors

const ErrInvalidRaceID = RacingErr("invalid Race ID")

type RacingErr string

func (e RacingErr) Error() string {
return string(e)
}
Loading