forked from pathwar/pathwar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request pathwar#164 from pathwar/dev/moul/tournament-list-…
…teams WIP feat: GET /tournament/teams API endoint
- Loading branch information
Showing
16 changed files
with
975 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.