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
12 changes: 11 additions & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ashleyjlive/entain/api

go 1.16
go 1.17

require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.5.0
Expand All @@ -13,3 +13,13 @@ require (
gopkg.in/yaml.v2 v2.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

require (
github.com/ghodss/yaml v1.0.0 // indirect
github.com/golang/glog v0.0.0-20210429001901-424d2337a529 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/magefile/mage v1.10.0 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
)
113 changes: 88 additions & 25 deletions api/proto/racing/racing.pb.go

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

7 changes: 7 additions & 0 deletions api/proto/racing/racing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ message Race {
bool visible = 5;
// AdvertisedStartTime is the time the race is advertised to run.
google.protobuf.Timestamp advertised_start_time = 6;
enum Status {
CLOSED = 0;
OPEN = 1;
}
// Status reflects whether the Race is open or closed, based off the
// advertised start time being in the past.
Status status = 7;
}
6 changes: 2 additions & 4 deletions racing/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/ashleyjlive/entain/racing/proto/racing"
"github.com/golang/protobuf/ptypes"
"google.golang.org/protobuf/types/known/timestamppb"
"syreclabs.com/go/faker"
)

Expand Down Expand Up @@ -58,10 +59,7 @@ func (r *racesRepo) listAll() ([]*racing.Race, error) {

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

race.AdvertisedStartTime = ts

Expand Down
21 changes: 13 additions & 8 deletions racing/db/races.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"time"
"unicode"

"github.com/golang/protobuf/ptypes"
_ "github.com/mattn/go-sqlite3"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/ashleyjlive/entain/racing/proto/racing"
)
Expand Down Expand Up @@ -154,16 +154,16 @@ func toOrderBySql(input string) (*string, error) {
words := strings.Fields(str)
wordCount := len(words)
if wordCount > 2 || wordCount < 1 {
return nil, errors.New("Invalid order by term count.")
return nil, errors.New("invalid order by term count")
}
sortField := words[0]
if strings.IndexFunc(sortField, isUnsafeColumnChar) != -1 {
return nil, errors.New("Invalid column name.")
return nil, errors.New("invalid column name")
}
if wordCount == 2 {
sort := words[1]
if !(strings.EqualFold(sort, "asc") || strings.EqualFold(sort, "desc")) {
return nil, errors.New("Invalid order by dir parameter.")
return nil, errors.New("invalid order by dir parameter")
}
sortField += " " + sort
}
Expand Down Expand Up @@ -203,11 +203,16 @@ func (m *racesRepo) scanRaces(
return nil, err
}

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

if advertisedStart.Before(time.Now()) {
// All races that have an `advertised_start_time` in the past should
// reflect `CLOSED`
// Note that this depends on the system having the correct time.
race.Status = racing.Race_CLOSED
} else {
race.Status = racing.Race_OPEN
}
race.AdvertisedStartTime = ts

races = append(races, &race)
Expand Down
53 changes: 49 additions & 4 deletions racing/db/races_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (

"github.com/ashleyjlive/entain/racing/db"
"github.com/ashleyjlive/entain/racing/proto/racing"
"github.com/golang/protobuf/ptypes"
"syreclabs.com/go/faker"

"google.golang.org/protobuf/types/known/timestamppb"
)

func TestRepo(t *testing.T) {
Expand Down Expand Up @@ -183,7 +184,7 @@ func TestOrderBy(t *testing.T) {
racesRepo := db.NewRacesRepo(racingDB)
_ = racesRepo.Init(false)

tm1, _ := ptypes.TimestampProto(time.Now().AddDate(0, 0, 2))
tm1 := timestamppb.New(time.Now().AddDate(0, 0, 2))
race1 :=
racing.Race{Id: int64(1), MeetingId: int64(5),
Name: "Test1", Number: int64(5),
Expand All @@ -192,7 +193,7 @@ func TestOrderBy(t *testing.T) {
if err != nil {
t.Fatalf("Failed to insert first race %v.", err)
}
tm2, _ := ptypes.TimestampProto(time.Now().AddDate(0, 0, 2))
tm2 := timestamppb.New(time.Now().AddDate(0, 0, -2))
race2 :=
racing.Race{Id: int64(5), MeetingId: int64(3),
Name: "Test2", Number: int64(9),
Expand Down Expand Up @@ -259,6 +260,50 @@ func TestOrderBy(t *testing.T) {
}
}

func TestStatusField(t *testing.T) {
racingDB, err := GetTestDB("races", "TestStatusField")
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)
}

// Test case
rq := racing.ListRacesRequest{}
rsp, err := racesRepo.List(&rq)
if err != nil {
t.Fatalf("Unable to retrieve races list.")
}
if len(rsp) != 2 {
t.Fatalf("Returned incorrect amount of races.")
}
if rsp[0].Status != racing.Race_CLOSED {
t.Fatalf("Returned invalid status flag for closed event %v.", rsp[0].Id)
}
if rsp[1].Status != racing.Race_OPEN {
t.Fatalf("Returned invalid status flag for open event %v.", rsp[1].Id)
}
}

// Helpers //

func GetRaces() []*racing.Race {
Expand All @@ -270,7 +315,7 @@ func GetRaces() []*racing.Race {
name := faker.Team().Name()
number, _ := strconv.Atoi(faker.Number().Between(1, 12))
visible := randBool()
tm, _ := ptypes.TimestampProto(
tm := timestamppb.New(
faker.Time().Between(
time.Now().AddDate(0, 0, -1), time.Now().AddDate(0, 0, 2)))
race :=
Expand Down
10 changes: 9 additions & 1 deletion racing/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ashleyjlive/entain/racing

go 1.16
go 1.17

require (
github.com/golang/protobuf v1.5.2
Expand All @@ -17,3 +17,11 @@ require (
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
syreclabs.com/go/faker v1.2.3
)

require (
github.com/ghodss/yaml v1.0.0 // indirect
github.com/golang/glog v0.0.0-20210429001901-424d2337a529 // indirect
github.com/magefile/mage v1.10.0 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
)
3 changes: 2 additions & 1 deletion racing/proto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ This defines the protobuf message types for the racing API.
- The unique identifier for the races meeting (int64).
- The name of the race (string).
- The visibility of the race (bool).
- The advertised start time of the race (Timestamp).
- The advertised start time of the race (Timestamp).
- A status flag indicating if the event is `OPEN` or `CLOSED` - this is based off the advertised start time with the systems current time.
Loading