|
1 | 1 | import express, { Request, Response } from "express"; |
2 | 2 | import { pool } from ".."; |
| 3 | +import { Violation } from "../types/datatypes"; |
3 | 4 |
|
4 | 5 | const router = express(); |
5 | 6 |
|
6 | | -router.get("/get", async (req: Request, res: Response) => { |
| 7 | +// router.post("/check-license", async (req: Request, res: Response) => { |
| 8 | +// try { |
| 9 | +// const { license_number } = req.body; |
| 10 | + |
| 11 | +// const { rows: drivers } = await pool.query( |
| 12 | +// `SELECT id |
| 13 | +// FROM drivers |
| 14 | +// WHERE license_number = $1`, |
| 15 | +// [license_number] |
| 16 | +// ); |
| 17 | + |
| 18 | +// const driverFound = drivers[0]; |
| 19 | +// console.log(drivers); |
| 20 | + |
| 21 | +// if (!driverFound) { |
| 22 | +// res.status(401).json({ |
| 23 | +// title: "License Number Not Found", |
| 24 | +// message: "Driver with this license number does not exist.", |
| 25 | +// }); |
| 26 | +// return; |
| 27 | +// } |
| 28 | + |
| 29 | +// res.status(200).json(driverFound); |
| 30 | +// } catch (error) { |
| 31 | +// console.log(error); |
| 32 | +// res.sendStatus(500); |
| 33 | +// } |
| 34 | +// }); |
| 35 | + |
| 36 | +router.post("/add", async (req: Request, res: Response) => { |
7 | 37 | try { |
8 | | - console.log("Fetching drivers from the database..."); |
9 | | - |
10 | | - const { rows: violation } = await pool.query( |
11 | | - `SELECT |
12 | | - id, |
13 | | - driver_id, |
14 | | - violation_type, |
15 | | - violation_date, |
16 | | - paid_status |
17 | | - FROM violations WHERE driver_id =$1`, |
18 | | - [req.query.driver_id?.toString] |
| 38 | + const { |
| 39 | + driver_id, |
| 40 | + violation_type, |
| 41 | + violation_date, |
| 42 | + description, |
| 43 | + }: Violation = req.body; |
| 44 | + |
| 45 | + const { rows: drivers } = await pool.query( |
| 46 | + `SELECT * |
| 47 | + FROM drivers |
| 48 | + WHERE id = $1`, |
| 49 | + [driver_id] |
19 | 50 | ); |
20 | 51 |
|
21 | | - console.log("Violations fetched successfully:", violation); |
| 52 | + const driverFound = await drivers[0]; |
| 53 | + |
| 54 | + |
| 55 | + if (!driverFound) { |
| 56 | + res |
| 57 | + .status(404) |
| 58 | + .json({ title: "No Driver Found", message: "Driver is not found." }); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + console.log("THIS IS THE DRIVER FROM VIOLATION") |
| 63 | + console.log(driverFound) |
| 64 | + |
| 65 | + const violations = await pool.query( |
| 66 | + `INSERT INTO violations ( |
| 67 | + driver_id, |
| 68 | + violation_type, |
| 69 | + violation_date, |
| 70 | + description |
| 71 | + ) |
| 72 | + VALUES($1, $2, $3, $4)`, |
| 73 | + [driver_id, violation_type, violation_date, description] |
| 74 | + ); |
| 75 | + |
| 76 | + console.log(violations.rows[0]); |
| 77 | + |
| 78 | + res.status(200).json({ |
| 79 | + title: "Violation Added!", |
| 80 | + message: `Violation has been added for ${driverFound.first_name} ${driverFound.last_name}, ${violation_type}.`, |
| 81 | + }); |
22 | 82 |
|
23 | | - // Send the drivers list as a response |
24 | | - res.json(violation); |
25 | 83 | } catch (error) { |
26 | 84 | const errorMessage = (error as Error).message; |
27 | | - console.error("Error fetching violations:", errorMessage); // Log the error for debugging |
28 | | - res.status(500).json({ title: "Unknown Error", message: errorMessage }); |
| 85 | + console.error("Error:", errorMessage); |
| 86 | + res.status(500).json({ title: "Error", message: errorMessage }); |
29 | 87 | } |
30 | 88 | }); |
31 | 89 |
|
| 90 | +router.get("/get", async (req: Request, res: Response) => { |
| 91 | + try { |
| 92 | + const { driverId } = req.query; |
32 | 93 |
|
| 94 | + console.log("Received driverId:", driverId); |
33 | 95 |
|
34 | | - router.patch("/update", async (req: Request, res: Response) => { |
35 | | - const { id, ...updates } = req.body; |
| 96 | + if (!driverId) { |
| 97 | + res.status(400).json({ |
| 98 | + title: "Driver ID is required", |
| 99 | + message: "Please provide a driver ID.", |
| 100 | + }); |
| 101 | + return; |
| 102 | + } |
36 | 103 |
|
37 | | - if (!id) { |
38 | | - res.status(400).json({ |
39 | | - title: "Validation Error", |
40 | | - message: "Driver ID is required to update the record." |
41 | | - }); |
42 | | - return |
| 104 | + const driverIdStr = String(driverId); |
| 105 | + |
| 106 | + const { rows: cars } = await pool.query( |
| 107 | + `SELECT car_model, license_plate, brand, color, license_number |
| 108 | + FROM cars |
| 109 | + WHERE driver_id = $1`, |
| 110 | + [driverIdStr] |
| 111 | + ); |
| 112 | + |
| 113 | + if (cars.length === 0) { |
| 114 | + res.status(404).json({ |
| 115 | + title: "No Cars Found", |
| 116 | + message: "No cars found for the given driver ID.", |
| 117 | + }); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + res.status(200).json(cars); |
| 122 | + } catch (error: unknown) { |
| 123 | + if (error instanceof Error) { |
| 124 | + console.error("Error fetching cars:", error.message); |
| 125 | + res.status(500).json({ title: "Unknown Error", message: error.message }); |
| 126 | + return; |
| 127 | + } else { |
| 128 | + console.error("Unknown error:", error); |
| 129 | + res.status(500).json({ |
| 130 | + title: "Unknown Error", |
| 131 | + message: "An unexpected error occurred", |
| 132 | + }); |
| 133 | + return; |
43 | 134 | } |
| 135 | + } |
| 136 | +}); |
44 | 137 |
|
45 | | - const fields = Object.keys(updates); |
46 | | - const values = Object.values(updates); |
| 138 | +router.patch("/update", async (req: Request, res: Response) => { |
| 139 | + const { id, ...updates } = req.body; |
47 | 140 |
|
48 | | - const setClause = fields.map((field, index) => `${field} = $${index + 1}`).join(", "); |
| 141 | + if (!id) { |
| 142 | + res.status(400).json({ |
| 143 | + title: "Validation Error", |
| 144 | + message: "License plate is required to update the record.", |
| 145 | + }); |
| 146 | + return; |
| 147 | + } |
49 | 148 |
|
50 | | - const query = |
51 | | - `UPDATE drivers |
52 | | - SET ${ setClause } |
53 | | - WHERE id = $${ fields.length + 1 } |
54 | | - RETURNING *`; |
| 149 | + const fields = Object.keys(updates); |
| 150 | + const values = Object.values(updates); |
55 | 151 |
|
56 | | - const result = await pool.query( |
57 | | - query, |
58 | | - [...values, id]); |
| 152 | + const setClause = fields |
| 153 | + .map((field, index) => `${field} = $${index + 1}`) |
| 154 | + .join(", "); |
59 | 155 |
|
60 | | - if (result.rowCount === 0) { |
61 | | - res.status(404).json({ |
62 | | - title: "Not Found", |
63 | | - message: "Driver with the specified ID does not exist." |
64 | | - }); |
65 | | - return |
66 | | - } |
| 156 | + const query = `UPDATE cars SET ${setClause} |
| 157 | + WHERE id = $${fields.length + 1} |
| 158 | + RETURNING *`; |
67 | 159 |
|
68 | | - const updatedViolation = result.rows[0]; |
69 | | - console.log("Driver updated successfully:", updatedViolation); |
| 160 | + const result = await pool.query(query, [...values, id]); |
70 | 161 |
|
71 | | - res.status(200).json({ |
72 | | - title: "Driver Updated!", |
73 | | - message: `Driver ${updatedViolation.last_name}, ${updatedViolation.first_name} has been updated successfully.`, |
74 | | - driver: updatedViolation |
| 162 | + if (result.rowCount === 0) { |
| 163 | + res.status(404).json({ |
| 164 | + title: "Not Found", |
| 165 | + message: "Cars with the specified ID does not exist.", |
75 | 166 | }); |
| 167 | + return; |
| 168 | + } |
| 169 | + |
| 170 | + const updateCar = result.rows[0]; |
| 171 | + console.log("Car updated successfully:", updateCar); |
| 172 | + |
| 173 | + res.status(200).json({ |
| 174 | + title: "Car Updated!", |
| 175 | + message: `Car has been updated successfully.`, |
| 176 | + driver: updateCar, |
| 177 | + }); |
76 | 178 | }); |
77 | 179 |
|
78 | | -router.delete("/delete", async (req : Request, res : Response) => { |
| 180 | +router.delete("/delete", async (req: Request, res: Response) => { |
| 181 | + try { |
| 182 | + console.log("Fetching. . ."); |
79 | 183 |
|
80 | | - try{ |
81 | | - await pool.query( |
82 | | - `DELETE FROM |
83 | | - violations |
84 | | - WHERE |
85 | | - id = $1 |
| 184 | + const car = await pool.query( |
| 185 | + `DELETE FROM |
| 186 | + cars |
| 187 | + WHERE |
| 188 | + id = $1 |
86 | 189 | RETURNING *`, |
87 | | - [req.body.id] |
88 | | - ); |
89 | | - res.status(200).json({title: "Violation Deleted", message: "Violation Deleted Successfully."}) |
90 | | - } |
91 | | - catch(error){ |
92 | | - console.log(error) |
93 | | - } |
94 | | - }); |
| 190 | + [req.body.id] |
| 191 | + ); |
| 192 | + |
| 193 | + res.status(200).json({ message: "Car Added Successfully" }); |
| 194 | + console.log("Driver deleted successfully:", car); |
| 195 | + } catch (error) { |
| 196 | + res.status(500).json({ message: error }); |
| 197 | + } |
| 198 | +}); |
95 | 199 |
|
96 | 200 | export default router; |
0 commit comments