package query import ( "context" "forge.cadoles.com/Cadoles/daddy/internal/graph/model" "forge.cadoles.com/Cadoles/daddy/internal/database" "github.com/pkg/errors" "gitlab.com/wpetit/goweb/cqrs" "gitlab.com/wpetit/goweb/middleware/container" ) const ( findUserStatement = `SELECT email, connected_at, created_at FROM users WHERE email = $1` ) type FindUserQueryRequest struct { Email string } type FindUserData struct { User *model.User } func HandleFindUserQuery(ctx context.Context, qry cqrs.Query) (interface{}, error) { req, ok := qry.Request().(*FindUserQueryRequest) if !ok { return nil, errors.WithStack(cqrs.ErrUnexpectedRequest) } ctn, err := container.From(ctx) if err != nil { return nil, errors.WithStack(err) } pool, err := database.From(ctn) if err != nil { return nil, errors.WithStack(err) } conn, err := pool.Acquire(ctx) if err != nil { return nil, errors.WithStack(err) } defer conn.Release() _, err = conn.Conn().Prepare( ctx, "find_user", findUserStatement, ) if err != nil { return nil, errors.WithStack(err) } user := &model.User{} err = conn.QueryRow(ctx, "find_user", req.Email). Scan(&user.Email, &user.ConnectedAt, &user.CreatedAt) if err != nil { return nil, errors.WithStack(err) } data := &FindUserData{ User: user, } return data, nil }