Reorganize project part 3

This commit is contained in:
Bernd Bestel
2018-04-12 21:13:38 +02:00
parent 7dcd39f82f
commit 5a1d21ef31
86 changed files with 625 additions and 583 deletions

View File

@@ -4,7 +4,7 @@ namespace Grocy\Controllers;
class BaseApiController extends BaseController
{
protected function ApiEncode($response)
protected function ApiResponse($response)
{
return json_encode($response);
}

View File

@@ -2,7 +2,8 @@
namespace Grocy\Controllers;
use Grocy\Services\DatabaseService;
use \Grocy\Services\DatabaseService;
use \Grocy\Services\ApplicationService;
class BaseController
{
@@ -11,6 +12,9 @@ class BaseController
$databaseService = new DatabaseService();
$this->Database = $databaseService->GetDbConnection();
$applicationService = new ApplicationService();
$container->view->set('version', $applicationService->GetInstalledVersion());
}
protected $AppContainer;

View File

@@ -2,7 +2,7 @@
namespace Grocy\Controllers;
use Grocy\Services\BatteriesService;
use \Grocy\Services\BatteriesService;
class BatteriesApiController extends BaseApiController
{
@@ -14,7 +14,7 @@ class BatteriesApiController extends BaseApiController
protected $BatteriesService;
public function TrackChargeCycle($request, $response, $args)
public function TrackChargeCycle(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$trackedTime = date('Y-m-d H:i:s');
if (isset($request->getQueryParams()['tracked_time']) && !empty($request->getQueryParams()['tracked_time']))
@@ -22,11 +22,11 @@ class BatteriesApiController extends BaseApiController
$trackedTime = $request->getQueryParams()['tracked_time'];
}
return $this->ApiEncode(array('success' => $this->BatteriesService->TrackChargeCycle($args['batteryId'], $trackedTime)));
return $this->ApiResponse(array('success' => $this->BatteriesService->TrackChargeCycle($args['batteryId'], $trackedTime)));
}
public function BatteryDetails($request, $response, $args)
public function BatteryDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiEncode($this->BatteriesService->GetBatteryDetails($args['batteryId']));
return $this->ApiResponse($this->BatteriesService->GetBatteryDetails($args['batteryId']));
}
}

View File

@@ -2,7 +2,7 @@
namespace Grocy\Controllers;
use Grocy\Services\BatteriesService;
use \Grocy\Services\BatteriesService;
class BatteriesController extends BaseController
{
@@ -14,49 +14,46 @@ class BatteriesController extends BaseController
protected $BatteriesService;
public function Overview($request, $response, $args)
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$nextChargeTimes = array();
foreach($this->Database->batteries() as $battery)
{
$nextChargeTimes[$battery->id] = $this->BatteriesService->GetNextChargeTime($battery->id);
}
return $this->AppContainer->view->render($response, 'batteriesoverview', [
'title' => 'Batteries overview',
'contentPage' => 'batteriesoverview.php',
'batteries' => $this->Database->batteries(),
'current' => $this->BatteriesService->GetCurrent(),
'nextChargeTimes' => $nextChargeTimes
]);
}
public function TrackChargeCycle($request, $response, $args)
public function TrackChargeCycle(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'batterytracking', [
'title' => 'Battery tracking',
'contentPage' => 'batterytracking.php',
'batteries' => $this->Database->batteries()
]);
}
public function BatteriesList($request, $response, $args)
public function BatteriesList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'batteries', [
'title' => 'Batteries',
'contentPage' => 'batteries.php',
'batteries' => $this->Database->batteries()
]);
}
public function BatteryEditForm($request, $response, $args)
public function BatteryEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($args['batteryId'] == 'new')
{
return $this->AppContainer->view->render($response, 'batteryform', [
'title' => 'Create battery',
'contentPage' => 'batteryform.php',
'mode' => 'create'
]);
}
else
{
return $this->AppContainer->view->render($response, 'batteryform', [
'title' => 'Edit battery',
'contentPage' => 'batteryform.php',
'battery' => $this->Database->batteries($args['batteryId']),
'mode' => 'edit'
]);

View File

@@ -2,12 +2,12 @@
namespace Grocy\Controllers;
use Grocy\Services\ApplicationService;
use Grocy\Services\DatabaseMigrationService;
use \Grocy\Services\ApplicationService;
use \Grocy\Services\DatabaseMigrationService;
class CliController extends BaseController
{
public function RecreateDemo($request, $response, $args)
public function RecreateDemo(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$applicationService = new ApplicationService();
if ($applicationService->IsDemoInstallation())

View File

@@ -4,37 +4,37 @@ namespace Grocy\Controllers;
class GenericEntityApiController extends BaseApiController
{
public function GetObjects($request, $response, $args)
public function GetObjects(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiEncode($this->Database->{$args['entity']}());
return $this->ApiResponse($this->Database->{$args['entity']}());
}
public function GetObject($request, $response, $args)
public function GetObject(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiEncode($this->Database->{$args['entity']}($args['objectId']));
return $this->ApiResponse($this->Database->{$args['entity']}($args['objectId']));
}
public function AddObject($request, $response, $args)
public function AddObject(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$newRow = $this->Database->{$args['entity']}()->createRow($request->getParsedBody());
$newRow->save();
$success = $newRow->isClean();
return $this->ApiEncode(array('success' => $success));
return $this->ApiResponse(array('success' => $success));
}
public function EditObject($request, $response, $args)
public function EditObject(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$row = $this->Database->{$args['entity']}($args['objectId']);
$row->update($request->getParsedBody());
$success = $row->isClean();
return $this->ApiEncode(array('success' => $success));
return $this->ApiResponse(array('success' => $success));
}
public function DeleteObject($request, $response, $args)
public function DeleteObject(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$row = $this->Database->{$args['entity']}($args['objectId']);
$row->delete();
$success = $row->isClean();
return $this->ApiEncode(array('success' => $success));
return $this->ApiResponse(array('success' => $success));
}
}

View File

@@ -2,7 +2,7 @@
namespace Grocy\Controllers;
use Grocy\Services\HabitsService;
use \Grocy\Services\HabitsService;
class HabitsApiController extends BaseApiController
{
@@ -14,7 +14,7 @@ class HabitsApiController extends BaseApiController
protected $HabitsService;
public function TrackHabitExecution($request, $response, $args)
public function TrackHabitExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$trackedTime = date('Y-m-d H:i:s');
if (isset($request->getQueryParams()['tracked_time']) && !empty($request->getQueryParams()['tracked_time']))
@@ -22,11 +22,11 @@ class HabitsApiController extends BaseApiController
$trackedTime = $request->getQueryParams()['tracked_time'];
}
return $this->ApiEncode(array('success' => $this->HabitsService->TrackHabit($args['habitId'], $trackedTime)));
return $this->ApiResponse(array('success' => $this->HabitsService->TrackHabit($args['habitId'], $trackedTime)));
}
public function HabitDetails($request, $response, $args)
public function HabitDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiEncode($this->HabitsService->GetHabitDetails($args['habitId']));
return $this->ApiResponse($this->HabitsService->GetHabitDetails($args['habitId']));
}
}

View File

@@ -2,7 +2,7 @@
namespace Grocy\Controllers;
use Grocy\Services\HabitsService;
use \Grocy\Services\HabitsService;
class HabitsController extends BaseController
{
@@ -14,52 +14,49 @@ class HabitsController extends BaseController
protected $HabitsService;
public function Overview($request, $response, $args)
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$nextHabitTimes = array();
foreach($this->Database->habits() as $habit)
{
$nextHabitTimes[$habit->id] = $this->HabitsService->GetNextHabitTime($habit->id);
}
return $this->AppContainer->view->render($response, 'habitsoverview', [
'title' => 'Habits overview',
'contentPage' => 'habitsoverview.php',
'habits' => $this->Database->habits(),
'currentHabits' => $this->HabitsService->GetCurrentHabits(),
'nextHabitTimes' => $nextHabitTimes
]);
}
public function TrackHabitExecution($request, $response, $args)
public function TrackHabitExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'habittracking', [
'title' => 'Habit tracking',
'contentPage' => 'habittracking.php',
'habits' => $this->Database->habits()
]);
}
public function HabitsList($request, $response, $args)
public function HabitsList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'habits', [
'title' => 'Habits',
'contentPage' => 'habits.php',
'habits' => $this->Database->habits()
]);
}
public function HabitEditForm($request, $response, $args)
public function HabitEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($args['habitId'] == 'new')
{
return $this->AppContainer->view->render($response, 'habitform', [
'title' => 'Create habit',
'contentPage' => 'habitform.php',
'periodTypes' => GetClassConstants('Grocy\Services\HabitsService'),
'periodTypes' => GetClassConstants('\Grocy\Services\HabitsService'),
'mode' => 'create'
]);
}
else
{
return $this->AppContainer->view->render($response, 'habitform', [
'title' => 'Edit habit',
'contentPage' => 'habitform.php',
'habit' => $this->Database->habits($args['habitId']),
'periodTypes' => GetClassConstants('Grocy\Services\HabitsService'),
'periodTypes' => GetClassConstants('\Grocy\Services\HabitsService'),
'mode' => 'edit'
]);
}

View File

@@ -2,11 +2,10 @@
namespace Grocy\Controllers;
use Grocy\Services\SessionService;
use Grocy\Services\DatabaseService;
use Grocy\Services\ApplicationService;
use Grocy\Services\DatabaseMigrationService;
use Grocy\Services\DemoDataGeneratorService;
use \Grocy\Services\SessionService;
use \Grocy\Services\ApplicationService;
use \Grocy\Services\DatabaseMigrationService;
use \Grocy\Services\DemoDataGeneratorService;
class LoginController extends BaseController
{
@@ -18,7 +17,7 @@ class LoginController extends BaseController
protected $SessionService;
public function ProcessLogin($request, $response, $args)
public function ProcessLogin(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$postParams = $request->getParsedBody();
if (isset($postParams['username']) && isset($postParams['password']))
@@ -41,21 +40,18 @@ class LoginController extends BaseController
}
}
public function LoginPage($request, $response, $args)
public function LoginPage(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'login', [
'title' => 'Login',
'contentPage' => 'login.php'
]);
return $this->AppContainer->view->render($response, 'login');
}
public function Logout($request, $response, $args)
public function Logout(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$this->SessionService->RemoveSession($_COOKIE['grocy_session']);
return $response->withRedirect('/');
}
public function Root($request, $response, $args)
public function Root(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
// Schema migration is done here
$databaseMigrationService = new DatabaseMigrationService();

View File

@@ -2,7 +2,7 @@
namespace Grocy\Controllers;
use Grocy\Services\StockService;
use \Grocy\Services\StockService;
class StockApiController extends BaseApiController
{
@@ -14,12 +14,12 @@ class StockApiController extends BaseApiController
protected $StockService;
public function ProductDetails($request, $response, $args)
public function ProductDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiEncode($this->StockService->GetProductDetails($args['productId']));
return $this->ApiResponse($this->StockService->GetProductDetails($args['productId']));
}
public function AddProduct($request, $response, $args)
public function AddProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$bestBeforeDate = date('Y-m-d');
if (isset($request->getQueryParams()['bestbeforedate']) && !empty($request->getQueryParams()['bestbeforedate']))
@@ -33,10 +33,10 @@ class StockApiController extends BaseApiController
$transactionType = $request->getQueryParams()['transactiontype'];
}
return $this->ApiEncode(array('success' => $this->StockService->AddProduct($args['productId'], $args['amount'], $bestBeforeDate, $transactionType)));
return $this->ApiResponse(array('success' => $this->StockService->AddProduct($args['productId'], $args['amount'], $bestBeforeDate, $transactionType)));
}
public function ConsumeProduct($request, $response, $args)
public function ConsumeProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$spoiled = false;
if (isset($request->getQueryParams()['spoiled']) && !empty($request->getQueryParams()['spoiled']) && $request->getQueryParams()['spoiled'] == '1')
@@ -50,10 +50,10 @@ class StockApiController extends BaseApiController
$transactionType = $request->getQueryParams()['transactiontype'];
}
return $this->ApiEncode(array('success' => $this->StockService->ConsumeProduct($args['productId'], $args['amount'], $spoiled, $transactionType)));
return $this->ApiResponse(array('success' => $this->StockService->ConsumeProduct($args['productId'], $args['amount'], $spoiled, $transactionType)));
}
public function InventoryProduct($request, $response, $args)
public function InventoryProduct(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$bestBeforeDate = date('Y-m-d');
if (isset($request->getQueryParams()['bestbeforedate']) && !empty($request->getQueryParams()['bestbeforedate']))
@@ -61,17 +61,17 @@ class StockApiController extends BaseApiController
$bestBeforeDate = $request->getQueryParams()['bestbeforedate'];
}
return $this->ApiEncode(array('success' => $this->StockService->InventoryProduct($args['productId'], $args['newAmount'], $bestBeforeDate)));
return $this->ApiResponse(array('success' => $this->StockService->InventoryProduct($args['productId'], $args['newAmount'], $bestBeforeDate)));
}
public function CurrentStock($request, $response, $args)
public function CurrentStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->ApiEncode($this->StockService->GetCurrentStock());
return $this->ApiResponse($this->StockService->GetCurrentStock());
}
public function AddmissingProductsToShoppingList($request, $response, $args)
public function AddmissingProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
$this->StockService->AddMissingProductsToShoppingList();
return $this->ApiEncode(array('success' => true));
return $this->ApiResponse(array('success' => true));
}
}

View File

@@ -2,7 +2,7 @@
namespace Grocy\Controllers;
use Grocy\Services\StockService;
use \Grocy\Services\StockService;
class StockController extends BaseController
{
@@ -15,11 +15,9 @@ class StockController extends BaseController
protected $StockService;
public function Overview($request, $response, $args)
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'stockoverview', [
'title' => 'Stock overview',
'contentPage' => 'stockoverview.php',
'products' => $this->Database->products(),
'quantityunits' => $this->Database->quantity_units(),
'currentStock' => $this->StockService->GetCurrentStock(),
@@ -27,38 +25,30 @@ class StockController extends BaseController
]);
}
public function Purchase($request, $response, $args)
public function Purchase(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'purchase', [
'title' => 'Purchase',
'contentPage' => 'purchase.php',
'products' => $this->Database->products()
]);
}
public function Consume($request, $response, $args)
public function Consume(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'consume', [
'title' => 'Consume',
'contentPage' => 'consume.php',
'products' => $this->Database->products()
]);
}
public function Inventory($request, $response, $args)
public function Inventory(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'inventory', [
'title' => 'Inventory',
'contentPage' => 'inventory.php',
'products' => $this->Database->products()
]);
}
public function ShoppingList($request, $response, $args)
public function ShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'shoppinglist', [
'title' => 'Shopping list',
'contentPage' => 'shoppinglist.php',
'listItems' => $this->Database->shopping_list(),
'products' => $this->Database->products(),
'quantityunits' => $this->Database->quantity_units(),
@@ -66,42 +56,34 @@ class StockController extends BaseController
]);
}
public function ProductsList($request, $response, $args)
public function ProductsList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'products', [
'title' => 'Products',
'contentPage' => 'products.php',
'products' => $this->Database->products(),
'locations' => $this->Database->locations(),
'quantityunits' => $this->Database->quantity_units()
]);
}
public function LocationsList($request, $response, $args)
public function LocationsList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'locations', [
'title' => 'Locations',
'contentPage' => 'locations.php',
'locations' => $this->Database->locations()
]);
}
public function QuantityUnitsList($request, $response, $args)
public function QuantityUnitsList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
return $this->AppContainer->view->render($response, 'quantityunits', [
'title' => 'Quantity units',
'contentPage' => 'quantityunits.php',
'quantityunits' => $this->Database->quantity_units()
]);
}
public function ProductEditForm($request, $response, $args)
public function ProductEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($args['productId'] == 'new')
{
return $this->AppContainer->view->render($response, 'productform', [
'title' => 'Create product',
'contentPage' => 'productform.php',
'locations' => $this->Database->locations(),
'quantityunits' => $this->Database->quantity_units(),
'mode' => 'create'
@@ -110,8 +92,6 @@ class StockController extends BaseController
else
{
return $this->AppContainer->view->render($response, 'productform', [
'title' => 'Edit product',
'contentPage' => 'productform.php',
'product' => $this->Database->products($args['productId']),
'locations' => $this->Database->locations(),
'quantityunits' => $this->Database->quantity_units(),
@@ -120,55 +100,45 @@ class StockController extends BaseController
}
}
public function LocationEditForm($request, $response, $args)
public function LocationEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($args['locationId'] == 'new')
{
return $this->AppContainer->view->render($response, 'locationform', [
'title' => 'Create location',
'contentPage' => 'locationform.php',
'mode' => 'create'
]);
}
else
{
return $this->AppContainer->view->render($response, 'locationform', [
'title' => 'Edit location',
'contentPage' => 'locationform.php',
'location' => $this->Database->locations($args['locationId']),
'mode' => 'edit'
]);
}
}
public function QuantityUnitEditForm($request, $response, $args)
public function QuantityUnitEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($args['quantityunitId'] == 'new')
{
return $this->AppContainer->view->render($response, 'quantityunitform', [
'title' => 'Create quantity unit',
'contentPage' => 'quantityunitform.php',
'mode' => 'create'
]);
}
else
{
return $this->AppContainer->view->render($response, 'quantityunitform', [
'title' => 'Edit quantity unit',
'contentPage' => 'quantityunitform.php',
'quantityunit' => $this->Database->quantity_units($args['quantityunitId']),
'mode' => 'edit'
]);
}
}
public function ShoppingListItemEditForm($request, $response, $args)
public function ShoppingListItemEditForm(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{
if ($args['itemId'] == 'new')
{
return $this->AppContainer->view->render($response, 'shoppinglistform', [
'title' => 'Add shopping list item',
'contentPage' => 'shoppinglistform.php',
'products' => $this->Database->products(),
'mode' => 'create'
]);
@@ -176,8 +146,6 @@ class StockController extends BaseController
else
{
return $this->AppContainer->view->render($response, 'shoppinglistform', [
'title' => 'Edit shopping list item',
'contentPage' => 'shoppinglistform.php',
'listItem' => $this->Database->shopping_list($args['itemId']),
'products' => $this->Database->products(),
'mode' => 'edit'