Skip to content

Commit 3def603

Browse files
authored
Merge pull request #169 from lavefayt/main
add violations
2 parents c836f02 + 8086ae6 commit 3def603

File tree

8 files changed

+324
-147
lines changed

8 files changed

+324
-147
lines changed

backend/src/routes/violation.ts

Lines changed: 168 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,200 @@
11
import express, { Request, Response } from "express";
22
import { pool } from "..";
3+
import { Violation } from "../types/datatypes";
34

45
const router = express();
56

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) => {
737
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]
1950
);
2051

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+
});
2282

23-
// Send the drivers list as a response
24-
res.json(violation);
2583
} catch (error) {
2684
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 });
2987
}
3088
});
3189

90+
router.get("/get", async (req: Request, res: Response) => {
91+
try {
92+
const { driverId } = req.query;
3293

94+
console.log("Received driverId:", driverId);
3395

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+
}
36103

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;
43134
}
135+
}
136+
});
44137

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;
47140

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+
}
49148

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);
55151

56-
const result = await pool.query(
57-
query,
58-
[...values, id]);
152+
const setClause = fields
153+
.map((field, index) => `${field} = $${index + 1}`)
154+
.join(", ");
59155

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 *`;
67159

68-
const updatedViolation = result.rows[0];
69-
console.log("Driver updated successfully:", updatedViolation);
160+
const result = await pool.query(query, [...values, id]);
70161

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.",
75166
});
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+
});
76178
});
77179

78-
router.delete("/delete", async (req : Request, res : Response) => {
180+
router.delete("/delete", async (req: Request, res: Response) => {
181+
try {
182+
console.log("Fetching. . .");
79183

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
86189
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+
});
95199

96200
export default router;

backend/src/types/datatypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface Violation {
2929
driver_id: string;
3030
violation_type: string;
3131
violation_date: string;
32+
description: string;
3233
paid_status: boolean;
3334
}
3435

frontend/src/components/AddCar.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ const AddCar = ({
111111
<>
112112
<p className="text-textgreen font-syke-light text-lg">Confirm car details.</p>
113113
<div className="grid grid-cols-2 gap-6 p-5 w-full max-w-xl">
114-
{/* License Plate */}
115114
<div className="flex flex-col">
116115
<h1 className="text-white font-syke-light text-xl">License Plate</h1>
117116
<h1 className="text-textgreen font-syke-medium text-3xl">

0 commit comments

Comments
 (0)