From 4b060609298b4967cbb8395355b2f6459c799052 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 24 May 2021 14:08:30 +0200 Subject: [PATCH] Added job deletions --- config/routes.yaml | 11 ++++++++++- public/resources/job/index.js | 21 +++++++++++++++++++++ src/Controller/JobController.php | 15 ++++++++++++--- src/Repository/Job.php | 10 ++++++++++ templates/base.html.twig | 2 +- templates/job/index.html.twig | 5 ++++- 6 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 public/resources/job/index.js diff --git a/config/routes.yaml b/config/routes.yaml index d0891ce..75c2604 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -26,8 +26,17 @@ job_index: job_view: path: '/job/{id}' + methods: [ 'GET' ] defaults: - _controller: JeroenED\Webcron\Controller\JobController::viewAction + _controller: JeroenED\Webcron\Controller\JobController::jobAction + requirements: + id: \d+ + +job_delete: + path: '/job/{id}' + methods: [ 'DELETE' ] + defaults: + _controller: JeroenED\Webcron\Controller\JobController::jobAction requirements: id: \d+ diff --git a/public/resources/job/index.js b/public/resources/job/index.js new file mode 100644 index 0000000..ad6e719 --- /dev/null +++ b/public/resources/job/index.js @@ -0,0 +1,21 @@ +$(function() { + initDeleteButtons(); +}) + +function initDeleteButtons() { + $('.delete-btn').on('click', function() { + let me = $(this) + let href = me.data('href'); + let confirmation = me.data('confirmation'); + + if(confirm(confirmation)) { + $.ajax({ + url: href, + method: 'DELETE', + success: function(data) { + window.location.href = data.return_path; + } + }) + } + }) +} \ No newline at end of file diff --git a/src/Controller/JobController.php b/src/Controller/JobController.php index 9b5198c..0ccd183 100644 --- a/src/Controller/JobController.php +++ b/src/Controller/JobController.php @@ -5,6 +5,7 @@ namespace JeroenED\Webcron\Controller; use JeroenED\Framework\Controller; use JeroenED\Webcron\Repository\Job; +use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; @@ -20,13 +21,21 @@ class JobController extends Controller return $this->render('job/index.html.twig', ['jobs' => $jobs]); } - public function viewAction($id) + public function jobAction($id) { if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) { return new RedirectResponse($this->generateRoute('login')); } $jobRepo = new Job($this->getDbCon()); - $job = $jobRepo->getJob($id); + + if($this->getRequest()->getMethod() == 'GET') { + $job = $jobRepo->getJob($id); + return new Response('Not implemented, yet', Response::HTTP_NOT_IMPLEMENTED); + } elseif($this->getRequest()->getMethod() == 'DELETE') { + $success = $jobRepo->deleteJob($id); + $this->addFlash('success', $success['message']); + return new JsonResponse(['return_path' => $this->generateRoute('job_index')]); + } } public function editAction($id) @@ -58,7 +67,7 @@ class JobController extends Controller if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) { return new RedirectResponse($this->generateRoute('login')); } - + if($this->getRequest()->getMethod() == 'GET') { return $this->render('job/add.html.twig'); } elseif ($this->getRequest()->getMethod() == 'POST') { diff --git a/src/Repository/Job.php b/src/Repository/Job.php index 7944d4f..596db81 100644 --- a/src/Repository/Job.php +++ b/src/Repository/Job.php @@ -230,4 +230,14 @@ class Job } return $jobRslt; } + + public function deleteJob(int $id) + { + $addJobSql = "DELETE FROM job WHERE id = :id"; + + $addJobStmt = $this->dbcon->prepare($addJobSql); + $addJobStmt->executeQuery([':id' => $id]); + + return ['success' => true, 'message' => 'Cronjob succesfully deleted']; + } } \ No newline at end of file diff --git a/templates/base.html.twig b/templates/base.html.twig index 890f3d8..4f55550 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -9,7 +9,7 @@ {% block extrastyles %}{% endblock %} - + {% block extrascripts %}{% endblock %} diff --git a/templates/job/index.html.twig b/templates/job/index.html.twig index 1c2f7b7..a4b2729 100644 --- a/templates/job/index.html.twig +++ b/templates/job/index.html.twig @@ -24,7 +24,7 @@ - + {% endfor %} @@ -53,3 +53,6 @@ {% block extrastyles %} {% endblock %} +{% block extrascripts %} + +{% endblock %}