From 4732ed3f94ab591fb6a669020da183671af6ab96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 7 Nov 2021 15:47:33 +0100 Subject: [PATCH] mutation: Add updateUser Can be used to update, avatar url, name, elo and teamspeakID. --- graph/generated/generated.go | 154 +++++++++++++++++++++++++++++++++++ graph/model/models_gen.go | 7 ++ graph/schema.graphqls | 8 ++ graph/schema.resolvers.go | 71 ++++++++++++++++ 4 files changed, 240 insertions(+) diff --git a/graph/generated/generated.go b/graph/generated/generated.go index 93a5056..a603a2b 100644 --- a/graph/generated/generated.go +++ b/graph/generated/generated.go @@ -50,6 +50,7 @@ type ComplexityRoot struct { DeleteUser func(childComplexity int, id int) int FinishMatch func(childComplexity int, id int, winner string) int StartQueue func(childComplexity int, id int) int + UpdateUser func(childComplexity int, id int, input model.UserInput) int } Query struct { @@ -71,6 +72,7 @@ type ComplexityRoot struct { type MutationResolver interface { CreateUser(ctx context.Context, input model.NewUser) (string, error) DeleteUser(ctx context.Context, id int) (string, error) + UpdateUser(ctx context.Context, id int, input model.UserInput) (*model.User, error) StartQueue(ctx context.Context, id int) (int, error) CancelQueue(ctx context.Context, id int) (int, error) CreateMatch(ctx context.Context) (int, error) @@ -176,6 +178,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Mutation.StartQueue(childComplexity, args["id"].(int)), true + case "Mutation.updateUser": + if e.complexity.Mutation.UpdateUser == nil { + break + } + + args, err := ec.field_Mutation_updateUser_args(context.TODO(), rawArgs) + if err != nil { + return 0, false + } + + return e.complexity.Mutation.UpdateUser(childComplexity, args["id"].(int), args["input"].(model.UserInput)), true + case "Query.userBySteam": if e.complexity.Query.UserBySteam == nil { break @@ -335,9 +349,17 @@ input NewUser { name: String } +input UserInput { + elo: Int + teamspeakID: String + avatar: String + name: String +} + type Mutation { createUser(input: NewUser!): String! deleteUser(id: Int!): String! + updateUser(id: Int!, input: UserInput!): User startQueue(id: Int!): Int! cancelQueue(id: Int!): Int! createMatch: Int! @@ -451,6 +473,30 @@ func (ec *executionContext) field_Mutation_startQueue_args(ctx context.Context, return args, nil } +func (ec *executionContext) field_Mutation_updateUser_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { + var err error + args := map[string]interface{}{} + var arg0 int + if tmp, ok := rawArgs["id"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) + arg0, err = ec.unmarshalNInt2int(ctx, tmp) + if err != nil { + return nil, err + } + } + args["id"] = arg0 + var arg1 model.UserInput + if tmp, ok := rawArgs["input"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("input")) + arg1, err = ec.unmarshalNUserInput2gitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐUserInput(ctx, tmp) + if err != nil { + return nil, err + } + } + args["input"] = arg1 + return args, nil +} + func (ec *executionContext) field_Query___type_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} @@ -618,6 +664,45 @@ func (ec *executionContext) _Mutation_deleteUser(ctx context.Context, field grap return ec.marshalNString2string(ctx, field.Selections, res) } +func (ec *executionContext) _Mutation_updateUser(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "Mutation", + Field: field, + Args: nil, + IsMethod: true, + IsResolver: true, + } + + ctx = graphql.WithFieldContext(ctx, fc) + rawArgs := field.ArgumentMap(ec.Variables) + args, err := ec.field_Mutation_updateUser_args(ctx, rawArgs) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + fc.Args = args + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Mutation().UpdateUser(rctx, args["id"].(int), args["input"].(model.UserInput)) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*model.User) + fc.Result = res + return ec.marshalOUser2ᚖgitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐUser(ctx, field.Selections, res) +} + func (ec *executionContext) _Mutation_startQueue(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -2378,6 +2463,53 @@ func (ec *executionContext) unmarshalInputNewUser(ctx context.Context, obj inter return it, nil } +func (ec *executionContext) unmarshalInputUserInput(ctx context.Context, obj interface{}) (model.UserInput, error) { + var it model.UserInput + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "elo": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("elo")) + it.Elo, err = ec.unmarshalOInt2ᚖint(ctx, v) + if err != nil { + return it, err + } + case "teamspeakID": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("teamspeakID")) + it.TeamspeakID, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "avatar": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("avatar")) + it.Avatar, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + case "name": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("name")) + it.Name, err = ec.unmarshalOString2ᚖstring(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + // endregion **************************** input.gotpl ***************************** // region ************************** interface.gotpl *************************** @@ -2411,6 +2543,8 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet) if out.Values[i] == graphql.Null { invalids++ } + case "updateUser": + out.Values[i] = ec._Mutation_updateUser(ctx, field) case "startQueue": out.Values[i] = ec._Mutation_startQueue(ctx, field) if out.Values[i] == graphql.Null { @@ -2850,6 +2984,11 @@ func (ec *executionContext) marshalNString2string(ctx context.Context, sel ast.S return res } +func (ec *executionContext) unmarshalNUserInput2gitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐUserInput(ctx context.Context, v interface{}) (model.UserInput, error) { + res, err := ec.unmarshalInputUserInput(ctx, v) + return res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) marshalN__Directive2githubᚗcomᚋ99designsᚋgqlgenᚋgraphqlᚋintrospectionᚐDirective(ctx context.Context, sel ast.SelectionSet, v introspection.Directive) graphql.Marshaler { return ec.___Directive(ctx, sel, &v) } @@ -3131,6 +3270,21 @@ func (ec *executionContext) marshalOBoolean2ᚖbool(ctx context.Context, sel ast return graphql.MarshalBoolean(*v) } +func (ec *executionContext) unmarshalOInt2ᚖint(ctx context.Context, v interface{}) (*int, error) { + if v == nil { + return nil, nil + } + res, err := graphql.UnmarshalInt(v) + return &res, graphql.ErrorOnPath(ctx, err) +} + +func (ec *executionContext) marshalOInt2ᚖint(ctx context.Context, sel ast.SelectionSet, v *int) graphql.Marshaler { + if v == nil { + return graphql.Null + } + return graphql.MarshalInt(*v) +} + func (ec *executionContext) unmarshalOString2string(ctx context.Context, v interface{}) (string, error) { res, err := graphql.UnmarshalString(v) return res, graphql.ErrorOnPath(ctx, err) diff --git a/graph/model/models_gen.go b/graph/model/models_gen.go index a6b3912..610a031 100644 --- a/graph/model/models_gen.go +++ b/graph/model/models_gen.go @@ -18,3 +18,10 @@ type User struct { Avatar *string `json:"avatar"` Name *string `json:"name"` } + +type UserInput struct { + Elo *int `json:"elo"` + TeamspeakID *string `json:"teamspeakID"` + Avatar *string `json:"avatar"` + Name *string `json:"name"` +} diff --git a/graph/schema.graphqls b/graph/schema.graphqls index 7978160..1a7077f 100644 --- a/graph/schema.graphqls +++ b/graph/schema.graphqls @@ -20,9 +20,17 @@ input NewUser { name: String } +input UserInput { + elo: Int + teamspeakID: String + avatar: String + name: String +} + type Mutation { createUser(input: NewUser!): String! deleteUser(id: Int!): String! + updateUser(id: Int!, input: UserInput!): User startQueue(id: Int!): Int! cancelQueue(id: Int!): Int! createMatch: Int! diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index e9893b3..067f761 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -73,6 +73,77 @@ func (r *mutationResolver) DeleteUser(ctx context.Context, id int) (string, erro return "Deleted", err } +func (r *mutationResolver) UpdateUser(ctx context.Context, id int, input model.UserInput) (*model.User, error) { + var user model.User + + tx, err := database.DB.Begin() + if err != nil { + return nil, err + } + defer tx.Rollback() + + if input.Elo != nil { + query := ` + UPDATE "User" + SET elo = $1 + WHERE id = $2` + _, err := tx.Exec(query, input.Elo, id) + if err != nil { + return nil, fmt.Errorf("failed to update elo: %w", err) + } + } + + if input.TeamspeakID != nil { + query := ` + UPDATE "User" + SET teamspeak_id = $1 + WHERE id = $2` + _, err := tx.Exec(query, input.TeamspeakID, id) + if err != nil { + return nil, fmt.Errorf("failed to update teamspeakID: %w", err) + } + } + + if input.Avatar != nil { + query := ` + UPDATE "User" + SET avatar = $1 + WHERE id = $2` + _, err := tx.Exec(query, input.Avatar, id) + if err != nil { + return nil, fmt.Errorf("failed to update avatar: %w", err) + } + } + + if input.Name != nil { + query := ` + UPDATE "User" + SET name = $1 + WHERE id = $2` + _, err := tx.Exec(query, input.Name, id) + if err != nil { + return nil, fmt.Errorf("failed to update name: %w", err) + } + } + + query := ` + SELECT steam_id, teamspeak_id, elo, admin, avatar, name + FROM "User" + WHERE id = $1` + err = tx.QueryRow(query, id).Scan(&user.SteamID, &user.TeamspeakID, + &user.Elo, &user.Admin, &user.Avatar, &user.Name) + if err != nil { + return nil, err + } + + err = tx.Commit() + if err != nil { + return nil, err + } + + return &user, nil +} + func (r *mutationResolver) StartQueue(ctx context.Context, id int) (int, error) { players, err := database.PlayersInQueue() if err != nil { -- 2.44.0