Validate all API request as the API is now open for third parties (references #5)

This commit is contained in:
Bernd Bestel
2018-04-22 14:25:08 +02:00
parent 538d789366
commit 4853174d03
12 changed files with 389 additions and 36 deletions

View File

@@ -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;
}
}