schema.resolvers.go 3.8 KB
package graph

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"context"
	"fmt"
	"github.com/tiptok/godevp/pkg/domain/role"

	"github.com/tiptok/gocomm/common"
	"github.com/tiptok/godevp/pkg/application/users/command"
	"github.com/tiptok/godevp/pkg/application/users/query"
	"github.com/tiptok/godevp/pkg/application/users/service"
	domain "github.com/tiptok/godevp/pkg/domain/users"
	"github.com/tiptok/godevp/pkg/port/graphql/graph/generated"
	"github.com/tiptok/godevp/pkg/port/graphql/graph/model"
)

func (r *mutationResolver) CreateUsers(ctx context.Context, input model.CreateUsersInput) (*model.Users, error) {
	usersService := service.NewUsersService(nil)
	createUsersCommand := &command.CreateUsersCommand{
		Name:  input.Name,
		Phone: input.Phone,
	}
	data, err := usersService.CreateUsers(createUsersCommand)
	if err != nil {
		return nil, err
	}
	if user, ok := data.(*domain.Users); ok {
		return &model.Users{
			Name:  &user.Name,
			Phone: &user.Phone,
		}, nil
	}
	return nil, fmt.Errorf("type error except: domain.Users")
}

func (r *mutationResolver) RemoveUsers(ctx context.Context, input model.RemoveUsersInput) (*model.Users, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *mutationResolver) UpdateUsers(ctx context.Context, input model.UpdateUsersInput) (*model.Users, error) {
	panic(fmt.Errorf("not implemented"))
}

func (r *queryResolver) GetUsers(ctx context.Context, input model.GetUsersInput) (*model.Users, error) {
	usersService := service.NewUsersService(nil)
	getUsersCommand := &query.GetUsersQuery{
		UsersId: int64(input.ID),
	}
	data, err := usersService.GetUsers(getUsersCommand)
	if err != nil {
		return nil, err
	}
	if user, ok := data.(*domain.Users); ok {
		return &model.Users{
			Name:   &user.Name,
			Phone:  &user.Phone,
			Status: &user.Status,
		}, nil
	}
	return nil, fmt.Errorf("type error except: domain.Users")
}

func (r *queryResolver) FindUsers(ctx context.Context, input model.ListUsersInput) ([]*model.Users, error) {
	usersService := service.NewUsersService(nil)
	getUsersCommand := &query.ListUsersQuery{
		Limit:  input.Limit,
		Offset: input.Offset,
	}
	data, err := usersService.ListUsers(getUsersCommand)
	if err != nil {
		return nil, err
	}
	if userMap, ok := data.(map[string]interface{}); ok {
		users := userMap["users"].([]*domain.Users)
		var ret []*model.Users
		for _, user := range users {
			createTime := common.AssertString(user.CreateTime.Unix())
			updateTime := common.AssertString(user.UpdateTime.Unix())
			ret = append(ret, &model.Users{
				Name:       &user.Name,
				Phone:      &user.Phone,
				Status:     &user.Status,
				AdminType:  &user.AdminType,
				CreateTime: &createTime,
				UpdateTime: &updateTime,
			})
		}
		return ret, nil
	}
	return nil, fmt.Errorf("type error except: domain.Users")
}

func (r *usersResolver) Roles(ctx context.Context, obj *model.Users) ([]*model.Role, error) {
	var ro = &role.Role{
		Id:       1,
		RoleName: "ccc",
		ParentId: 1,
	}
	var id = int(ro.Id)
	return []*model.Role{
		&model.Role{
			ID:       &id,
			RoleName: &ro.RoleName,
		},
		&model.Role{
			ID:       &id,
			RoleName: &ro.RoleName,
		},
	}, nil
}

// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }

// Query returns generated.QueryResolver implementation.
func (r *Resolver) Query() generated.QueryResolver { return &queryResolver{r} }

// Users returns generated.UsersResolver implementation.
func (r *Resolver) Users() generated.UsersResolver { return &usersResolver{r} }

type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
type usersResolver struct{ *Resolver }