38 lines
843 B
Go
38 lines
843 B
Go
package categoryService
|
|
|
|
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 = "categories"
|
|
|
|
func Find(ctx context.Context) ([]*models.Category, error) {
|
|
return app.Find[models.Category](ctx, coll, bson.D{})
|
|
}
|
|
|
|
func Create(ctx context.Context, input models.CreateCategoryInput) (*models.Category, error) {
|
|
return app.InsertOne[models.Category](ctx, coll, input)
|
|
}
|
|
|
|
func FindByID(ctx context.Context, id primitive.ObjectID) (*models.Category, error) {
|
|
|
|
loaders := app.LoaderFor(ctx)
|
|
|
|
thunk := loaders.CategoryLoader.Load(ctx, dataloader.StringKey(id.Hex()))
|
|
|
|
result, err := thunk()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result.(*models.Category), nil
|
|
|
|
}
|