From 9b4a0ed1438aeeb3c9f736dad2c199b1e61a8c51 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Mon, 5 Sep 2016 17:13:22 -0400 Subject: [PATCH] Add task toggle and delete --- .../Controllers/Server/TaskController.php | 51 ++++++++++++ app/Http/Routes/ServerRoutes.php | 5 ++ app/Repositories/TaskRepository.php | 37 +++++++++ resources/views/server/tasks/index.blade.php | 83 ++++++++++++++++++- 4 files changed, 174 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/Server/TaskController.php b/app/Http/Controllers/Server/TaskController.php index 220f7ff18..f92a29deb 100644 --- a/app/Http/Controllers/Server/TaskController.php +++ b/app/Http/Controllers/Server/TaskController.php @@ -27,6 +27,7 @@ use Alert; use Log; use Cron; +use Pterodactyl\Repositories; use Pterodactyl\Models; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayValidationException; @@ -84,4 +85,54 @@ class TaskController extends Controller 'task' => Models\Task::where('id', $id)->where('server', $server->id)->firstOrFail() ]); } + + public function deleteTask(Request $request, $uuid, $id) + { + $server = Models\Server::getByUUID($uuid); + $this->authorize('delete-task', $server); + + $task = Models\Task::findOrFail($id); + + if (!$task || $server->id !== $task->server) { + return response()->json([ + 'error' => 'No task by that ID was found associated with this server.' + ], 404); + } + + try { + $repo = new Repositories\TaskRepository; + $repo->delete($id); + return response()->json([], 204); + } catch (\Exception $ex) { + return response()->json([ + 'error' => 'A server error occured while attempting to delete this task.' + ], 503); + } + } + + public function toggleTask(Request $request, $uuid, $id) + { + $server = Models\Server::getByUUID($uuid); + $this->authorize('toggle-task', $server); + + $task = Models\Task::findOrFail($id); + + if (!$task || $server->id !== $task->server) { + return response()->json([ + 'error' => 'No task by that ID was found associated with this server.' + ], 404); + } + + try { + $repo = new Repositories\TaskRepository; + $resp = $repo->toggle($id); + return response()->json([ + 'status' => $resp + ]); + } catch (\Exception $ex) { + return response()->json([ + 'error' => 'A server error occured while attempting to toggle this task.' + ], 503); + } + } } diff --git a/app/Http/Routes/ServerRoutes.php b/app/Http/Routes/ServerRoutes.php index d6093d37d..095deedf8 100644 --- a/app/Http/Routes/ServerRoutes.php +++ b/app/Http/Routes/ServerRoutes.php @@ -141,6 +141,11 @@ class ServerRoutes { 'uses' => 'Server\TaskController@deleteTask' ]); + $router->post('tasks/toggle/{id}', [ + 'as' => 'server.tasks.toggle', + 'uses' => 'Server\TaskController@toggleTask' + ]); + // Assorted AJAX Routes $router->group(['prefix' => 'ajax'], function ($server) use ($router) { // Returns Server Status diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index 3493ba272..dead06381 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -53,6 +53,43 @@ class TaskRepository // } + /** + * Deletes a given task. + * @param int $id + * + * @return bool + */ + public function delete($id) + { + $task = Models\Task::findOrFail($id); + try { + $task->delete(); + return true; + } catch (\Exception $ex) { + throw $ex; + } + } + + /** + * Toggles a task active or inactive. + * @param int $id + * + * @return int + */ + public function toggle($id) + { + $task = Models\Task::findOrFail($id); + try { + $task->active = ($task->active === 1) ? 0 : 1; + $task->queued = 0; + $task->save(); + + return $task->active; + } catch (\Exception $ex) { + throw $ex; + } + } + /** * Create a new scheduled task for a given server. * @param int $id diff --git a/resources/views/server/tasks/index.blade.php b/resources/views/server/tasks/index.blade.php index aa044c1b0..3598a76f8 100644 --- a/resources/views/server/tasks/index.blade.php +++ b/resources/views/server/tasks/index.blade.php @@ -51,10 +51,10 @@ @endif @can('delete-task', $server) - + @endcan @can('toggle-task', $server) - + @endcan @@ -68,6 +68,85 @@ @endsection