Skip to content

Commit

Permalink
#205 Add/Edit/Delete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbrendel committed May 15, 2024
1 parent 202dd7c commit 4d5bd92
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@
array('/api/plants/remove', 'ANY', 'api@remove_plant'),
array('/api/plants/list', 'ANY', 'api@get_plant_list'),
array('/api/plants/search', 'ANY', 'api@search_plants'),
array('/api/plants/attributes/add', 'ANY', 'api@add_attribute'),
array('/api/plants/attributes/edit', 'ANY', 'api@edit_attribute'),
array('/api/plants/attributes/remove', 'ANY', 'api@remove_attribute'),

/** Backup Controller */
array('/export/start', 'POST', 'backup@export'),
Expand Down
88 changes: 86 additions & 2 deletions app/controller/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class ApiController extends BaseController {
public function __construct()
{
parent::__construct();
//parent::__construct();

$token = null;
if (isset($_GET['token'])) {
Expand All @@ -30,10 +30,16 @@ public function get_plant($request)
$plantId = $request->params()->query('plant', null);

$plant = PlantsModel::getDetails($plantId);
$cust_attr = CustPlantAttrModel::getForPlant($plantId);

$data = [
'default' => $plant?->asArray(),
'custom' => $cust_attr
];

return json([
'code' => 200,
'data' => $plant?->asArray()
'data' => $data
]);
} catch (\Exception $e) {
return json([
Expand Down Expand Up @@ -149,4 +155,82 @@ public function search_plants($request)
]);
}
}

/**
* Handles URL: /api/plants/attributes/add
*
* @param Asatru\Controller\ControllerArg $request
* @return Asatru\View\JsonHandler
*/
public static function add_attribute($request)
{
try {
$plant = $request->params()->query('plant', null);
$label = $request->params()->query('label', null);
$datatype = $request->params()->query('datatype', null);
$content = $request->params()->query('content', null);

CustPlantAttrModel::addAttribute($plant, $label, $datatype, $content, true);

return json([
'code' => 200
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}

/**
* Handles URL: /api/plants/attributes/edit
*
* @param Asatru\Controller\ControllerArg $request
* @return Asatru\View\JsonHandler
*/
public static function edit_attribute($request)
{
try {
$attribute = $request->params()->query('attribute', null);
$label = $request->params()->query('label', null);
$datatype = $request->params()->query('datatype', null);
$content = $request->params()->query('content', null);

CustPlantAttrModel::editAttribute($attribute, null, $label, $datatype, $content, true);

return json([
'code' => 200
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}

/**
* Handles URL: /api/plants/attributes/remove
*
* @param Asatru\Controller\ControllerArg $request
* @return Asatru\View\JsonHandler
*/
public static function remove_attribute($request)
{
try {
$attribute = $request->params()->query('attribute', null);

CustPlantAttrModel::removeAttribute($attribute, true);

return json([
'code' => 200
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
}
49 changes: 38 additions & 11 deletions app/models/CustPlantAttrModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,27 @@ public static function interpretContent($content, $datatype)
* @param $label
* @param $datatype
* @param $content
* @param $api
* @return void
* @throws \Exception
*/
public static function addAttribute($plant, $label, $datatype, $content)
public static function addAttribute($plant, $label, $datatype, $content, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
if (!$api) {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
}
}

static::raw('INSERT INTO `' . self::tableName() . '` (plant, label, datatype, content) VALUES(?, ?, ?, ?)', [
$plant, $label, $datatype, static::interpretContent($content, $datatype)
]);

LogModel::addLog($user->get('id'), $plant, $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
if (!$api) {
LogModel::addLog($user->get('id'), $plant, '[add] ' . $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
}
} catch (\Exception $e) {
throw $e;
}
Expand All @@ -103,36 +108,58 @@ public static function addAttribute($plant, $label, $datatype, $content)
* @param $label
* @param $datatype
* @param $content
* @param $api
* @return void
* @throws \Exception
*/
public static function editAttribute($id, $plant, $label, $datatype, $content)
public static function editAttribute($id, $plant, $label, $datatype, $content, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
if (!$api) {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
}
}

static::raw('UPDATE `' . self::tableName() . '` SET label = ?, datatype = ?, content = ? WHERE id = ?', [
$label, $datatype, static::interpretContent($content, $datatype), $id
]);

LogModel::addLog($user->get('id'), $plant, $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
if (!$api) {
LogModel::addLog($user->get('id'), $plant, '[edit] ' . $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
}
} catch (\Exception $e) {
throw $e;
}
}

/**
* @param $id
* @param $api
* @return void
* @throws \Exception
*/
public static function removeAttribute($id)
public static function removeAttribute($id, $api = false)
{
try {
if (!$api) {
$user = UserModel::getAuthUser();
if (!$user) {
throw new \Exception('Invalid user');
}
}

$item = static::raw('SELECT * FROM `' . self::tableName() . '` WHERE id = ?', [$id])->first();
if (!$item) {
throw new \Exception('Custom plant attribute not found: ' . $id);
}

static::raw('DELETE FROM `' . self::tableName() . '` WHERE id = ?', [$id]);

if (!$api) {
LogModel::addLog($user->get('id'), $item->get('plant'), '[remove] ' . $item->get('label') . ' (' . $item->get('datatype') . ')', $item->get('content'), url('/plants/details/' . $item->get('plant')));
}
} catch (\Exception $e) {
throw $e;
}
Expand Down

0 comments on commit 4d5bd92

Please sign in to comment.