guesstimate/internal/graph/user_handler.go

47 lines
937 B
Go

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 int64, 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
}