Clean up controllers

This commit is contained in:
Aaron Moses 2019-12-22 22:14:05 +00:00
parent 1ec2b26d4b
commit 1cdef09832
16 changed files with 190 additions and 125 deletions

View File

@ -10,12 +10,29 @@ class CalendarApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->CalendarService = new CalendarService();
$this->ApiKeyService = new ApiKeyService();
} }
protected $CalendarService; protected $CalendarService = null;
protected $ApiKeyService;
protected function getCalendarService()
{
if($this->CalendarService == null)
{
$this->CalendarService = new CalendarService();
}
return $this->CalendarService;
}
protected $ApiKeyService = null;
protected function getApiKeyService()
{
if($this->ApiKeyService == null)
{
$this->ApiKeyService = new ApiKeyService();
}
return $this->ApiKeyService;
}
public function Ical(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Ical(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -23,7 +40,7 @@ class CalendarApiController extends BaseApiController
{ {
$vCalendar = new \Eluceo\iCal\Component\Calendar('grocy'); $vCalendar = new \Eluceo\iCal\Component\Calendar('grocy');
$events = $this->CalendarService->GetEvents(); $events = $this->getCalendarService()->GetEvents();
foreach($events as $event) foreach($events as $event)
{ {
$date = new \DateTime($event['start']); $date = new \DateTime($event['start']);
@ -60,7 +77,7 @@ class CalendarApiController extends BaseApiController
try try
{ {
return $this->ApiResponse(array( return $this->ApiResponse(array(
'url' => $this->AppContainer->UrlManager->ConstructUrl('/api/calendar/ical?secret=' . $this->ApiKeyService->GetOrCreateApiKey(ApiKeyService::API_KEY_TYPE_SPECIAL_PURPOSE_CALENDAR_ICAL)) 'url' => $this->AppContainer->UrlManager->ConstructUrl('/api/calendar/ical?secret=' . $this->getApiKeyService()->GetOrCreateApiKey(ApiKeyService::API_KEY_TYPE_SPECIAL_PURPOSE_CALENDAR_ICAL))
)); ));
} }
catch (\Exception $ex) catch (\Exception $ex)

View File

@ -9,15 +9,23 @@ class CalendarController extends BaseController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->CalendarService = new CalendarService();
} }
protected $CalendarService; protected $CalendarService = null;
protected function getCalendarService()
{
if($this->CalendarService == null)
{
$this->CalendarService = new CalendarService();
}
return $this->CalendarService;
}
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->renderPage($response, 'calendar', [ return $this->renderPage($response, 'calendar', [
'fullcalendarEventSources' => $this->CalendarService->GetEvents() 'fullcalendarEventSources' => $this->getCalendarService()->GetEvents()
]); ]);
} }
} }

View File

@ -9,10 +9,18 @@ class ChoresApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->ChoresService = new ChoresService();
} }
protected $ChoresService; protected $ChoresService = null;
protected function getChoresService()
{
if($this->ChoresService == null)
{
$this->ChoresService = new ChoresService();
}
return $this->ChoresService;
}
public function TrackChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function TrackChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -32,7 +40,7 @@ class ChoresApiController extends BaseApiController
$doneBy = $requestBody['done_by']; $doneBy = $requestBody['done_by'];
} }
$choreExecutionId = $this->ChoresService->TrackChore($args['choreId'], $trackedTime, $doneBy); $choreExecutionId = $this->getChoresService()->TrackChore($args['choreId'], $trackedTime, $doneBy);
return $this->ApiResponse($this->getDatabase()->chores_log($choreExecutionId)); return $this->ApiResponse($this->getDatabase()->chores_log($choreExecutionId));
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -45,7 +53,7 @@ class ChoresApiController extends BaseApiController
{ {
try try
{ {
return $this->ApiResponse($this->ChoresService->GetChoreDetails($args['choreId'])); return $this->ApiResponse($this->getChoresService()->GetChoreDetails($args['choreId']));
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -55,14 +63,14 @@ class ChoresApiController extends BaseApiController
public function Current(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Current(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->ApiResponse($this->ChoresService->GetCurrent()); return $this->ApiResponse($this->getChoresService()->GetCurrent());
} }
public function UndoChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function UndoChoreExecution(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try
{ {
$this->ApiResponse($this->ChoresService->UndoChoreExecution($args['executionId'])); $this->ApiResponse($this->getChoresService()->UndoChoreExecution($args['executionId']));
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -88,12 +96,12 @@ class ChoresApiController extends BaseApiController
$chores = $this->getDatabase()->chores(); $chores = $this->getDatabase()->chores();
foreach ($chores as $chore) foreach ($chores as $chore)
{ {
$this->ChoresService->CalculateNextExecutionAssignment($chore->id); $this->getChoresService()->CalculateNextExecutionAssignment($chore->id);
} }
} }
else else
{ {
$this->ChoresService->CalculateNextExecutionAssignment($choreId); $this->getChoresService()->CalculateNextExecutionAssignment($choreId);
} }
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);

View File

@ -9,10 +9,18 @@ class ChoresController extends BaseController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->ChoresService = new ChoresService();
} }
protected $ChoresService; protected $ChoresService = null;
protected function getChoresService()
{
if($this->ChoresService == null)
{
$this->ChoresService = new ChoresService();
}
return $this->ChoresService;
}
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -21,7 +29,7 @@ class ChoresController extends BaseController
return $this->renderPage($response, 'choresoverview', [ return $this->renderPage($response, 'choresoverview', [
'chores' => $this->getDatabase()->chores()->orderBy('name'), 'chores' => $this->getDatabase()->chores()->orderBy('name'),
'currentChores' => $this->ChoresService->GetCurrent(), 'currentChores' => $this->getChoresService()->GetCurrent(),
'nextXDays' => $nextXDays, 'nextXDays' => $nextXDays,
'userfields' => $this->getUserfieldsService()->GetFields('chores'), 'userfields' => $this->getUserfieldsService()->GetFields('chores'),
'userfieldValues' => $this->getUserfieldsService()->GetAllValues('chores'), 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('chores'),

View File

@ -9,10 +9,18 @@ class FilesApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->FilesService = new FilesService();
} }
protected $FilesService; protected $FilesService = null;
protected function getFilesService()
{
if($this->FilesService == null)
{
$this->FilesService = new FilesService();
}
return $this->FilesService;
}
public function UploadFile(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function UploadFile(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -28,7 +36,7 @@ class FilesApiController extends BaseApiController
} }
$data = $request->getBody()->getContents(); $data = $request->getBody()->getContents();
file_put_contents($this->FilesService->GetFilePath($args['group'], $fileName), $data); file_put_contents($this->getFilesService()->GetFilePath($args['group'], $fileName), $data);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
@ -71,11 +79,11 @@ class FilesApiController extends BaseApiController
$bestFitWidth = $request->getQueryParams()['best_fit_width']; $bestFitWidth = $request->getQueryParams()['best_fit_width'];
} }
$filePath = $this->FilesService->DownscaleImage($args['group'], $fileName, $bestFitHeight, $bestFitWidth); $filePath = $this->getFilesService()->DownscaleImage($args['group'], $fileName, $bestFitHeight, $bestFitWidth);
} }
else else
{ {
$filePath = $this->FilesService->GetFilePath($args['group'], $fileName); $filePath = $this->getFilesService()->GetFilePath($args['group'], $fileName);
} }
if (file_exists($filePath)) if (file_exists($filePath))
@ -109,7 +117,7 @@ class FilesApiController extends BaseApiController
throw new \Exception('Invalid filename'); throw new \Exception('Invalid filename');
} }
$filePath = $this->FilesService->GetFilePath($args['group'], $fileName); $filePath = $this->getFilesService->GetFilePath($args['group'], $fileName);
if (file_exists($filePath)) if (file_exists($filePath))
{ {
unlink($filePath); unlink($filePath);

View File

@ -10,15 +10,8 @@ class LoginController extends BaseController
{ {
public function __construct(\Slim\Container $container, string $sessionCookieName) public function __construct(\Slim\Container $container, string $sessionCookieName)
{ {
#$fp = fopen('/config/data/sql.log', 'a');
#$time_start = microtime(true);
parent::__construct($container); parent::__construct($container);
#fwrite($fp, "£££ Login controller - parent construstor time : " . round((microtime(true) - $time_start),6) . "\n");
#$this->SessionService = SessionService::getInstance();
#fwrite($fp, "£££ Login controller - got session service instance : " . round((microtime(true) - $time_start),6) . "\n");
$this->SessionCookieName = $sessionCookieName; $this->SessionCookieName = $sessionCookieName;
#fwrite($fp, "£££ Login controller - construction time : " . round((microtime(true) - $time_start),6) . "\n");
#fclose($fp);
} }
protected $SessionService = null; protected $SessionService = null;
@ -35,9 +28,6 @@ class LoginController extends BaseController
public function ProcessLogin(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function ProcessLogin(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
#$fp = fopen('/config/data/sql.log', 'a');
#fwrite($fp, "£££ Login controller - ProcessLogin called\n");
#fclose($fp);
$postParams = $request->getParsedBody(); $postParams = $request->getParsedBody();
if (isset($postParams['username']) && isset($postParams['password'])) if (isset($postParams['username']) && isset($postParams['password']))
{ {

View File

@ -10,10 +10,18 @@ class OpenApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->ApiKeyService = new ApiKeyService();
} }
protected $ApiKeyService; protected $ApiKeyService = null;
protected function getApiKeyService()
{
if($this->ApiKeyService == null)
{
$this->ApiKeyService = new ApiKeyService();
}
return $this->ApiKeyService;
}
public function DocumentationUi(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function DocumentationUi(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -42,8 +50,8 @@ class OpenApiController extends BaseApiController
public function CreateNewApiKey(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function CreateNewApiKey(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
$newApiKey = $this->ApiKeyService->CreateApiKey(); $newApiKey = $this->getApiKeyService()->CreateApiKey();
$newApiKeyId = $this->ApiKeyService->GetApiKeyId($newApiKey); $newApiKeyId = $this->getApiKeyService()->GetApiKeyId($newApiKey);
return $response->withRedirect($this->AppContainer->UrlManager->ConstructUrl("/manageapikeys?CreatedApiKeyId=$newApiKeyId")); return $response->withRedirect($this->AppContainer->UrlManager->ConstructUrl("/manageapikeys?CreatedApiKeyId=$newApiKeyId"));
} }
} }

View File

@ -9,10 +9,18 @@ class RecipesApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->RecipesService = new RecipesService();
} }
protected $RecipesService; protected $RecipesService = null;
protected function getRecipesService()
{
if($this->RecipesService == null)
{
$this->RecipesService = new RecipesService();
}
return $this->RecipesService;
}
public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function AddNotFulfilledProductsToShoppingList(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -24,7 +32,7 @@ class RecipesApiController extends BaseApiController
$excludedProductIds = $requestBody['excludedProductIds']; $excludedProductIds = $requestBody['excludedProductIds'];
} }
$this->RecipesService->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds); $this->getRecipesService()->AddNotFulfilledProductsToShoppingList($args['recipeId'], $excludedProductIds);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
@ -32,7 +40,7 @@ class RecipesApiController extends BaseApiController
{ {
try try
{ {
$this->RecipesService->ConsumeRecipe($args['recipeId']); $this->getRecipesService()->ConsumeRecipe($args['recipeId']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -47,10 +55,10 @@ class RecipesApiController extends BaseApiController
{ {
if(!isset($args['recipeId'])) if(!isset($args['recipeId']))
{ {
return $this->ApiResponse($this->RecipesService->GetRecipesResolved()); return $this->ApiResponse($this->getRecipesService()->GetRecipesResolved());
} }
$recipeResolved = FindObjectInArrayByPropertyValue($this->RecipesService->GetRecipesResolved(), 'recipe_id', $args['recipeId']); $recipeResolved = FindObjectInArrayByPropertyValue($this->getRecipesService()->GetRecipesResolved(), 'recipe_id', $args['recipeId']);
if(!$recipeResolved) if(!$recipeResolved)
{ {
throw new \Exception('Recipe does not exist'); throw new \Exception('Recipe does not exist');

View File

@ -10,12 +10,18 @@ class RecipesController extends BaseController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->RecipesService = new RecipesService();
$this->UserfieldsService = new UserfieldsService();
} }
protected $RecipesService; protected $RecipesService = null;
protected $UserfieldsService;
protected function getRecipesService()
{
if($this->RecipesService == null)
{
$this->RecipesService = new RecipesService();
}
return $this->RecipesService;
}
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -27,7 +33,7 @@ class RecipesController extends BaseController
{ {
$recipes = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name'); $recipes = $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name');
} }
$recipesResolved = $this->RecipesService->GetRecipesResolved(); $recipesResolved = $this->getRecipesService()->GetRecipesResolved();
$selectedRecipe = null; $selectedRecipe = null;
$selectedRecipePositionsResolved = null; $selectedRecipePositionsResolved = null;
@ -69,8 +75,8 @@ class RecipesController extends BaseController
'includedRecipeIdsAbsolute' => $includedRecipeIdsAbsolute, 'includedRecipeIdsAbsolute' => $includedRecipeIdsAbsolute,
'selectedRecipeTotalCosts' => FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->costs, 'selectedRecipeTotalCosts' => FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->costs,
'selectedRecipeTotalCalories' => FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->calories, 'selectedRecipeTotalCalories' => FindObjectInArrayByPropertyValue($recipesResolved, 'recipe_id', $selectedRecipe->id)->calories,
'userfields' => $this->UserfieldsService->GetFields('recipes'), 'userfields' => $this->getUserfieldsService()->GetFields('recipes'),
'userfieldValues' => $this->UserfieldsService->GetAllValues('recipes'), 'userfieldValues' => $this->getUserfieldsService()->GetAllValues('recipes'),
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved() 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
]); ]);
} }
@ -94,11 +100,11 @@ class RecipesController extends BaseController
'mode' => 'edit', 'mode' => 'edit',
'products' => $this->getDatabase()->products(), 'products' => $this->getDatabase()->products(),
'quantityunits' => $this->getDatabase()->quantity_units(), 'quantityunits' => $this->getDatabase()->quantity_units(),
'recipePositionsResolved' => $this->RecipesService->GetRecipesPosResolved(), 'recipePositionsResolved' => $this->getRecipesService()->GetRecipesPosResolved(),
'recipesResolved' => $this->RecipesService->GetRecipesResolved(), 'recipesResolved' => $this->getRecipesService()->GetRecipesResolved(),
'recipes' => $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name'), 'recipes' => $this->getDatabase()->recipes()->where('type', RecipesService::RECIPE_TYPE_NORMAL)->orderBy('name'),
'recipeNestings' => $this->getDatabase()->recipes_nestings()->where('recipe_id', $recipeId), 'recipeNestings' => $this->getDatabase()->recipes_nestings()->where('recipe_id', $recipeId),
'userfields' => $this->UserfieldsService->GetFields('recipes'), 'userfields' => $this->getUserfieldsService()->GetFields('recipes'),
'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved() 'quantityUnitConversionsResolved' => $this->getDatabase()->quantity_unit_conversions_resolved()
]); ]);
} }
@ -157,7 +163,7 @@ class RecipesController extends BaseController
'fullcalendarEventSources' => $events, 'fullcalendarEventSources' => $events,
'recipes' => $recipes, 'recipes' => $recipes,
'internalRecipes' => $this->getDatabase()->recipes()->whereNot('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(), 'internalRecipes' => $this->getDatabase()->recipes()->whereNot('type', RecipesService::RECIPE_TYPE_NORMAL)->fetchAll(),
'recipesResolved' => $this->RecipesService->GetRecipesResolved() 'recipesResolved' => $this->getRecipesService()->GetRecipesResolved()
]); ]);
} }
} }

View File

@ -9,16 +9,24 @@ class StockApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->StockService = new StockService();
} }
protected $StockService; protected $StockService = null;
protected function getStockService()
{
if($this->StockService == null)
{
$this->StockService = new StockService();
}
return $this->StockService;
}
public function ProductDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function ProductDetails(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try
{ {
return $this->ApiResponse($this->StockService->GetProductDetails($args['productId'])); return $this->ApiResponse($this->getStockService()->GetProductDetails($args['productId']));
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -30,8 +38,8 @@ class StockApiController extends BaseApiController
{ {
try try
{ {
$productId = $this->StockService->GetProductIdFromBarcode($args['barcode']); $productId = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
return $this->ApiResponse($this->StockService->GetProductDetails($productId)); return $this->ApiResponse($this->getStockService()->GetProductDetails($productId));
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -43,7 +51,7 @@ class StockApiController extends BaseApiController
{ {
try try
{ {
return $this->ApiResponse($this->StockService->GetProductPriceHistory($args['productId'])); return $this->ApiResponse($this->getStockService()->GetProductPriceHistory($args['productId']));
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -91,7 +99,7 @@ class StockApiController extends BaseApiController
$transactionType = $requestBody['transactiontype']; $transactionType = $requestBody['transactiontype'];
} }
$bookingId = $this->StockService->AddProduct($args['productId'], $requestBody['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price, $locationId); $bookingId = $this->getStockService()->AddProduct($args['productId'], $requestBody['amount'], $bestBeforeDate, $transactionType, date('Y-m-d'), $price, $locationId);
return $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); return $this->ApiResponse($this->getDatabase()->stock_log($bookingId));
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -104,7 +112,7 @@ class StockApiController extends BaseApiController
{ {
try try
{ {
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
return $this->AddProduct($request, $response, $args); return $this->AddProduct($request, $response, $args);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -159,7 +167,7 @@ class StockApiController extends BaseApiController
$recipeId = $requestBody['recipe_id']; $recipeId = $requestBody['recipe_id'];
} }
$bookingId = $this->StockService->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId); $bookingId = $this->getStockService()->ConsumeProduct($args['productId'], $requestBody['amount'], $spoiled, $transactionType, $specificStockEntryId, $recipeId);
$result = $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); $result = $this->ApiResponse($this->getDatabase()->stock_log($bookingId));
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -175,7 +183,7 @@ class StockApiController extends BaseApiController
{ {
try try
{ {
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
return $this->ConsumeProduct($request, $response, $args); return $this->ConsumeProduct($request, $response, $args);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -218,7 +226,7 @@ class StockApiController extends BaseApiController
$price = $requestBody['price']; $price = $requestBody['price'];
} }
$bookingId = $this->StockService->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price); $bookingId = $this->getStockService()->InventoryProduct($args['productId'], $requestBody['new_amount'], $bestBeforeDate, $locationId, $price);
return $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); return $this->ApiResponse($this->getDatabase()->stock_log($bookingId));
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -231,7 +239,7 @@ class StockApiController extends BaseApiController
{ {
try try
{ {
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
return $this->InventoryProduct($request, $response, $args); return $this->InventoryProduct($request, $response, $args);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -262,7 +270,7 @@ class StockApiController extends BaseApiController
$specificStockEntryId = $requestBody['stock_entry_id']; $specificStockEntryId = $requestBody['stock_entry_id'];
} }
$bookingId = $this->StockService->OpenProduct($args['productId'], $requestBody['amount'], $specificStockEntryId); $bookingId = $this->getStockService()->OpenProduct($args['productId'], $requestBody['amount'], $specificStockEntryId);
return $this->ApiResponse($this->getDatabase()->stock_log($bookingId)); return $this->ApiResponse($this->getDatabase()->stock_log($bookingId));
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -275,7 +283,7 @@ class StockApiController extends BaseApiController
{ {
try try
{ {
$args['productId'] = $this->StockService->GetProductIdFromBarcode($args['barcode']); $args['productId'] = $this->getStockService()->GetProductIdFromBarcode($args['barcode']);
return $this->OpenProduct($request, $response, $args); return $this->OpenProduct($request, $response, $args);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -286,7 +294,7 @@ class StockApiController extends BaseApiController
public function CurrentStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function CurrentStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->ApiResponse($this->StockService->GetCurrentStock()); return $this->ApiResponse($this->getStockService()->GetCurrentStock());
} }
public function CurrentVolatilStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function CurrentVolatilStock(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
@ -297,9 +305,9 @@ class StockApiController extends BaseApiController
$nextXDays = $request->getQueryParams()['expiring_days']; $nextXDays = $request->getQueryParams()['expiring_days'];
} }
$expiringProducts = $this->StockService->GetExpiringProducts($nextXDays, true); $expiringProducts = $this->getStockService()->GetExpiringProducts($nextXDays, true);
$expiredProducts = $this->StockService->GetExpiringProducts(-1); $expiredProducts = $this->getStockService()->GetExpiringProducts(-1);
$missingProducts = $this->StockService->GetMissingProducts(); $missingProducts = $this->getStockService()->GetMissingProducts();
return $this->ApiResponse(array( return $this->ApiResponse(array(
'expiring_products' => $expiringProducts, 'expiring_products' => $expiringProducts,
'expired_products' => $expiredProducts, 'expired_products' => $expiredProducts,
@ -319,7 +327,7 @@ class StockApiController extends BaseApiController
$listId = intval($requestBody['list_id']); $listId = intval($requestBody['list_id']);
} }
$this->StockService->AddMissingProductsToShoppingList($listId); $this->getStockService()->AddMissingProductsToShoppingList($listId);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -340,7 +348,7 @@ class StockApiController extends BaseApiController
$listId = intval($requestBody['list_id']); $listId = intval($requestBody['list_id']);
} }
$this->StockService->ClearShoppingList($listId); $this->getStockService()->ClearShoppingList($listId);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -382,7 +390,7 @@ class StockApiController extends BaseApiController
throw new \Exception("No product id was supplied"); throw new \Exception("No product id was supplied");
} }
$this->StockService->AddProductToShoppingList($productId, $amount, $note, $listId); $this->getStockService()->AddProductToShoppingList($productId, $amount, $note, $listId);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -418,7 +426,7 @@ class StockApiController extends BaseApiController
throw new \Exception("No product id was supplied"); throw new \Exception("No product id was supplied");
} }
$this->StockService->RemoveProductFromShoppingList($productId, $amount, $listId); $this->getStockService()->RemoveProductFromShoppingList($productId, $amount, $listId);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -437,7 +445,7 @@ class StockApiController extends BaseApiController
$addFoundProduct = true; $addFoundProduct = true;
} }
return $this->ApiResponse($this->StockService->ExternalBarcodeLookup($args['barcode'], $addFoundProduct)); return $this->ApiResponse($this->getStockService()->ExternalBarcodeLookup($args['barcode'], $addFoundProduct));
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -460,7 +468,7 @@ class StockApiController extends BaseApiController
public function ProductStockEntries(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function ProductStockEntries(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->ApiResponse($this->StockService->GetProductStockEntries($args['productId'])); return $this->ApiResponse($this->getStockService()->GetProductStockEntries($args['productId']));
} }
public function StockBooking(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function StockBooking(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)

View File

@ -10,11 +10,7 @@ class StockController extends BaseController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
#$fp = fopen('/www/data/sql.log', 'a');
#fwrite($fp, "!!!constructing StockController\n");
#fclose($fp);
parent::__construct($container); parent::__construct($container);
#$this->StockService = new StockService();
} }
protected $StockService = null; protected $StockService = null;
@ -57,15 +53,10 @@ class StockController extends BaseController
public function Consume(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Consume(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
#$fp = fopen('/www/data/sql.log', 'a');
#fwrite($fp, "???executing consume stock");
#$time_start = microtime(true);
$result = $this->renderPage($response, 'consume', [ $result = $this->renderPage($response, 'consume', [
'products' => $this->getDatabase()->products()->orderBy('name'), 'products' => $this->getDatabase()->products()->orderBy('name'),
'recipes' => $this->getDatabase()->recipes()->orderBy('name') 'recipes' => $this->getDatabase()->recipes()->orderBy('name')
]); ]);
#fwrite($fp, "???Total execution time in seconds: " . round((microtime(true) - $time_start),6) . "\n");
#fclose($fp);
return $result; return $result;
} }

View File

@ -2,35 +2,19 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
#use \Grocy\Services\DatabaseService;
#use \Grocy\Services\ApplicationService;
class SystemApiController extends BaseApiController class SystemApiController extends BaseApiController
{ {
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
#$this->ApplicationService = ApplicationService::getInstance();
} }
#protected $DatabaseService;
#protected $ApplicationService;
public function GetDbChangedTime(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function GetDbChangedTime(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
#$fp = fopen('/config/data/sql.log', 'a');
#fwrite($fp, "---- getting db changed time ----\n");
#$time_start = microtime(true);
$response = $this->ApiResponse(array( $response = $this->ApiResponse(array(
'changed_time' => $this->getDatabaseService()->GetDbChangedTime() 'changed_time' => $this->getDatabaseService()->GetDbChangedTime()
)); ));
#fwrite($fp, "----Total execution time in seconds: " . round((microtime(true) - $time_start),4) . "\n");
#fwrite($fp, "---- time obtained ----\n");
#fclose($fp);
return $response; return $response;
#return $this->ApiResponse(array(
# 'changed_time' => $this->getDatabaseService()->GetDbChangedTime()
#));
} }
public function LogMissingLocalization(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function LogMissingLocalization(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)

View File

@ -2,18 +2,15 @@
namespace Grocy\Controllers; namespace Grocy\Controllers;
#use \Grocy\Services\ApplicationService;
use \Grocy\Services\DatabaseMigrationService; use \Grocy\Services\DatabaseMigrationService;
use \Grocy\Services\DemoDataGeneratorService; use \Grocy\Services\DemoDataGeneratorService;
class SystemController extends BaseController class SystemController extends BaseController
{ {
#protected $ApplicationService;
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
#$this->ApplicationService = ApplicationService::getInstance();
} }
public function Root(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Root(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)

View File

@ -9,14 +9,22 @@ class TasksApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->TasksService = new TasksService();
} }
protected $TasksService; protected $TasksService = null;
protected function getTasksService()
{
if($this->TasksService == null)
{
$this->TasksService = new TasksService();
}
return $this->TasksService;
}
public function Current(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Current(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
return $this->ApiResponse($this->TasksService->GetCurrent()); return $this->ApiResponse($this->getTasksService()->GetCurrent());
} }
public function MarkTaskAsCompleted(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function MarkTaskAsCompleted(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
@ -31,7 +39,7 @@ class TasksApiController extends BaseApiController
$doneTime = $requestBody['done_time']; $doneTime = $requestBody['done_time'];
} }
$this->TasksService->MarkTaskAsCompleted($args['taskId'], $doneTime); $this->getTasksService()->MarkTaskAsCompleted($args['taskId'], $doneTime);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -44,7 +52,7 @@ class TasksApiController extends BaseApiController
{ {
try try
{ {
$this->TasksService->UndoTask($args['taskId']); $this->getTasksService()->UndoTask($args['taskId']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)

View File

@ -9,10 +9,18 @@ class TasksController extends BaseController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->TasksService = new TasksService();
} }
protected $TasksService; protected $TasksService = null;
protected function getTasksService()
{
if($this->TasksService == null)
{
$this->TasksService = new TasksService();
}
return $this->TasksService;
}
public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function Overview(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
@ -22,7 +30,7 @@ class TasksController extends BaseController
} }
else else
{ {
$tasks = $this->TasksService->GetCurrent(); $tasks = $this->getTasksService()->GetCurrent();
} }
$usersService = $this->getUsersService(); $usersService = $this->getUsersService();

View File

@ -9,16 +9,24 @@ class UsersApiController extends BaseApiController
public function __construct(\Slim\Container $container) public function __construct(\Slim\Container $container)
{ {
parent::__construct($container); parent::__construct($container);
$this->UsersService = new UsersService();
} }
protected $UsersService; protected $UsersService = null;
protected function getUsersService()
{
if($this->UsersService == null)
{
$this->UsersService = new UsersService();
}
return $this->UsersService;
}
public function GetUsers(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) public function GetUsers(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args)
{ {
try try
{ {
return $this->ApiResponse($this->UsersService->GetUsersAsDto()); return $this->ApiResponse($this->getUsersService()->GetUsersAsDto());
} }
catch (\Exception $ex) catch (\Exception $ex)
{ {
@ -37,7 +45,7 @@ class UsersApiController extends BaseApiController
throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)'); throw new \Exception('Request body could not be parsed (probably invalid JSON format or missing/wrong Content-Type header)');
} }
$this->UsersService->CreateUser($requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']); $this->getUsersService()->CreateUser($requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -50,7 +58,7 @@ class UsersApiController extends BaseApiController
{ {
try try
{ {
$this->UsersService->DeleteUser($args['userId']); $this->getUsersService()->DeleteUser($args['userId']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -65,7 +73,7 @@ class UsersApiController extends BaseApiController
try try
{ {
$this->UsersService->EditUser($args['userId'], $requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']); $this->getUsersService()->EditUser($args['userId'], $requestBody['username'], $requestBody['first_name'], $requestBody['last_name'], $requestBody['password']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -78,7 +86,7 @@ class UsersApiController extends BaseApiController
{ {
try try
{ {
$value = $this->UsersService->GetUserSetting(GROCY_USER_ID, $args['settingKey']); $value = $this->getUsersService()->GetUserSetting(GROCY_USER_ID, $args['settingKey']);
return $this->ApiResponse(array('value' => $value)); return $this->ApiResponse(array('value' => $value));
} }
catch (\Exception $ex) catch (\Exception $ex)
@ -93,7 +101,7 @@ class UsersApiController extends BaseApiController
{ {
$requestBody = $request->getParsedBody(); $requestBody = $request->getParsedBody();
$value = $this->UsersService->SetUserSetting(GROCY_USER_ID, $args['settingKey'], $requestBody['value']); $value = $this->getUsersService()->SetUserSetting(GROCY_USER_ID, $args['settingKey'], $requestBody['value']);
return $this->EmptyApiResponse($response); return $this->EmptyApiResponse($response);
} }
catch (\Exception $ex) catch (\Exception $ex)