diff --git a/TODO.md b/TODO.md index 94d3732..e9238bc 100644 --- a/TODO.md +++ b/TODO.md @@ -12,7 +12,7 @@ - [ ] remove/edit fuel record ## Until release -- [ ] Sync offline data from locale storage +- [x] Sync offline data from locale storage - [ ] Include kilometer state of an car - [ ] More charts - [ ] Average fuel conusption @@ -21,3 +21,24 @@ - [ ] Average fuel conusption in last 30 days - [ ] Kilometer state in last 30 days` - [ ] Offline navigation between dashboard and offline form + +## What has to be done +- [x] Vehicle delete +- [ ] intro tutorial when no car exist or just dont show anything +- [ ] change/set default car +- [ ] hide errors + +## Nice to have +- [ ] specific car view - charts, fuel records +- [ ] remove/edit fuel record +- [ ] Include kilometer state of an car +- [ ] More charts + - [ ] Average fuel conusption + - [ ] Kilometer state +- [ ] More cards + - [ ] Average fuel conusption in last 30 days + - [ ] Kilometer state in last 30 days` +- [ ] Offline navigation between dashboard and offline form +- [ ] Fix vehicle deletion - wrong redirect + + diff --git a/app/controllers/VehicleController.php b/app/controllers/VehicleController.php index 7ef6dcf..231e8dc 100644 --- a/app/controllers/VehicleController.php +++ b/app/controllers/VehicleController.php @@ -56,7 +56,23 @@ class VehicleController extends Controller { } public function delete() { - // TODO: Delete vehicle (to be implemented later) + if(!$_SERVER['REQUEST_METHOD'] === 'POST') { + echo "Wrong method"; + return; + } + + // TODO: Validate the request + $vehicle_id = $_POST['vehicle_id']; + + $vehicle = new Vehicle(); + $result = $vehicle->delete($vehicle_id, $_SESSION['user']['id']); + + if($result != true) { + echo "Something went wrong"; + return; + } + + $this->view('vehicles/index', ['title' => 'Vehicles', 'vehicles' => $vehicles]); } public function api_get() { diff --git a/app/models/Vehicle.php b/app/models/Vehicle.php index 3832345..6884c9f 100644 --- a/app/models/Vehicle.php +++ b/app/models/Vehicle.php @@ -60,4 +60,28 @@ class Vehicle { return $result->fetch_assoc(); } + + public function delete($vehicle_id, $user_id) { + try { + $stmt = $this->db->prepare("SELECT id FROM vehicles WHERE id = ? AND user_id = ?"); + $stmt->bind_param("ii", $vehicle_id, $user_id); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows === 0) { + return "Error: Unauthorized action or vehicle not found."; + } + + $stmt = $this->db->prepare("DELETE FROM vehicles WHERE id = ?"); + $stmt->bind_param("i", $vehicle_id); + + if ($stmt->execute()) { + return true; + } else { + return "Error: " . $stmt->error; + } + } catch (mysqli_sql_exception $e) { + return $e->getMessage(); + } + } } diff --git a/app/views/vehicles/index.php b/app/views/vehicles/index.php index 29b6a74..7d86f3a 100644 --- a/app/views/vehicles/index.php +++ b/app/views/vehicles/index.php @@ -14,8 +14,10 @@

- Edit - Delete +
+ + +
diff --git a/public/index.php b/public/index.php index 256fa97..2159595 100644 --- a/public/index.php +++ b/public/index.php @@ -46,7 +46,7 @@ $router->group('/vehicles', ['RequireAuth'], function ($router) { $router->add('', 'VehicleController@index'); $router->add('/create', 'VehicleController@create'); $router->add('/edit/{id}', 'VehicleController@edit'); - $router->add('/delete/{id}', 'VehicleController@delete'); + $router->add('/delete', 'VehicleController@delete'); }); $router->group('/refuel', ['RequireAuth'], function ($router) {