Skip to content

Commit

Permalink
Merge pull request pathwar#164 from pathwar/dev/moul/tournament-list-…
Browse files Browse the repository at this point in the history
…teams

WIP feat: GET /tournament/teams API endoint
  • Loading branch information
moul authored Oct 6, 2019
2 parents 07ba023 + 2cf52a0 commit ede07a3
Show file tree
Hide file tree
Showing 16 changed files with 975 additions and 320 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ vendor/* linguist-vendored
*.gen.graphql linguist-generated
*.pb.go linguist-generated
*.swagger.json linguist-generated
swagger.yaml linguist-generated
go.sum linguist-generated
gen.sum linguist-generated

# Reduce conflicts on markdown files
*.md merge=union
2 changes: 2 additions & 0 deletions api/pwdb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ message TournamentTeam {
repeated TournamentMember members = 206 [(gogoproto.moretags) = "gorm:\"PRELOAD:false\""];
}

message TournamentTeamList { repeated TournamentTeam items = 1; }

message WhoswhoAttempt {
string id = 1 [(gogoproto.moretags) = "gorm:\"primary_key\"", (gogoproto.customname) = "ID"];
google.protobuf.Timestamp created_at = 2 [(gogoproto.stdtime) = true, (gogoproto.nullable) = true];
Expand Down
5 changes: 5 additions & 0 deletions api/pwengine.proto
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ service Engine {
rpc SetPreferences(SetPreferencesInput) returns (Void) { option (google.api.http) = {post: "/preferences"}; };
rpc ListLevels(Void) returns (pathwar.db.LevelList) { option (google.api.http) = {get: "/levels"}; };
rpc ListTeams(Void) returns (pathwar.db.TeamList) { option (google.api.http) = {get: "/teams"}; };
rpc ListTournamentTeams(ListTournamentTeamsInput) returns (pathwar.db.TournamentTeamList) { option (google.api.http) = {get: "/tournament/teams"}; };
rpc GetStatus(Void) returns (Status) { option (google.api.http) = {get: "/status"}; }

//
Expand All @@ -111,6 +112,10 @@ message Void {
};
}

message ListTournamentTeamsInput {
string tournament_id = 1 [(gogoproto.customname) = "TournamentID"];
}

message SetPreferencesInput {
string active_tournament_id = 1 [(gogoproto.customname) = "ActiveTournamentID"];
// bool dark_mode
Expand Down
4 changes: 2 additions & 2 deletions docs/gen.sum

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

4 changes: 2 additions & 2 deletions go/gen.sum

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

1 change: 0 additions & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require (
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-chi/chi v4.0.2+incompatible
github.com/go-errors/errors v1.0.1
github.com/go-sql-driver/mysql v1.4.1
github.com/gobuffalo/packr/v2 v2.6.0
github.com/gogo/gateway v1.1.0
Expand Down
2 changes: 0 additions & 2 deletions go/go.sum

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

651 changes: 418 additions & 233 deletions go/pkg/pwdb/pwdb.pb.go

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions go/pkg/pwengine/api_pub_listtournamentteams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pwengine

import (
"context"
"fmt"

"pathwar.land/go/pkg/pwdb"
)

func (e *engine) ListTournamentTeams(ctx context.Context, in *ListTournamentTeamsInput) (*pwdb.TournamentTeamList, error) {
{ // validation
if in.TournamentID == "" {
return nil, ErrMissingArgument
}

var c int
err := e.db.
Table("tournament").
Select("id").
Where("id = ?", in.TournamentID).
Count(&c).
Error
if err != nil {
return nil, fmt.Errorf("failed to fetch tournament: %w", err)
}
if c == 0 {
return nil, ErrInvalidArgument // invalid in.TournamentID
}
}

var ret pwdb.TournamentTeamList
err := e.db.
Set("gorm:auto_preload", true).
Where(pwdb.TournamentTeam{TournamentID: in.TournamentID}).
Find(&ret.Items).
Error
if err != nil {
return nil, fmt.Errorf("failed to fetch tournament teams from db: %w", err)
}

return &ret, nil
}
73 changes: 73 additions & 0 deletions go/pkg/pwengine/api_pub_listtournamentteams_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package pwengine

import (
"context"
"errors"
"testing"
)

func TestEngine_ListTournamentTeams(t *testing.T) {
engine, cleanup := TestingEngine(t, Opts{})
defer cleanup()
ctx := testSetContextToken(t, context.Background())

// FIXME: check for permissions

tournaments := map[string]string{}
for _, tournament := range testingTournaments(t, engine).Items {
tournaments[tournament.Name] = tournament.ID
}

var tests = []struct {
name string
input *ListTournamentTeamsInput
expectedErr error
expectedTeams int
// expectedOwnedTeams int?
}{
{
"empty",
&ListTournamentTeamsInput{},
ErrMissingArgument,
0,
}, {
"unknown-tournament-id",
&ListTournamentTeamsInput{TournamentID: "does not exist"},
ErrInvalidArgument,
0,
}, {
"solo-mode",
&ListTournamentTeamsInput{TournamentID: tournaments["Solo Mode"]},
nil,
1,
}, {
"test-tournament",
&ListTournamentTeamsInput{TournamentID: tournaments["Test Tournament"]},
nil,
0,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ret, err := engine.ListTournamentTeams(ctx, test.input)
if !errors.Is(err, test.expectedErr) {
t.Fatalf("Expected %#v, got %#v.", test.expectedErr, err)
}
if err != nil {
return
}

// fmt.Println(godev.PrettyJSON(ret))
for _, team := range ret.Items {
if team.TournamentID != test.input.TournamentID {
t.Fatalf("Expected %q, got %q.", test.input.TournamentID, team.TournamentID)
}
}

if len(ret.Items) != test.expectedTeams {
t.Fatalf("Expected %d, got %d.", test.expectedTeams, len(ret.Items))
}
})
}
}
2 changes: 1 addition & 1 deletion go/pkg/pwengine/api_pub_setpreferences.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (e *engine) SetPreferences(ctx context.Context, in *SetPreferencesInput) (*
var tournamentIDs []string
err := e.db.
Table("tournament").
Where("ID = ?", in.ActiveTournamentID).
Where("id = ?", in.ActiveTournamentID).
Pluck("id", &tournamentIDs).
Error
switch {
Expand Down
3 changes: 1 addition & 2 deletions go/pkg/pwengine/api_pub_setpreferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package pwengine

import (
"context"
"errors"
"testing"

"github.com/go-errors/errors"
)

func TestEngine_SetPreferences(t *testing.T) {
Expand Down
Loading

0 comments on commit ede07a3

Please sign in to comment.