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.
feat: GET /tournament/teams API endoint
- Loading branch information
Showing
15 changed files
with
972 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
03acd307b08ec4c03ccfb8c4c0620f573612a5b2 ../api/pwlevel.proto | ||
0de9739f87fa0d19673b7d757bcd13a081d710ca ../api/pwengine.proto | ||
30302d31bca0924719daf9e95a0ea15dfbcfb131 ../api/pwhypervisor.proto | ||
3d7019edce77afca17dce1ea2be49cf9e406ff5d ../api/pwengine.proto | ||
58eb4b1d9b5be40a69ffdab1a13b9fb90b669839 ../api/pwsso.proto | ||
5d333f98cfbafd36ddc97b3c7c364bf94b898384 ../api/pwcompose.proto | ||
71351a6ac01962fbeaeeeafd431d481e6c1f288b Makefile | ||
bcbb91db20d1b10a3a19757fc010af0409740d4b ../api/pwdb.proto | ||
db9d6a9a49123ed17cd2cb8afb02b22257df34ef ../api/pwdb.proto |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
006d046d6df55771deca224d63b6bba42a606e4a Makefile | ||
03acd307b08ec4c03ccfb8c4c0620f573612a5b2 ../api/pwlevel.proto | ||
0de9739f87fa0d19673b7d757bcd13a081d710ca ../api/pwengine.proto | ||
30302d31bca0924719daf9e95a0ea15dfbcfb131 ../api/pwhypervisor.proto | ||
3d7019edce77afca17dce1ea2be49cf9e406ff5d ../api/pwengine.proto | ||
58eb4b1d9b5be40a69ffdab1a13b9fb90b669839 ../api/pwsso.proto | ||
5d333f98cfbafd36ddc97b3c7c364bf94b898384 ../api/pwcompose.proto | ||
bcbb91db20d1b10a3a19757fc010af0409740d4b ../api/pwdb.proto | ||
db9d6a9a49123ed17cd2cb8afb02b22257df34ef ../api/pwdb.proto |
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
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.