Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single Race Lookup #4

Open
wants to merge 11 commits into
base: feat-status_field
Choose a base branch
from
191 changes: 130 additions & 61 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,6 +11,9 @@ service Racing {
rpc ListRaces(ListRacesRequest) returns (ListRacesResponse) {
option (google.api.http) = { post: "/v1/list-races", body: "*" };
}
rpc GetRace(GetRaceRequest) returns (Race) {
option (google.api.http) = { get: "/v1/race/{id}" };
}
}

/* Requests/Responses */
Expand All @@ -22,6 +25,12 @@ message ListRacesRequest {
optional string order_by = 2;
}

// Request for GetRace call.
message GetRaceRequest {
// The unique id of the race.
int64 id = 1;
}

// Response to ListRaces call.
message ListRacesResponse {
repeated Race races = 1;
Expand Down
36 changes: 36 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"
getRace = "get"
)

func getRaceQueries() map[string]string {
Expand All @@ -16,5 +17,16 @@ func getRaceQueries() map[string]string {
advertised_start_time
FROM races
`,
getRace: `
SELECT
id,
meeting_id,
name,
number,
visible,
advertised_start_time
FROM races
WHERE id = ?
`,
}
}
27 changes: 27 additions & 0 deletions racing/db/races.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RacesRepo interface {
InsertRace(*racing.Race) error
// List will return a list of races.
List(request *racing.ListRacesRequest) ([]*racing.Race, error)
Get(request *racing.GetRaceRequest) (*racing.Race, error)
ListAll() ([]*racing.Race, error)
}

Expand Down Expand Up @@ -81,6 +82,32 @@ func (r *racesRepo) List(request *racing.ListRacesRequest) ([]*racing.Race, erro
return r.scanRaces(rows)
}

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

query = getRaceQueries()[getRace]

args = append(args, request.Id)
rows, err := r.db.Query(query, args...)
if err != nil {
return nil, err
}

races, err := r.scanRaces(rows)
if err != nil {
return nil, err
} else if len(races) == 0 {
// No race found, return error.
return nil, sql.ErrNoRows
} else {
// Race found, return single race.
return races[0], nil
}
}

func (r *racesRepo) ListAll() ([]*racing.Race, error) {
return r.listAll()
}
Expand Down
37 changes: 37 additions & 0 deletions racing/db/races_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,43 @@ func TestStatusField(t *testing.T) {
}
}

func TestGetRaceRequest(t *testing.T) {
racingDB, err := GetTestDB("races", "TestGetRaceRequest")
if err != nil {
t.Fatalf("Failed to open testdb %v", err)
}
racesRepo := db.NewRacesRepo(racingDB)
_ = racesRepo.Init(false)

tm1 := timestamppb.New(time.Now().AddDate(0, 0, 2))
race1 :=
racing.Race{Id: int64(1), MeetingId: int64(5),
Name: "Test1", Number: int64(5),
Visible: true, AdvertisedStartTime: tm1}
err = racesRepo.InsertRace(&race1)
if err != nil {
t.Fatalf("Failed to insert first race %v.", err)
}
tm2 := timestamppb.New(time.Now().AddDate(0, 0, -2))
race2 :=
racing.Race{Id: int64(5), MeetingId: int64(3),
Name: "Test2", Number: int64(9),
Visible: false, AdvertisedStartTime: tm2}
err = racesRepo.InsertRace(&race2)
if err != nil {
t.Fatalf("Failed to insert second race %v.", err)
}

rq := racing.GetRaceRequest{Id: race2.Id}
rsp, err := racesRepo.Get(&rq)
if err != nil {
t.Fatalf("Unable to retrieve individual race %v", rq.Id)
}
if rsp.Name != race2.Name {
t.Fatalf("Invalid race returned %v", rsp.Id)
}
}

// Helpers //

func GetRaces() []*racing.Race {
Expand Down
4 changes: 4 additions & 0 deletions racing/proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ This defines the protobuf message types for the racing API.
**RPCS**

- `ListRaces(ListRacesRequest) ListRacesResponse`
- `GetRace(GetRaceRequest) Race`

### ListRacesRequest
- Supports a filter parameter of type `ListRacesRequestFilter`.
- Supports a order_by parameter of the form defined by [Google API Design](https://cloud.google.com/apis/design/design_patterns#sorting_order).

### GetRaceRequest
- The ID of the race to be retrieved (int64).

### ListRacesRequestFilter
- A list of integer IDs can be supplied to perform a bulk lookup request. (optional)
- A category filter can be supplied which finds any races which match the category (optional, case-insensitive).
Expand Down
Loading