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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ racing/racing
*.out
.idea
.vscode
*/*.exe
*/*.exe
*/**/*_testdata
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Please treat the services provided as if they would live in a real-world environ
### Directory Structure

- `api`: A basic REST gateway, forwarding requests onto service(s).
- `racing`: A very bare-bones racing service.
- [`racing`](racing/README.md): A very bare-bones racing service.

```
entain/
Expand Down
2 changes: 1 addition & 1 deletion api/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module git.neds.sh/matty/entain/api
module github.com/ashleyjlive/entain/api

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"flag"
"net/http"

"github.com/ashleyjlive/entain/api/proto/racing"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"git.neds.sh/matty/entain/api/proto/racing"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
Expand Down
57 changes: 34 additions & 23 deletions api/proto/racing/racing.pb.go

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

2 changes: 2 additions & 0 deletions api/proto/racing/racing.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ message ListRacesResponse {
// Filter for listing races.
message ListRacesRequestFilter {
repeated int64 meeting_ids = 1;
// Returns Races that have `visible` set to the requested value.
optional bool visible = 2;
}

/* Resources */
Expand Down
30 changes: 30 additions & 0 deletions racing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Racing Service
The racing service provides the ability to access racing events over a gRPC server.

## Building
To build a executable simply call

$ go build

This will place a racing executable in the root directory.

## Command Line

You may provide optional command line arguments to the executable.
Currently, you may configure:
- `grpc-endpoint` - This is the endpoint that the front facing API server will speak to.
- `db_path` - This is the path of the database that the service will utilise.

For example:

$ ./racing --grpc-endpoint=localhost:8080 --db_path:/foo/bar/db.db

## API

Please [see](proto/README.md) the documentation for the protobuf definitions.

## Testing

To test individual packages:

$ go test ./...
70 changes: 68 additions & 2 deletions racing/db/db.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package db

import (
"database/sql"
"time"

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

func (r *racesRepo) seed() error {
func (r *racesRepo) init_tbl() error {
statement, err := r.db.Prepare(`CREATE TABLE IF NOT EXISTS races (id INTEGER PRIMARY KEY, meeting_id INTEGER, name TEXT, number INTEGER, visible INTEGER, advertised_start_time DATETIME)`)
if err == nil {
_, err = statement.Exec()
return err
} else {
return err
}
}

func (r *racesRepo) seed() error {
var (
err error
)
for i := 1; i <= 100; i++ {
statement, err = r.db.Prepare(`INSERT OR IGNORE INTO races(id, meeting_id, name, number, visible, advertised_start_time) VALUES (?,?,?,?,?,?)`)
statement, err := r.db.Prepare(`INSERT OR IGNORE INTO races(id, meeting_id, name, number, visible, advertised_start_time) VALUES (?,?,?,?,?,?)`)
if err == nil {
_, err = statement.Exec(
i,
Expand All @@ -25,6 +36,61 @@ func (r *racesRepo) seed() error {
)
}
}
return err
}

func (r *racesRepo) listAll() ([]*racing.Race, error) {
var races []*racing.Race
rows, err :=
r.db.Query(
"SELECT id, meeting_id, name, number, visible, advertised_start_time FROM races")
if err != nil {
return nil, err
}
for rows.Next() {
var race racing.Race
var advertisedStart time.Time

if err := rows.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

races = append(races, &race)
}
return races, nil
}

func (r *racesRepo) insert(race *racing.Race) error {
var statement *sql.Stmt
ts, err := ptypes.Timestamp(race.AdvertisedStartTime)
if err != nil {
return err
}
statement, err = r.db.Prepare(`INSERT INTO races(id, meeting_id, name, number, visible, advertised_start_time) VALUES (?,?,?,?,?,?)`)
if err == nil {
_, err = statement.Exec(
&race.Id,
&race.MeetingId,
&race.Name,
&race.Number,
&race.Visible,
ts,
)
}
return err
}

func (r *racesRepo) clear() error {
_, err := r.db.Exec("DELETE FROM races")
return err
}
45 changes: 37 additions & 8 deletions racing/db/races.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ package db

import (
"database/sql"
"github.com/golang/protobuf/ptypes"
_ "github.com/mattn/go-sqlite3"
"strings"
"sync"
"time"

"git.neds.sh/matty/entain/racing/proto/racing"
"github.com/golang/protobuf/ptypes"
_ "github.com/mattn/go-sqlite3"

"github.com/ashleyjlive/entain/racing/proto/racing"
)

// RacesRepo provides repository access to races.
type RacesRepo interface {
// Init will initialise our races repository.
Init() error

Init(bool) error
Clear() error
InsertRace(*racing.Race) error
// List will return a list of races.
List(filter *racing.ListRacesRequestFilter) ([]*racing.Race, error)
ListAll() ([]*racing.Race, error)
}

type racesRepo struct {
Expand All @@ -31,17 +34,32 @@ func NewRacesRepo(db *sql.DB) RacesRepo {
}

// Init prepares the race repository dummy data.
func (r *racesRepo) Init() error {
func (r *racesRepo) Init(seed bool) error {
var err error

r.init.Do(func() {
// For test/example purposes, we seed the DB with some dummy races.
err = r.seed()
if seed {
if err = r.seed(); err != nil {
err = r.init_tbl()
}
} else {
err = r.init_tbl()
}
})

return err
}

// Clears all data in the races repository.
func (r *racesRepo) Clear() error {
return r.clear()
}

// Allows insertions of a race into the repository.
func (r *racesRepo) InsertRace(race *racing.Race) error {
return r.insert(race)
}

func (r *racesRepo) List(filter *racing.ListRacesRequestFilter) ([]*racing.Race, error) {
var (
err error
Expand All @@ -61,6 +79,10 @@ func (r *racesRepo) List(filter *racing.ListRacesRequestFilter) ([]*racing.Race,
return r.scanRaces(rows)
}

func (r *racesRepo) ListAll() ([]*racing.Race, error) {
return r.listAll()
}

func (r *racesRepo) applyFilter(query string, filter *racing.ListRacesRequestFilter) (string, []interface{}) {
var (
clauses []string
Expand All @@ -71,6 +93,13 @@ func (r *racesRepo) applyFilter(query string, filter *racing.ListRacesRequestFil
return query, args
}

if filter.Visible != nil {
// Visibility is optional. If nil, then allow non filtering of
// visibility.
clauses = append(clauses, "visible = ?")
args = append(args, filter.Visible)
}

if len(filter.MeetingIds) > 0 {
clauses = append(clauses, "meeting_id IN ("+strings.Repeat("?,", len(filter.MeetingIds)-1)+"?)")

Expand Down
Loading