#276 Add inventory items

This commit is contained in:
Daniel Brendel
2024-10-01 12:52:30 +02:00
parent 42b173b692
commit c09c8b33e4
4 changed files with 40 additions and 7 deletions

View File

@@ -172,6 +172,7 @@ return [
array('/api/plants/log/edit', 'ANY', 'api@edit_plant_log_entry'),
array('/api/plants/log/remove', 'ANY', 'api@remove_plant_log_entry'),
array('/api/plants/log/fetch', 'ANY', 'api@fetch_plant_log_entries'),
array('/api/inventory/add', 'ANY', 'api@add_inventory_item'),
array('/api/chat/message/add', 'ANY', 'api@add_chat_message'),
/** Backup Controller */

View File

@@ -455,6 +455,35 @@ class ApiController extends BaseController {
}
}
/**
* Handles URL: /api/inventory/add
*
* @param Asatru\Controller\ControllerArg $request
* @return Asatru\View\JsonHandler
*/
public static function add_inventory_item($request)
{
try {
$name = $request->params()->query('name', null);
$description = $request->params()->query('description', null);
$location = $request->params()->query('location', null);
$group = $request->params()->query('group', null);
$photo = $request->params()->query('photo', null);
$itemid = InventoryModel::addItem($name, $description, $location, $group, $photo, true);
return json([
'code' => 200,
'item' => $itemid
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
/**
* Handles URL: /api/chat/message/add
*

View File

@@ -14,14 +14,15 @@ class InventoryModel extends \Asatru\Database\Model {
* @param $location
* @param $group
* @param $photo
* @param $api
* @return int
* @throws \Exception
*/
public static function addItem($name, $description, $location, $group, $photo)
public static function addItem($name, $description, $location, $group, $photo, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
if ((!$user) && (!$api)) {
throw new \Exception('Invalid user');
}
@@ -30,7 +31,7 @@ class InventoryModel extends \Asatru\Database\Model {
}
static::raw('INSERT INTO `' . self::tableName() . '` (name, group_ident, description, location, last_edited_user, last_edited_date) VALUES(?, ?, ?, ?, ?, CURRENT_TIMESTAMP)', [
$name, $group, $description, $location, $user->get('id')
$name, $group, $description, $location, (($user) ? $user->get('id') : 0)
]);
$row = static::raw('SELECT * FROM `' . self::tableName() . '` ORDER BY id DESC LIMIT 1')->first();
@@ -61,9 +62,11 @@ class InventoryModel extends \Asatru\Database\Model {
}
}
LogModel::addLog($user->get('id'), 'inventory', 'add_inventory_item', $name, url('/inventory?expand=' . $row->get('id') . '#anchor-item-' . $row->get('id')));
TextBlockModule::createdInventoryItem($name, url('/inventory?expand=' . $row->get('id') . '#anchor-item-' . $row->get('id')));
if (!$api) {
LogModel::addLog($user->get('id'), 'inventory', 'add_inventory_item', $name, url('/inventory?expand=' . $row->get('id') . '#anchor-item-' . $row->get('id')));
TextBlockModule::createdInventoryItem($name, url('/inventory?expand=' . $row->get('id') . '#anchor-item-' . $row->get('id')));
}
return $row->get('id');
} catch (\Exception $e) {
throw $e;

View File

@@ -78,7 +78,7 @@
</div>
<div class="inventory-item-author">
{{ __('app.last_edited_by', ['name' => UserModel::getNameById($inventory->get($i)->get('last_edited_user')), 'when' => (new Carbon($inventory->get($i)->get('last_edited_date')))->diffForHumans()]) }}
{{ __('app.last_edited_by', ['name' => (($inventory->get($i)->get('last_edited_user')) ? UserModel::getNameById($inventory->get($i)->get('last_edited_user')) : 'System'), 'when' => (new Carbon($inventory->get($i)->get('last_edited_date')))->diffForHumans()]) }}
</div>
</div>
</div>