#396 Handle API situation

This commit is contained in:
Daniel Brendel
2025-07-01 22:45:02 +02:00
parent 5c2e5355c8
commit 0978344df3
3 changed files with 40 additions and 19 deletions
+3 -3
View File
@@ -399,7 +399,7 @@ class ApiController extends BaseController {
$plantId = $request->params()->query('plant', null);
$content = $request->params()->query('content', null);
$logid = PlantLogModel::addEntry($plantId, $content);
$logid = PlantLogModel::addEntry($plantId, $content, true);
return json([
'code' => 200,
@@ -425,7 +425,7 @@ class ApiController extends BaseController {
$logid = $request->params()->query('logid', null);
$content = $request->params()->query('content', null);
PlantLogModel::editEntry($logid, $content);
PlantLogModel::editEntry($logid, $content, true);
return json([
'code' => 200
@@ -449,7 +449,7 @@ class ApiController extends BaseController {
try {
$logid = $request->params()->query('logid', null);
PlantLogModel::removeEntry($logid);
PlantLogModel::removeEntry($logid, true);
return json([
'code' => 200
+36 -15
View File
@@ -9,22 +9,29 @@ class PlantLogModel extends \Asatru\Database\Model {
/**
* @param $plant
* @param $content
* @param $api
* @return int
* @throws \Exception
*/
public static function addEntry($plant, $content)
public static function addEntry($plant, $content, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
$user = null;
if (!$api) {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
}
}
static::raw('INSERT INTO `@THIS` (plant, content) VALUES(?, ?)', [
$plant, $content
]);
LogModel::addLog($user->get('id'), $plant, 'add_plant_log', $content, url('/plants/details/' . $plant));
if (!$api) {
LogModel::addLog($user->get('id'), $plant, 'add_plant_log', $content, url('/plants/details/' . $plant));
}
$item = static::raw('SELECT * FROM `@THIS` ORDER BY id DESC LIMIT 1')->first();
if ($item) {
@@ -40,15 +47,20 @@ class PlantLogModel extends \Asatru\Database\Model {
/**
* @param $id
* @param $content
* @param $api
* @return void
* @throws \Exception
*/
public static function editEntry($id, $content)
public static function editEntry($id, $content, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
$user = null;
if (!$api) {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
}
}
$item = static::raw('SELECT * FROM `@THIS` WHERE id = ?', [$id])->first();
@@ -60,7 +72,9 @@ class PlantLogModel extends \Asatru\Database\Model {
$content, $item->get('id')
]);
LogModel::addLog($user->get('id'), $item->get('id'), 'edit_plant_log', $content, url('/plants/details/' . $item->get('plant')));
if (!$api) {
LogModel::addLog($user->get('id'), $item->get('id'), 'edit_plant_log', $content, url('/plants/details/' . $item->get('plant')));
}
} catch (\Exception $e) {
throw $e;
}
@@ -68,15 +82,20 @@ class PlantLogModel extends \Asatru\Database\Model {
/**
* @param $id
* @param $api
* @return void
* @throws \Exception
*/
public static function removeEntry($id)
public static function removeEntry($id, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
$user = null;
if (!$api) {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
}
}
$item = static::raw('SELECT * FROM `@THIS` WHERE id = ?', [$id])->first();
@@ -88,7 +107,9 @@ class PlantLogModel extends \Asatru\Database\Model {
$item->get('id')
]);
LogModel::addLog($user->get('id'), $item->get('id'), 'remove_plant_log', '', url('/plants/details/' . $item->get('plant')));
if (!$api) {
LogModel::addLog($user->get('id'), $item->get('id'), 'remove_plant_log', '', url('/plants/details/' . $item->get('plant')));
}
} catch (\Exception $e) {
throw $e;
}
+1 -1
View File
@@ -338,7 +338,7 @@ class PlantsModel extends \Asatru\Database\Model {
}
if (app('system_message_plant_log')) {
PlantLogModel::addEntry($plantId, '[System] ' . $attribute . ' = ' . $value);
PlantLogModel::addEntry($plantId, '[System] ' . $attribute . ' = ' . $value, true);
}
} catch (\Exception $e) {
throw $e;