diff --git a/bootstrap.php b/bootstrap.php index 26b82a4..315205d 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -8,6 +8,7 @@ require_once __DIR__ . '/vendor/autoload.php'; use Aura\Di\ContainerBuilder; +use Aura\Sql\ExtendedPdo; use AvalancheDevelopment\Talus\Talus; // load the config for the application @@ -35,6 +36,18 @@ new DateTimeZone('America/Phoenix') )); +// set up db and models +$di->set('dbal', $di->lazyNew( + 'Aura\Sql\ExtendedPdo', + (array) $config->database +)); +$di->types['Aura\Sql\ExtendedPdo'] = $di->lazyGet('dbal'); + +$di->set('typeModel', $di->lazyNew('Jacobemerick\LifestreamService\Model\Type')); + +// set up serializers +$di->set('typeSerializer', $di->lazyNew('Jacobemerick\LifestreamService\Serializer\Type')); + // set up swagger $handle = fopen(__DIR__ . '/swagger.json', 'r'); $swagger = ''; diff --git a/composer.json b/composer.json index 14b11f8..e187b1d 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "avalanche-development/talus": "^1.8", "avalanche-development/swagger-router-middleware": "^1.2", "container-interop/container-interop": "^1.2", - "aura/di": "^3.2" + "aura/di": "^3.2", + "aura/sql": "^2.5" }, "require-dev": { "codeclimate/php-test-reporter": "^0.4.4", diff --git a/composer.lock b/composer.lock index 442a2b0..e0604ce 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "f1a501728d7615ea43f7e57188399eff", - "content-hash": "11835826a9424c28cac880515aa7869e", + "hash": "b810baa811b918fa4a5448136d6e7034", + "content-hash": "10f5a0d1b39e442924940b91f68d384c", "packages": [ { "name": "aura/di", @@ -59,6 +59,59 @@ ], "time": "2016-10-04 15:52:29" }, + { + "name": "aura/sql", + "version": "2.5.1", + "source": { + "type": "git", + "url": "https://github.com/auraphp/Aura.Sql.git", + "reference": "ffc692d2037c1e8d17103ad0a128189ba6f59b96" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/auraphp/Aura.Sql/zipball/ffc692d2037c1e8d17103ad0a128189ba6f59b96", + "reference": "ffc692d2037c1e8d17103ad0a128189ba6f59b96", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "aura": { + "type": "library" + } + }, + "autoload": { + "psr-4": { + "Aura\\Sql\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-2-Clause" + ], + "authors": [ + { + "name": "Aura.Sql Contributors", + "homepage": "https://github.com/auraphp/Aura.Sql/contributors" + } + ], + "description": "A PDO extension that provides lazy connections, array quoting, identifier quoting, query profiling, value binding, and convenience methods for common fetch styles. Because it extends PDO, existing code that uses PDO can use this without any changes to the existing code.", + "homepage": "https://github.com/auraphp/Aura.Sql", + "keywords": [ + "mysql", + "pdo", + "pgsql", + "postgres", + "postgresql", + "sql server", + "sqlite", + "sqlserver", + "sqlsrv" + ], + "time": "2016-10-03 20:31:07" + }, { "name": "avalanche-development/crash-pad", "version": "0.5.0", diff --git a/src/Controller/Type.php b/src/Controller/Type.php index d367f7b..b9e3fe8 100644 --- a/src/Controller/Type.php +++ b/src/Controller/Type.php @@ -27,9 +27,11 @@ public function __construct(Container $container) */ public function getTypes(Request $request, Response $response) { - $types = [ - 'message' => 'hello world', - ]; + $types = $this->container + ->get('typeModel') + ->getTypes(); + + $types = array_map($this->container->get('typeSerializer'), $types); $types = json_encode($types); $response->getBody()->write($types); return $response; diff --git a/src/Model/Type.php b/src/Model/Type.php new file mode 100644 index 0000000..be663a3 --- /dev/null +++ b/src/Model/Type.php @@ -0,0 +1,33 @@ +extendedPdo = $extendedPdo; + } + + /** + * @return array + */ + public function getTypes() + { + $query = " + SELECT `id`, `name` + FROM `type` + ORDER BY `name` ASC"; + + return $this->extendedPdo->fetchAll($query); + } +} diff --git a/src/Serializer/Type.php b/src/Serializer/Type.php new file mode 100644 index 0000000..22028c2 --- /dev/null +++ b/src/Serializer/Type.php @@ -0,0 +1,19 @@ + $type['id'], + 'name' => $type['name'], + ]; + } +}