41 lines
862 B
Go
41 lines
862 B
Go
package userService
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.farahty.com/nimer/go-mongo/app"
|
|
"git.farahty.com/nimer/go-mongo/models"
|
|
"github.com/graph-gophers/dataloader"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
var coll = "users"
|
|
|
|
func Create(ctx context.Context, input models.CreateUserInput) (*models.User, error) {
|
|
|
|
input.Password, _ = models.MakeHash(input.Password)
|
|
|
|
return app.InsertOne[models.User](ctx, coll, input)
|
|
|
|
}
|
|
|
|
func Find(ctx context.Context) ([]*models.User, error) {
|
|
return app.Find[models.User](ctx, coll, bson.D{})
|
|
}
|
|
|
|
func FindById(ctx context.Context, id primitive.ObjectID) (*models.User, error) {
|
|
|
|
loader := app.LoaderFor(ctx).UsersLoader
|
|
|
|
thunk := loader.Load(ctx, dataloader.StringKey(id.Hex()))
|
|
|
|
result, err := thunk()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return result.(*models.User), nil
|
|
}
|