From 12fc0d4bf56157767cb5261ad83fc881e888d893 Mon Sep 17 00:00:00 2001 From: chrosey Date: Wed, 22 Jan 2020 00:06:22 +0100 Subject: [PATCH] [TASK] update DatabaseController - refactoring - adding seed-function --- src/Controller/DatabaseController.php | 161 +++++++++++++++++++++++--- src/Model/Article.php | 0 2 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 src/Model/Article.php diff --git a/src/Controller/DatabaseController.php b/src/Controller/DatabaseController.php index 7ba33b6..98d1a47 100644 --- a/src/Controller/DatabaseController.php +++ b/src/Controller/DatabaseController.php @@ -5,23 +5,14 @@ namespace Chrosey\Inventur\Controller; use Illuminate\Database\Migrations\DatabaseMigrationRepository; use Illuminate\Database\Migrations\Migrator; use Illuminate\Filesystem\Filesystem; -use Psr\Container\ContainerInterface; -use Slim\Psr7\Stream; -class DatabaseController +class DatabaseController extends BaseController { - protected $container; - - function __construct(ContainerInterface $container) - { - - $this->container = $container; - } function migrate($request, $response, $args) { - $capsule = $this->container->get('db')->getDatabaseManager(); - $repository = new DatabaseMigrationRepository($capsule, 'migrations'); + $resolver = $this->db->getDatabaseManager(); + $repository = new DatabaseMigrationRepository($resolver, 'migrations'); if (!$repository->repositoryExists()) { $repository->createRepository(); } @@ -29,14 +20,154 @@ class DatabaseController $migrationFilesPath = __DIR__ . '/../../database/migrations/'; $fs = new Filesystem(); - $migrator = new Migrator($repository, $capsule, $fs); + $migrator = new Migrator($repository, $resolver, $fs); $files = $fs->files($migrationFilesPath); - $migrator->run($files); + $migrations = $migrator->run($files); + $batches = $repository->getRan(); + $response->getBody()->write(json_encode([$migrations, $batches])); + return $response->withHeader('Content-Type', 'application/json'); } - function getMigrations() + function seed($request, $response, $args) { + $db = $this->db; + $data = []; + + if ($db::schema()->hasTable('dimensions')) { + $entries = [ + [ + 'name' => 'Liter', + 'short' => 'l' + ], [ + 'name' => 'Stück', + 'short' => 'Stk' + ], [ + 'name' => 'Flasche', + 'short' => 'Fl' + ], [ + 'name' => 'Glas', + 'short' => 'Gl' + ], [ + 'name' => 'Tasse', + 'short' => 'T' + ] + ]; + $data['dimensions'] = $entries; + + file_put_contents(__DIR__ . '/../../database/seeds/dimensions.json', json_encode($entries)); + foreach ($entries as $item) { + try { + $db->table('dimensions')->insert($item); + } catch (\Throwable $th) { + $this->logger->error($th); + } + } + } + + if ($db::schema()->hasTable('groups')) { + $entries = [ + [ + 'name' => 'Bier' + ], [ + 'name' => 'afG Gastro' + ], [ + 'name' => 'afG offen' + ], [ + 'name' => 'Wein und Sekt' + ], [ + 'name' => 'Fingerfood' + ], [ + 'name' => 'Heißgetränke' + ] + ]; + $data['groups'] = $entries; + file_put_contents(__DIR__ . '/../../database/seeds/groups.json', json_encode($entries)); + + foreach ($entries as $item) { + try { + $db->table('groups')->insert($item); + } catch (\Throwable $th) { + $this->logger->error($th); + } + } + } + + if ($db::schema()->hasTable('sets')) { + $entries = [ + [ + 'name' => 'UG' + ], [ + 'name' => 'mobile' + ], [ + 'name' => 'Studio' + ], [ + 'name' => 'Domstufen' + ] + ]; + $data['sets'] = $entries; + file_put_contents(__DIR__ . '/../../database/seeds/sets.json', json_encode($entries)); + foreach ($entries as $item) { + try { + $db->table('sets')->insert($item); + } catch (\Throwable $th) { + $this->logger->error($th); + } + } + } + + if ($db::schema()->hasTable('articles')) { + + ini_set('encode_precision', -1); + $file = file_get_contents(__DIR__ . '/../../data/articles.theater.json'); + $entries = json_decode($file, true); + + file_put_contents(__DIR__ . '/../../database/seeds/articles.theater.json', json_encode($entries)); + + $data['articles'] = $entries; + foreach ($entries as $entry) { + try { + $cDim = $db + ->table('dimensions') + ->where( + 'short', + '=', + str_replace('.', '', $entry['dimension']) + )->first(); + $pDim = $db + ->table('dimensions') + ->where( + 'short', + '=', + str_replace('.', '', $entry['portion']['type']) + )->first(); + $group = $db + ->table('groups') + ->where( + 'name', + '=', + str_replace('.', '', $entry['group']) + )->first(); + $item = [ + 'name' => $entry['name'], + 'short' => $entry['short'], + 'content_size' => $entry['content']['size'], + 'portion_size' => $entry['portion']['size'], + 'content_dimension' => $cDim->id, + 'portion_dimension' => $pDim->id, + 'portion_price' => $entry['portion']['price'], + 'group_id' => $group->id + ]; + $db->table('articles')->insert($item); + } catch (\Throwable $th) { + $this->logger->error($th); + } + } + } + + $articles = $db->table('articles')->get(); + $response->getBody()->write(json_encode($data)); + return $response->withHeader('Content-Type', 'application/json'); } } diff --git a/src/Model/Article.php b/src/Model/Article.php new file mode 100644 index 0000000..e69de29