38 lines
803 B
Go
38 lines
803 B
Go
package todoService
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/farahty/go-mongo/app"
|
|
"github.com/farahty/go-mongo/models"
|
|
"github.com/graph-gophers/dataloader"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
var coll = "todos"
|
|
|
|
func Find(ctx context.Context) ([]*models.Todo, error) {
|
|
return app.Find[models.Todo](ctx, coll, bson.D{})
|
|
}
|
|
|
|
func FindByID(ctx context.Context, id primitive.ObjectID) (*models.Todo, error) {
|
|
|
|
loaders := app.LoaderFor(ctx)
|
|
|
|
thunk := loaders.TodosLoader.Load(ctx, dataloader.StringKey(id.Hex()))
|
|
|
|
result, err := thunk()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result.(*models.Todo), nil
|
|
|
|
}
|
|
|
|
func Create(ctx context.Context, input models.CreateTodoInput) (*models.Todo, error) {
|
|
return app.InsertOne[models.Todo](ctx, coll, input)
|
|
}
|