#276 Increment amount

This commit is contained in:
Daniel Brendel
2024-10-03 12:45:31 +02:00
parent c5d443abf4
commit 3ac403b5bc
3 changed files with 33 additions and 4 deletions

View File

@@ -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 */

View File

@@ -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
*

View File

@@ -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) {