GithubHelp home page GithubHelp logo

ne's Introduction

this is a web project built by gin

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/itachilee/gin/global"
"github.com/itachilee/gin/graph/generated"
"github.com/itachilee/gin/graph/model"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"

)

// CreateTodo is the resolver for the createTodo field. func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {

collection := global.MgoCli.Database("blog").Collection("todo")

// ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
res, err := collection.InsertOne(ctx, bson.D{
	{Key: "userId", Value: input.UserID},
	{Key: "text", Value: input.Text}})
if err != nil {
	fmt.Print(err)
}
id := res.InsertedID.(primitive.ObjectID)

todo := &model.Todo{
	ID:   id.String(), // fmt.Sprint((rand.Int())),
	Text: input.Text,
}

return todo, nil

}

// Todos is the resolver for the todos field. func (r *queryResolver) Todos(ctx context.Context) ([]*model.Todo, error) { // fmt.Print("hhh") // todos := make([]*model.Todo, 0) // for i := 0; i < 3; i++ { // todo := &model.Todo{ // ID: string(rune(i)), // } // todos = append(todos, todo) // } // return todos, nil

findOption := options.Find()
findOption.SetLimit(5)
collection := global.MgoCli.Database("blog").Collection("todo")
cur, err := collection.Find(context.TODO(), bson.D{{}}, findOption)
if err != nil {
	fmt.Print(err)
}
var todos []*model.Todo
for cur.Next(context.TODO()) {
	var todo model.Todo
	err = cur.Decode(&todo)

	if err != nil {
		fmt.Print(err)
	}
	todos = append(todos, &todo)
}
if err := cur.Err(); err != nil {
	fmt.Print(err)
}
cur.Close(context.TODO())
return todos, nil
// panic(fmt.Errorf("not implemented"))

}

// Todo is the resolver for the todo field. func (r *queryResolver) Todo(ctx context.Context, id string) (*model.Todo, error) { panic(fmt.Errorf("not implemented")) }

// 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} }

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

ne's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.