This commit is contained in:
Daniel Brendel
2024-10-01 22:02:50 +02:00
parent c09c8b33e4
commit 76a55d7cc9
3 changed files with 43 additions and 5 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/tasks/add', 'ANY', 'api@add_task'),
array('/api/inventory/add', 'ANY', 'api@add_inventory_item'),
array('/api/chat/message/add', 'ANY', 'api@add_chat_message'),

View File

@@ -455,6 +455,33 @@ class ApiController extends BaseController {
}
}
/**
* Handles URL: /api/tasks/add
*
* @param Asatru\Controller\ControllerArg $request
* @return Asatru\View\JsonHandler
*/
public static function add_task($request)
{
try {
$title = $request->params()->query('title', null);
$description = $request->params()->query('description', null);
$due_date = $request->params()->query('due_date', null);
$itemid = TasksModel::addTask($title, $description, $due_date, true);
return json([
'code' => 200,
'item' => $itemid
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
/**
* Handles URL: /api/inventory/add
*

View File

@@ -10,21 +10,31 @@ class TasksModel extends \Asatru\Database\Model {
* @param $title
* @param $description
* @param $due_date
* @return void
* @param $api
* @return int
* @throws \Exception
*/
public static function addTask($title, $description = '', $due_date = null)
public static function addTask($title, $description = '', $due_date = null, $api = false)
{
try {
$user = UserModel::getAuthUser();
if (!$user) {
if ((!$user) && (!$api)) {
throw new \Exception('Invalid user');
}
static::raw('INSERT INTO `' . self::tableName() . '` (title, description, due_date) VALUES(?, ?, ?)', [$title, $description, $due_date]);
LogModel::addLog($user->get('id'), 'tasks', 'add_task', $title, url('/tasks'));
TextBlockModule::createdTask($title, url('/tasks'));
if (!$api) {
LogModel::addLog($user->get('id'), 'tasks', 'add_task', $title, url('/tasks'));
TextBlockModule::createdTask($title, url('/tasks'));
}
$latest = static::raw('SELECT * FROM `' . self::tableName() . '` ORDER BY id DESC LIMIT 1')->first();
if ($latest) {
return $latest->get('id');
}
return 0;
} catch (\Exception $e) {
throw $e;
}