174 lines
5.8 KiB
PHP
174 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Chrosey\Inventur\Controller;
|
|
|
|
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
|
|
use Illuminate\Database\Migrations\Migrator;
|
|
use Illuminate\Filesystem\Filesystem;
|
|
|
|
class DatabaseController extends BaseController
|
|
{
|
|
|
|
function migrate($request, $response, $args)
|
|
{
|
|
$resolver = $this->db->getDatabaseManager();
|
|
$repository = new DatabaseMigrationRepository($resolver, 'migrations');
|
|
if (!$repository->repositoryExists()) {
|
|
$repository->createRepository();
|
|
}
|
|
|
|
$migrationFilesPath = __DIR__ . '/../../database/migrations/';
|
|
$fs = new Filesystem();
|
|
|
|
$migrator = new Migrator($repository, $resolver, $fs);
|
|
|
|
$files = $fs->files($migrationFilesPath);
|
|
|
|
$migrations = $migrator->run($files);
|
|
$batches = $repository->getRan();
|
|
$response->getBody()->write(json_encode([$migrations, $batches]));
|
|
return $response->withHeader('Content-Type', 'application/json');
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|