mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
Validate all API request as the API is now open for third parties (references #5)
This commit is contained in:
@@ -15,6 +15,11 @@ class HabitsService extends BaseService
|
||||
|
||||
public function GetNextHabitTime(int $habitId)
|
||||
{
|
||||
if (!$this->HabitExists($habitId))
|
||||
{
|
||||
throw new \Exception('Habit does not exist');
|
||||
}
|
||||
|
||||
$habit = $this->Database->habits($habitId);
|
||||
$habitLastLogRow = $this->DatabaseService->ExecuteDbQuery("SELECT * from habits_current WHERE habit_id = $habitId LIMIT 1")->fetch(\PDO::FETCH_OBJ);
|
||||
|
||||
@@ -31,6 +36,11 @@ class HabitsService extends BaseService
|
||||
|
||||
public function GetHabitDetails(int $habitId)
|
||||
{
|
||||
if (!$this->HabitExists($habitId))
|
||||
{
|
||||
throw new \Exception('Habit does not exist');
|
||||
}
|
||||
|
||||
$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');
|
||||
@@ -44,6 +54,11 @@ class HabitsService extends BaseService
|
||||
|
||||
public function TrackHabit(int $habitId, string $trackedTime)
|
||||
{
|
||||
if (!$this->HabitExists($habitId))
|
||||
{
|
||||
throw new \Exception('Habit does not exist');
|
||||
}
|
||||
|
||||
$logRow = $this->Database->habits_log()->createRow(array(
|
||||
'habit_id' => $habitId,
|
||||
'tracked_time' => $trackedTime
|
||||
@@ -52,4 +67,10 @@ class HabitsService extends BaseService
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function HabitExists($habitId)
|
||||
{
|
||||
$habitRow = $this->Database->habits()->where('id = :1', $habitId)->fetch();
|
||||
return $habitRow !== null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user