Optimize and refactor latest changes

This commit is contained in:
Bernd Bestel
2018-07-25 19:28:15 +02:00
parent 4685ff4145
commit 42c1709633
33 changed files with 315 additions and 254 deletions

View File

@@ -39,6 +39,7 @@ class ApiKeyService extends BaseService
$apiKeyRow = $this->Database->api_keys()->createRow(array(
'api_key' => $newApiKey,
'user_id' => GROCY_USER_ID,
'expires' => '2999-12-31 23:59:59' // Default is that API keys expire never
));
$apiKeyRow->save();
@@ -57,6 +58,16 @@ class ApiKeyService extends BaseService
return $apiKey->id;
}
public function GetUserByApiKey($apiKey)
{
$apiKeyRow = $this->Database->api_keys()->where('api_key', $apiKey)->fetch();
if ($apiKeyRow !== null)
{
return $this->Database->users($apiKeyRow->user_id);
}
return null;
}
private function GenerateApiKey()
{
return RandomString(50);

View File

@@ -4,22 +4,6 @@ namespace Grocy\Services;
class ApplicationService extends BaseService
{
/**
* @return boolean
*/
public function IsDemoInstallation()
{
return file_exists(GROCY_DATAPATH . '/demo.txt');
}
/**
* @return boolean
*/
public function IsEmbeddedInstallation()
{
return file_exists(__DIR__ . '/../embedded.txt');
}
private $InstalledVersion;
public function GetInstalledVersion()
{

View File

@@ -16,7 +16,10 @@ class DemoDataGeneratorService extends BaseService
$loremIpsum = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
$sql = "
INSERT INTO users (id, username, password) VALUES (-1, '{$localizationService->Localize('Demo User')}', 'x');
UPDATE users SET username = '{$localizationService->Localize('Demo User')}' WHERE id = 1;
INSERT INTO users (username, password) VALUES ('{$localizationService->Localize('Demo User')} 2', 'x');
INSERT INTO users (username, password) VALUES ('{$localizationService->Localize('Demo User')} 3', 'x');
INSERT INTO users (username, password) VALUES ('{$localizationService->Localize('Demo User')} 4', 'x');
INSERT INTO locations (name) VALUES ('{$localizationService->Localize('Pantry')}'); --2
INSERT INTO locations (name) VALUES ('{$localizationService->Localize('Candy cupboard')}'); --3

View File

@@ -44,11 +44,20 @@ class HabitsService extends BaseService
$habit = $this->Database->habits($habitId);
$habitTrackedCount = $this->Database->habits_log()->where('habit_id', $habitId)->count();
$habitLastTrackedTime = $this->Database->habits_log()->where('habit_id', $habitId)->max('tracked_time');
$doneByUserId = $this->Database->habits_log()->where('habit_id = :1 AND tracked_time = :2', $habitId, $habitLastTrackedTime)->fetch()->done_by_user_id;
if ($doneByUserId !== null && !empty($doneByUserId))
{
$usersService = new UsersService();
$users = $usersService->GetUsersAsDto();
$lastDoneByUser = FindObjectInArrayByPropertyValue($users, 'id', $doneByUserId);
}
return array(
'habit' => $habit,
'last_tracked' => $habitLastTrackedTime,
'tracked_count' => $habitTrackedCount
'tracked_count' => $habitTrackedCount,
'last_done_by' => $lastDoneByUser
);
}

View File

@@ -62,6 +62,11 @@ class SessionService extends BaseService
return null;
}
public function GetDefaultUser()
{
return $this->Database->users(1);
}
private function GenerateSessionKey()
{
return RandomString(50);

View File

@@ -33,10 +33,21 @@ class UsersService extends BaseService
public function DeleteUser($userId)
{
$row = $this->Database->users($args['userId']);
$row = $this->Database->users($userId);
$row->delete();
$success = $row->isClean();
return $this->ApiResponse(array('success' => $success));
}
public function GetUsersAsDto()
{
$users = $this->Database->users();
$returnUsers = array();
foreach ($users as $user)
{
unset($user->password);
$user->display_name = GetUserDisplayName($user);
$returnUsers[] = $user;
}
return $returnUsers;
}
private function UserExists($userId)