From 1c5dbd306ec83270dff53ce565ccf64bfa54eb98 Mon Sep 17 00:00:00 2001 From: Daniel Brendel Date: Mon, 30 Sep 2024 22:40:53 +0200 Subject: [PATCH] Resolves #279 --- app/config/routes.php | 1 + app/controller/api.php | 24 ++++++++++++++++++++++++ app/models/PlantPhotoModel.php | 13 ++++++++----- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/config/routes.php b/app/config/routes.php index 68441e6..3a32a61 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -167,6 +167,7 @@ return [ array('/api/plants/photo/update', 'ANY', 'api@update_plant_photo'), array('/api/plants/gallery/add', 'ANY', 'api@add_plant_gallery_photo'), array('/api/plants/gallery/edit', 'ANY', 'api@edit_plant_gallery_photo'), + array('/api/plants/gallery/remove', 'ANY', 'api@remove_plant_gallery_photo'), array('/api/plants/log/add', 'ANY', 'api@add_plant_log_entry'), array('/api/plants/log/edit', 'ANY', 'api@edit_plant_log_entry'), array('/api/plants/log/remove', 'ANY', 'api@remove_plant_log_entry'), diff --git a/app/controller/api.php b/app/controller/api.php index fa4bdb7..b036159 100644 --- a/app/controller/api.php +++ b/app/controller/api.php @@ -326,6 +326,30 @@ class ApiController extends BaseController { } } + /** + * Handles URL: /api/plants/gallery/remove + * + * @param Asatru\Controller\ControllerArg $request + * @return Asatru\View\JsonHandler + */ + public static function remove_plant_gallery_photo($request) + { + try { + $item = $request->params()->query('item', null); + + PlantPhotoModel::removePhoto($item, true); + + return json([ + 'code' => 200 + ]); + } catch (\Exception $e) { + return json([ + 'code' => 500, + 'msg' => $e->getMessage() + ]); + } + } + /** * Handles URL: /api/plants/log/add * diff --git a/app/models/PlantPhotoModel.php b/app/models/PlantPhotoModel.php index 6c62882..375e13d 100644 --- a/app/models/PlantPhotoModel.php +++ b/app/models/PlantPhotoModel.php @@ -97,14 +97,15 @@ class PlantPhotoModel extends \Asatru\Database\Model { /** * @param $photo + * @param $api * @return void * @throws \Exception */ - public static function removePhoto($photo) + public static function removePhoto($photo, $api = false) { try { $user = UserModel::getAuthUser(); - if (!$user) { + if ((!$user) && (!$api)) { throw new \Exception('Invalid user'); } @@ -122,10 +123,12 @@ class PlantPhotoModel extends \Asatru\Database\Model { static::raw('DELETE FROM `' . self::tableName() . '` WHERE id = ?', [$photo]); - LogModel::addLog($user->get('id'), $plant->get('name'), 'remove_gallery_photo', $photo_data->get('label'), url('/plants/details/' . $plant->get('id') . '#plant-gallery-photo-anchor')); + if (!$api) { + LogModel::addLog($user->get('id'), $plant->get('name'), 'remove_gallery_photo', $photo_data->get('label'), url('/plants/details/' . $plant->get('id') . '#plant-gallery-photo-anchor')); - if (app('system_message_plant_log')) { - PlantLogModel::addEntry($plant->get('id'), '[System] remove_gallery_photo: ' . $photo_data->get('label')); + if (app('system_message_plant_log')) { + PlantLogModel::addEntry($plant->get('id'), '[System] remove_gallery_photo: ' . $photo_data->get('label')); + } } } catch (\Exception $e) { throw $e;