guesstimate/internal/graph/user_handler.go

47 lines
938 B
Go
Raw Normal View History

2020-08-08 15:04:59 +02:00
package graph
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/v2/gqlerror"
"forge.cadoles.com/Cadoles/guesstimate/internal/model"
"github.com/pkg/errors"
)
func handleCurrentUser(ctx context.Context) (*model.User, error) {
user, _, err := getSessionUser(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
return user, nil
}
func handleUpdateUser(ctx context.Context, id string, changes model.UserChanges) (*model.User, error) {
user, db, err := getSessionUser(ctx)
if err != nil {
return nil, errors.WithStack(err)
}
if user.ID != id {
graphql.AddError(ctx, gqlerror.Errorf("Forbidden"))
}
repo := model.NewUserRepository(db)
userChanges := &model.User{}
if changes.Name != nil {
userChanges.Name = changes.Name
}
user, err = repo.UpdateUserByEmail(ctx, user.Email, userChanges)
if err != nil {
return nil, errors.WithStack(err)
}
return user, nil
}