diff --git a/app/config/routes.php b/app/config/routes.php index 98c1630..ebc94c3 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -179,6 +179,7 @@ return [ array('/api/inventory/fetch', 'ANY', 'api@fetch_inventory'), array('/api/inventory/add', 'ANY', 'api@add_inventory_item'), array('/api/inventory/edit', 'ANY', 'api@edit_inventory_item'), + array('/api/inventory/amount/inc', 'ANY', 'api@inc_inventory_item'), array('/api/chat/message/add', 'ANY', 'api@add_chat_message'), /** Backup Controller */ diff --git a/app/controller/api.php b/app/controller/api.php index 9a65d38..fecdb56 100644 --- a/app/controller/api.php +++ b/app/controller/api.php @@ -617,6 +617,31 @@ class ApiController extends BaseController { } } + /** + * Handles URL: /api/inventory/amount/inc + * + * @param Asatru\Controller\ControllerArg $request + * @return Asatru\View\JsonHandler + */ + public static function inc_inventory_item($request) + { + try { + $item = $request->params()->query('item', null); + + $amount = InventoryModel::incAmount($item, true); + + return json([ + 'code' => 200, + 'amount' => $amount + ]); + } catch (\Exception $e) { + return json([ + 'code' => 500, + 'msg' => $e->getMessage() + ]); + } + } + /** * Handles URL: /api/chat/message/add * diff --git a/app/models/InventoryModel.php b/app/models/InventoryModel.php index 0726fd4..62bdd03 100644 --- a/app/models/InventoryModel.php +++ b/app/models/InventoryModel.php @@ -145,14 +145,15 @@ class InventoryModel extends \Asatru\Database\Model { /** * @param $id + * @param $api * @return int * @throws \Exception */ - public static function incAmount($id) + public static function incAmount($id, $api = false) { try { $user = UserModel::getAuthUser(); - if (!$user) { + if ((!$user) && (!$api)) { throw new \Exception('Invalid user'); } @@ -164,10 +165,12 @@ class InventoryModel extends \Asatru\Database\Model { $amount = $row->get('amount') + 1; static::raw('UPDATE `' . self::tableName() . '` SET amount = ?, last_edited_user = ?, last_edited_date = CURRENT_TIMESTAMP WHERE id = ?', [ - $amount, $user->get('id'), $row->get('id') + $amount, (($user) ? $user->get('id') : 0), $row->get('id') ]); - LogModel::addLog($user->get('id'), 'inventory', 'increment_inventory_item', $row->get('name'), url('/inventory?expand=' . $row->get('id') . '#anchor-item-' . $row->get('id'))); + if (!$api) { + LogModel::addLog($user->get('id'), 'inventory', 'increment_inventory_item', $row->get('name'), url('/inventory?expand=' . $row->get('id') . '#anchor-item-' . $row->get('id'))); + } return $amount; } catch (\Exception $e) {