mirror of
https://github.com/danielbrendel/hortusfox-web.git
synced 2026-01-06 04:40:13 -06:00
#275 Add tasks
This commit is contained in:
@@ -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'),
|
||||
|
||||
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user