mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 04:12:59 +00:00
Validate all API request as the API is now open for third parties (references #5)
This commit is contained in:
@@ -12,6 +12,11 @@ class BatteriesService extends BaseService
|
||||
|
||||
public function GetNextChargeTime(int $batteryId)
|
||||
{
|
||||
if (!$this->BatteryExists($batteryId))
|
||||
{
|
||||
throw new \Exception('Battery does not exist');
|
||||
}
|
||||
|
||||
$battery = $this->Database->batteries($batteryId);
|
||||
$batteryLastLogRow = $this->DatabaseService->ExecuteDbQuery("SELECT * from batteries_current WHERE battery_id = $batteryId LIMIT 1")->fetch(\PDO::FETCH_OBJ);
|
||||
|
||||
@@ -29,6 +34,11 @@ class BatteriesService extends BaseService
|
||||
|
||||
public function GetBatteryDetails(int $batteryId)
|
||||
{
|
||||
if (!$this->BatteryExists($batteryId))
|
||||
{
|
||||
throw new \Exception('Battery does not exist');
|
||||
}
|
||||
|
||||
$battery = $this->Database->batteries($batteryId);
|
||||
$batteryChargeCylcesCount = $this->Database->battery_charge_cycles()->where('battery_id', $batteryId)->count();
|
||||
$batteryLastChargedTime = $this->Database->battery_charge_cycles()->where('battery_id', $batteryId)->max('tracked_time');
|
||||
@@ -42,6 +52,11 @@ class BatteriesService extends BaseService
|
||||
|
||||
public function TrackChargeCycle(int $batteryId, string $trackedTime)
|
||||
{
|
||||
if (!$this->BatteryExists($batteryId))
|
||||
{
|
||||
throw new \Exception('Battery does not exist');
|
||||
}
|
||||
|
||||
$logRow = $this->Database->battery_charge_cycles()->createRow(array(
|
||||
'battery_id' => $batteryId,
|
||||
'tracked_time' => $trackedTime
|
||||
@@ -50,4 +65,10 @@ class BatteriesService extends BaseService
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function BatteryExists($batteryId)
|
||||
{
|
||||
$batteryRow = $this->Database->batteries()->where('id = :1', $batteryId)->fetch();
|
||||
return $batteryRow !== null;
|
||||
}
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,11 @@ class StockService extends BaseService
|
||||
|
||||
public function GetProductDetails(int $productId)
|
||||
{
|
||||
if (!$this->ProductExists($productId))
|
||||
{
|
||||
throw new \Exception('Product does not exist');
|
||||
}
|
||||
|
||||
$product = $this->Database->products($productId);
|
||||
$productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount');
|
||||
$productLastPurchased = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_PURCHASE)->max('purchased_date');
|
||||
@@ -41,6 +46,11 @@ class StockService extends BaseService
|
||||
|
||||
public function AddProduct(int $productId, int $amount, string $bestBeforeDate, $transactionType)
|
||||
{
|
||||
if (!$this->ProductExists($productId))
|
||||
{
|
||||
throw new \Exception('Product does not exist');
|
||||
}
|
||||
|
||||
if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
|
||||
{
|
||||
$stockId = uniqid();
|
||||
@@ -68,12 +78,17 @@ class StockService extends BaseService
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Transaction type $transactionType is not valid (StockService.AddProduct)");
|
||||
throw new \Exception("Transaction type $transactionType is not valid (StockService.AddProduct)");
|
||||
}
|
||||
}
|
||||
|
||||
public function ConsumeProduct(int $productId, int $amount, bool $spoiled, $transactionType)
|
||||
{
|
||||
if (!$this->ProductExists($productId))
|
||||
{
|
||||
throw new \Exception('Product does not exist');
|
||||
}
|
||||
|
||||
if ($transactionType === self::TRANSACTION_TYPE_CONSUME || $transactionType === self::TRANSACTION_TYPE_PURCHASE || $transactionType === self::TRANSACTION_TYPE_INVENTORY_CORRECTION)
|
||||
{
|
||||
$productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount');
|
||||
@@ -141,6 +156,11 @@ class StockService extends BaseService
|
||||
|
||||
public function InventoryProduct(int $productId, int $newAmount, string $bestBeforeDate)
|
||||
{
|
||||
if (!$this->ProductExists($productId))
|
||||
{
|
||||
throw new \Exception('Product does not exist');
|
||||
}
|
||||
|
||||
$productStockAmount = $this->Database->stock()->where('product_id', $productId)->sum('amount');
|
||||
|
||||
if ($newAmount > $productStockAmount)
|
||||
@@ -182,4 +202,10 @@ class StockService extends BaseService
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function ProductExists($productId)
|
||||
{
|
||||
$productRow = $this->Database->products()->where('id = :1', $productId)->fetch();
|
||||
return $productRow !== null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user