[TASK] update DatabaseController

- refactoring
- adding seed-function
This commit is contained in:
chrosey 2020-01-22 00:06:22 +01:00
parent 1773cae679
commit 12fc0d4bf5
2 changed files with 146 additions and 15 deletions

View File

@ -5,23 +5,14 @@ namespace Chrosey\Inventur\Controller;
use Illuminate\Database\Migrations\DatabaseMigrationRepository; use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\Migrator; use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem; 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) function migrate($request, $response, $args)
{ {
$capsule = $this->container->get('db')->getDatabaseManager(); $resolver = $this->db->getDatabaseManager();
$repository = new DatabaseMigrationRepository($capsule, 'migrations'); $repository = new DatabaseMigrationRepository($resolver, 'migrations');
if (!$repository->repositoryExists()) { if (!$repository->repositoryExists()) {
$repository->createRepository(); $repository->createRepository();
} }
@ -29,14 +20,154 @@ class DatabaseController
$migrationFilesPath = __DIR__ . '/../../database/migrations/'; $migrationFilesPath = __DIR__ . '/../../database/migrations/';
$fs = new Filesystem(); $fs = new Filesystem();
$migrator = new Migrator($repository, $capsule, $fs); $migrator = new Migrator($repository, $resolver, $fs);
$files = $fs->files($migrationFilesPath); $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');
} }
} }

0
src/Model/Article.php Normal file
View File