diff --git a/backend/gqlgen.yml b/backend/gqlgen.yml index f863d1c6..8fa94d11 100644 --- a/backend/gqlgen.yml +++ b/backend/gqlgen.yml @@ -88,4 +88,7 @@ models: DateTime: model: - - github.com/99designs/gqlgen/graphql.Time \ No newline at end of file + - github.com/99designs/gqlgen/graphql.Time + SteamID: + model: + - github.com/99designs/gqlgen/graphql.Uint64 \ No newline at end of file diff --git a/backend/graph/converter.go b/backend/graph/converter.go index 735ee348..6f6500cc 100644 --- a/backend/graph/converter.go +++ b/backend/graph/converter.go @@ -1,8 +1,6 @@ package graph import ( - "strconv" - "github.com/FlowingSPDG/get5loader/backend/entity" "github.com/FlowingSPDG/get5loader/backend/graph/model" ) @@ -53,7 +51,7 @@ func convertPlayer(player *entity.Player) *model.Player { return &model.Player{ ID: string(player.ID), TeamID: string(player.TeamID), - SteamID: strconv.Itoa(int(player.SteamID)), + SteamID: uint64(player.SteamID), Name: player.Name, } } @@ -123,7 +121,7 @@ func convertPlayerstat(playerstat *entity.PlayerStat) *model.PlayerStats { ID: string(playerstat.ID), MatchID: string(playerstat.MatchID), MapstatsID: string(playerstat.MapID), - SteamID: strconv.Itoa(int(playerstat.SteamID)), + SteamID: uint64(playerstat.SteamID), Name: playerstat.Name, Kills: int(playerstat.Kills), Assists: int(playerstat.Assists), @@ -163,7 +161,7 @@ func convertPlayerstats(playerstats []*entity.PlayerStat) []*model.PlayerStats { func convertUser(user *entity.User) *model.User { return &model.User{ ID: string(user.ID), - SteamID: strconv.Itoa(int(user.SteamID)), + SteamID: uint64(user.SteamID), Name: user.Name, Admin: false, Gameservers: convertGameServers(user.Servers), diff --git a/backend/graph/generated.go b/backend/graph/generated.go index 1a3f1480..30e5f9e1 100644 --- a/backend/graph/generated.go +++ b/backend/graph/generated.go @@ -2933,9 +2933,9 @@ func (ec *executionContext) _Player_steamId(ctx context.Context, field graphql.C } return graphql.Null } - res := resTmp.(string) + res := resTmp.(uint64) fc.Result = res - return ec.marshalNString2string(ctx, field.Selections, res) + return ec.marshalNSteamID2uint64(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_Player_steamId(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -2945,7 +2945,7 @@ func (ec *executionContext) fieldContext_Player_steamId(ctx context.Context, fie IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type String does not have child fields") + return nil, errors.New("field of type SteamID does not have child fields") }, } return fc, nil @@ -3153,9 +3153,9 @@ func (ec *executionContext) _PlayerStats_steamId(ctx context.Context, field grap } return graphql.Null } - res := resTmp.(string) + res := resTmp.(uint64) fc.Result = res - return ec.marshalNString2string(ctx, field.Selections, res) + return ec.marshalNSteamID2uint64(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_PlayerStats_steamId(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -3165,7 +3165,7 @@ func (ec *executionContext) fieldContext_PlayerStats_steamId(ctx context.Context IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type String does not have child fields") + return nil, errors.New("field of type SteamID does not have child fields") }, } return fc, nil @@ -5465,9 +5465,9 @@ func (ec *executionContext) _User_steamId(ctx context.Context, field graphql.Col } return graphql.Null } - res := resTmp.(string) + res := resTmp.(uint64) fc.Result = res - return ec.marshalNString2string(ctx, field.Selections, res) + return ec.marshalNSteamID2uint64(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_User_steamId(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -5477,7 +5477,7 @@ func (ec *executionContext) fieldContext_User_steamId(ctx context.Context, field IsMethod: false, IsResolver: false, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { - return nil, errors.New("field of type String does not have child fields") + return nil, errors.New("field of type SteamID does not have child fields") }, } return fc, nil @@ -7693,7 +7693,7 @@ func (ec *executionContext) unmarshalInputNewPlayer(ctx context.Context, obj int var err error ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("steamId")) - data, err := ec.unmarshalNString2string(ctx, v) + data, err := ec.unmarshalNSteamID2uint64(ctx, v) if err != nil { return it, err } @@ -7740,7 +7740,7 @@ func (ec *executionContext) unmarshalInputNewPlayerForTeam(ctx context.Context, var err error ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("steamId")) - data, err := ec.unmarshalNString2string(ctx, v) + data, err := ec.unmarshalNSteamID2uint64(ctx, v) if err != nil { return it, err } @@ -7852,7 +7852,7 @@ func (ec *executionContext) unmarshalInputNewUser(ctx context.Context, obj inter var err error ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("steamId")) - data, err := ec.unmarshalNString2string(ctx, v) + data, err := ec.unmarshalNSteamID2uint64(ctx, v) if err != nil { return it, err } @@ -7946,7 +7946,7 @@ func (ec *executionContext) unmarshalInputUserLoginSteamID(ctx context.Context, var err error ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("steamId")) - data, err := ec.unmarshalNString2string(ctx, v) + data, err := ec.unmarshalNSteamID2uint64(ctx, v) if err != nil { return it, err } @@ -9598,6 +9598,21 @@ func (ec *executionContext) marshalNPlayerStats2ᚖgithubᚗcomᚋFlowingSPDGᚋ return ec._PlayerStats(ctx, sel, v) } +func (ec *executionContext) unmarshalNSteamID2uint64(ctx context.Context, v interface{}) (uint64, error) { + res, err := graphql.UnmarshalUint64(v) + return res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalNSteamID2uint64(ctx context.Context, sel ast.SelectionSet, v uint64) graphql.Marshaler { + res := graphql.MarshalUint64(v) + if res == graphql.Null { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + } + return res +} + func (ec *executionContext) unmarshalNString2string(ctx context.Context, v interface{}) (string, error) { res, err := graphql.UnmarshalString(v) return res, graphql.ErrorOnPath(ctx, err) diff --git a/backend/graph/model/models_gen.go b/backend/graph/model/models_gen.go index 03b15bda..0538a436 100644 --- a/backend/graph/model/models_gen.go +++ b/backend/graph/model/models_gen.go @@ -66,13 +66,13 @@ type NewMatch struct { } type NewPlayer struct { - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Name string `json:"name"` Teamid string `json:"teamid"` } type NewPlayerForTeam struct { - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Name string `json:"name"` } @@ -86,7 +86,7 @@ type NewTeam struct { } type NewUser struct { - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Name string `json:"name"` Password string `json:"password"` Admin bool `json:"admin"` @@ -95,7 +95,7 @@ type NewUser struct { type Player struct { ID string `json:"id"` TeamID string `json:"teamId"` - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Name string `json:"name"` } @@ -103,7 +103,7 @@ type PlayerStats struct { ID string `json:"id"` MatchID string `json:"matchId"` MapstatsID string `json:"mapstatsId"` - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Name string `json:"name"` Kills int `json:"kills"` Assists int `json:"assists"` @@ -144,7 +144,7 @@ type Team struct { type User struct { ID string `json:"id"` - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Name string `json:"name"` Admin bool `json:"admin"` Gameservers []*GameServer `json:"gameservers"` @@ -158,6 +158,6 @@ type UserLoginID struct { } type UserLoginSteamID struct { - SteamID string `json:"steamId"` + SteamID uint64 `json:"steamId"` Password string `json:"password"` } diff --git a/backend/graph/qls/schema.graphqls b/backend/graph/qls/schema.graphqls index 32e323ea..02aa0a0a 100644 --- a/backend/graph/qls/schema.graphqls +++ b/backend/graph/qls/schema.graphqls @@ -1,9 +1,10 @@ scalar DateTime +scalar SteamID # User Operations type User { id: ID! - steamId: String! + steamId: SteamID! name: String! admin: Boolean! gameservers: [GameServer!]! @@ -12,14 +13,14 @@ type User { } input NewUser { - steamId: String! + steamId: SteamID! name: String! password: String! admin: Boolean! } input UserLoginSteamID { - steamId: String! + steamId: SteamID! password: String! } @@ -57,18 +58,18 @@ input NewTeam { type Player { id: ID! teamId: ID! - steamId: String! + steamId: SteamID! name: String! } input NewPlayer { - steamId: String! + steamId: SteamID! name: String! teamid: ID! } input NewPlayerForTeam { - steamId: String! + steamId: SteamID! name: String! } @@ -164,7 +165,7 @@ type PlayerStats { id: ID! matchId: ID! mapstatsId: ID! - steamId: String! + steamId: SteamID! name: String! kills: Int! diff --git a/backend/graph/schema.resolvers.go b/backend/graph/schema.resolvers.go index 967df836..134d4907 100644 --- a/backend/graph/schema.resolvers.go +++ b/backend/graph/schema.resolvers.go @@ -10,6 +10,7 @@ import ( "github.com/FlowingSPDG/get5loader/backend/entity" "github.com/FlowingSPDG/get5loader/backend/g5ctx" "github.com/FlowingSPDG/get5loader/backend/graph/model" + "github.com/FlowingSPDG/get5loader/backend/usecase" ) // RegisterTeam is the resolver for the registerTeam field. @@ -18,7 +19,23 @@ func (r *mutationResolver) RegisterTeam(ctx context.Context, input model.NewTeam if err != nil { return nil, err } - team, err := r.TeamUsecase.RegisterTeam(ctx, token.UserID, input.Name, input.Flag, input.Tag, input.Logo, input.Public) + inputPlayers := make([]usecase.InputPlayers, 0, len(input.Players)) + for _, player := range input.Players { + inputPlayers = append(inputPlayers, usecase.InputPlayers{ + SteamID: entity.SteamID(player.SteamID), + Name: player.Name, + }) + } + + team, err := r.TeamUsecase.RegisterTeam(ctx, usecase.RegisterTeamInput{ + UserID: token.UserID, + Name: input.Name, + Flag: input.Flag, + Tag: input.Tag, + Logo: input.Logo, + PublicTeam: input.Public, + Players: inputPlayers, + }) if err != nil { return nil, err } diff --git a/backend/usecase/team.go b/backend/usecase/team.go index 954b4328..8a92bb96 100644 --- a/backend/usecase/team.go +++ b/backend/usecase/team.go @@ -7,8 +7,23 @@ import ( "github.com/FlowingSPDG/get5loader/backend/gateway/database" ) +type InputPlayers struct { + SteamID entity.SteamID + Name string +} + +type RegisterTeamInput struct { + UserID entity.UserID + Name string + Flag string + Tag string + Logo string + PublicTeam bool + Players []InputPlayers +} + type Team interface { - RegisterTeam(ctx context.Context, userID entity.UserID, name string, flag string, tag string, logo string, publicTeam bool) (*entity.Team, error) + RegisterTeam(ctx context.Context, input RegisterTeamInput) (*entity.Team, error) GetTeam(ctx context.Context, id entity.TeamID) (*entity.Team, error) GetTeamsByUser(ctx context.Context, userID entity.UserID) ([]*entity.Team, error) } @@ -23,33 +38,34 @@ func NewTeam(repositoryConnector database.RepositoryConnector) Team { } } -func (t *team) RegisterTeam(ctx context.Context, userID entity.UserID, name string, flag string, tag string, logo string, publicTeam bool) (*entity.Team, error) { +func (t *team) RegisterTeam(ctx context.Context, input RegisterTeamInput) (*entity.Team, error) { if err := t.repositoryConnector.Open(); err != nil { return nil, err } defer t.repositoryConnector.Close() - repository := t.repositoryConnector.GetTeamsRepository() + teamRepository := t.repositoryConnector.GetTeamsRepository() + playerRepository := t.repositoryConnector.GetPlayersRepository() - teamID, err := repository.AddTeam(ctx, userID, name, tag, flag, logo, publicTeam) + teamID, err := teamRepository.AddTeam(ctx, input.UserID, input.Name, input.Tag, input.Flag, input.Logo, input.PublicTeam) if err != nil { return nil, err } - team, err := repository.GetTeam(ctx, teamID) + // TODO: Batch addする + for _, player := range input.Players { + playerRepository.AddPlayer(ctx, teamID, player.SteamID, player.Name) + } + + team, err := teamRepository.GetTeam(ctx, teamID) if err != nil { return nil, err } - return &entity.Team{ - ID: entity.TeamID(team.ID), - UserID: entity.UserID(team.UserID), - Name: team.Name, - Flag: team.Flag, - Tag: team.Tag, - Logo: team.Logo, - Public: team.Public, - Players: []*entity.Player{}, - }, nil + players, err := playerRepository.GetPlayersByTeam(ctx, teamID) + if err != nil { + return nil, err + } + return convertTeam(team, players), nil } // GetTeam implements Team. @@ -72,26 +88,7 @@ func (t *team) GetTeam(ctx context.Context, id entity.TeamID) (*entity.Team, err return nil, err } - teamPlayers := make([]*entity.Player, 0, len(players)) - for _, player := range players { - teamPlayers = append(teamPlayers, &entity.Player{ - ID: entity.PlayerID(player.ID), - TeamID: entity.TeamID(player.TeamID), - SteamID: entity.SteamID(player.SteamID), - Name: player.Name, - }) - } - - return &entity.Team{ - ID: entity.TeamID(team.ID), - UserID: entity.UserID(team.UserID), - Name: team.Name, - Flag: team.Flag, - Tag: team.Tag, - Logo: team.Logo, - Public: team.Public, - Players: teamPlayers, - }, nil + return convertTeam(team, players), nil } // GetTeamsByUser implements Team. @@ -116,26 +113,7 @@ func (t *team) GetTeamsByUser(ctx context.Context, userID entity.UserID) ([]*ent return nil, err } - teamPlayers := make([]*entity.Player, 0, len(players)) - for _, player := range players { - teamPlayers = append(teamPlayers, &entity.Player{ - ID: entity.PlayerID(player.ID), - TeamID: entity.TeamID(player.TeamID), - SteamID: entity.SteamID(player.SteamID), - Name: player.Name, - }) - } - - ret = append(ret, &entity.Team{ - ID: entity.TeamID(team.ID), - UserID: entity.UserID(team.UserID), - Name: team.Name, - Flag: team.Flag, - Tag: team.Tag, - Logo: team.Logo, - Public: team.Public, - Players: teamPlayers, - }) + ret = append(ret, convertTeam(team, players)) } return ret, nil }