From 13bd9f50e01c75ccf37314b17b876d6b5acb98f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 7 Nov 2021 14:57:57 +0100 Subject: [PATCH] User: Add optional field for avatar url and nickname --- database/schema.go | 4 +- graph/generated/generated.go | 104 +++++++++++++++++++++++++++++++++++ graph/model/models_gen.go | 18 +++--- graph/schema.graphqls | 4 ++ graph/schema.resolvers.go | 17 +++--- 5 files changed, 132 insertions(+), 15 deletions(-) diff --git a/database/schema.go b/database/schema.go index 2e196a5..d8652a7 100644 --- a/database/schema.go +++ b/database/schema.go @@ -14,7 +14,9 @@ CREATE TABLE "User" ( teamspeak_id VARCHAR(255) NOT NULL UNIQUE, elo SMALLINT NOT NULL, admin BOOLEAN NOT NULL DEFAULT FALSE, - queueing BOOLEAN NOT NULL DEFAULT FALSE + queueing BOOLEAN NOT NULL DEFAULT FALSE, + avatar VARCHAR(255), + name VARCHAR(255) ); CREATE TABLE "Team" ( diff --git a/graph/generated/generated.go b/graph/generated/generated.go index 97bb703..93a5056 100644 --- a/graph/generated/generated.go +++ b/graph/generated/generated.go @@ -59,8 +59,10 @@ type ComplexityRoot struct { User struct { Admin func(childComplexity int) int + Avatar func(childComplexity int) int Elo func(childComplexity int) int ID func(childComplexity int) int + Name func(childComplexity int) int SteamID func(childComplexity int) int TeamspeakID func(childComplexity int) int } @@ -205,6 +207,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.User.Admin(childComplexity), true + case "User.avatar": + if e.complexity.User.Avatar == nil { + break + } + + return e.complexity.User.Avatar(childComplexity), true + case "User.elo": if e.complexity.User.Elo == nil { break @@ -219,6 +228,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.User.ID(childComplexity), true + case "User.name": + if e.complexity.User.Name == nil { + break + } + + return e.complexity.User.Name(childComplexity), true + case "User.steamID": if e.complexity.User.SteamID == nil { break @@ -303,6 +319,8 @@ var sources = []*ast.Source{ teamspeakID: String! elo: Int! admin: Boolean! + avatar: String + name: String } type Query { @@ -313,6 +331,8 @@ type Query { input NewUser { steamID: String! teamspeakID: String! + avatar: String + name: String } type Mutation { @@ -1125,6 +1145,70 @@ func (ec *executionContext) _User_admin(ctx context.Context, field graphql.Colle return ec.marshalNBoolean2bool(ctx, field.Selections, res) } +func (ec *executionContext) _User_avatar(ctx context.Context, field graphql.CollectedField, obj *model.User) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "User", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Avatar, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*string) + fc.Result = res + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) +} + +func (ec *executionContext) _User_name(ctx context.Context, field graphql.CollectedField, obj *model.User) (ret graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + fc := &graphql.FieldContext{ + Object: "User", + Field: field, + Args: nil, + IsMethod: false, + IsResolver: false, + } + + ctx = graphql.WithFieldContext(ctx, fc) + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Name, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + return graphql.Null + } + res := resTmp.(*string) + fc.Result = res + return ec.marshalOString2ᚖstring(ctx, field.Selections, res) +} + func (ec *executionContext) ___Directive_name(ctx context.Context, field graphql.CollectedField, obj *introspection.Directive) (ret graphql.Marshaler) { defer func() { if r := recover(); r != nil { @@ -2272,6 +2356,22 @@ func (ec *executionContext) unmarshalInputNewUser(ctx context.Context, obj inter 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 + } } } @@ -2435,6 +2535,10 @@ func (ec *executionContext) _User(ctx context.Context, sel ast.SelectionSet, obj if out.Values[i] == graphql.Null { invalids++ } + case "avatar": + out.Values[i] = ec._User_avatar(ctx, field, obj) + case "name": + out.Values[i] = ec._User_name(ctx, field, obj) default: panic("unknown field " + strconv.Quote(field.Name)) } diff --git a/graph/model/models_gen.go b/graph/model/models_gen.go index b42248b..a6b3912 100644 --- a/graph/model/models_gen.go +++ b/graph/model/models_gen.go @@ -3,14 +3,18 @@ package model type NewUser struct { - SteamID string `json:"steamID"` - TeamspeakID string `json:"teamspeakID"` + SteamID string `json:"steamID"` + TeamspeakID string `json:"teamspeakID"` + Avatar *string `json:"avatar"` + Name *string `json:"name"` } type User struct { - ID int `json:"id"` - SteamID string `json:"steamID"` - TeamspeakID string `json:"teamspeakID"` - Elo int `json:"elo"` - Admin bool `json:"admin"` + ID int `json:"id"` + SteamID string `json:"steamID"` + TeamspeakID string `json:"teamspeakID"` + Elo int `json:"elo"` + Admin bool `json:"admin"` + Avatar *string `json:"avatar"` + Name *string `json:"name"` } diff --git a/graph/schema.graphqls b/graph/schema.graphqls index 2637012..7978160 100644 --- a/graph/schema.graphqls +++ b/graph/schema.graphqls @@ -4,6 +4,8 @@ type User { teamspeakID: String! elo: Int! admin: Boolean! + avatar: String + name: String } type Query { @@ -14,6 +16,8 @@ type Query { input NewUser { steamID: String! teamspeakID: String! + avatar: String + name: String } type Mutation { diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index 637f13f..e9893b3 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -56,9 +56,10 @@ func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) } query := ` - INSERT INTO "User" (steam_id, teamspeak_id, elo, admin) - VALUES ($1, $2, $3, $4)` - _, err = database.DB.Exec(query, input.SteamID, input.TeamspeakID, elo.Elo, isAdmin) + INSERT INTO "User" (steam_id, teamspeak_id, elo, admin, avatar, name) + VALUES ($1, $2, $3, $4, $5, $6)` + _, err = database.DB.Exec(query, input.SteamID, input.TeamspeakID, elo.Elo, + isAdmin, input.Avatar, input.Name) if err != nil { return "", err } @@ -323,10 +324,11 @@ func (r *queryResolver) UserBySteam(ctx context.Context, steamID string) (*model user.SteamID = steamID query := ` - SELECT id, teamspeak_id, Elo, admin + SELECT id, teamspeak_id, Elo, admin, avatar, name FROM "User" WHERE steam_id = $1` - err := database.DB.QueryRow(query, steamID).Scan(&user.ID, &user.TeamspeakID, &user.Elo, &user.Admin) + err := database.DB.QueryRow(query, steamID).Scan(&user.ID, + &user.TeamspeakID, &user.Elo, &user.Admin, &user.Avatar, &user.Name) if err != nil { return nil, err } @@ -339,10 +341,11 @@ func (r *queryResolver) UserByTs(ctx context.Context, teamspeakID string) (*mode user.TeamspeakID = teamspeakID query := ` - SELECT id, steam_id, Elo, admin + SELECT id, steam_id, Elo, admin, avatar, name FROM "User" WHERE teamspeak_id = $1` - err := database.DB.QueryRow(query, teamspeakID).Scan(&user.ID, &user.SteamID, &user.Elo, &user.Admin) + err := database.DB.QueryRow(query, teamspeakID).Scan(&user.ID, + &user.SteamID, &user.Elo, &user.Admin, &user.Avatar, &user.Name) if err != nil { return nil, err } -- 2.44.0