Skip to content

Commit f14677b

Browse files
feat: consolidate train and trip API routes to REST API format, update fetch calls in components
1 parent 3dcf3df commit f14677b

File tree

12 files changed

+194
-230
lines changed

12 files changed

+194
-230
lines changed

app/api/trains/find/route.js renamed to app/api/trains/[trainID]/route.js

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,18 @@ import validator from "validator";
55
import { connectToDatabase } from "@/app/lib/mongodb";
66
import { checkAuth } from "@/app/utils/auth";
77
import User from "@/app/models/User";
8-
import { today } from "@/app/utils/date";
98

10-
export async function GET() {
9+
export async function GET(req, { params }) {
1110
try {
1211
const email = await checkAuth();
1312

14-
await connectToDatabase(); // redundant but okay
15-
16-
// // Delete old entries
17-
// const dateObj = today();
18-
// await Train.deleteMany({
19-
// date: { $lt: dateObj.toISOString().slice(0, 10) },
20-
// });
21-
22-
const trains = await Train.find({
23-
email: email,
24-
});
25-
26-
return NextResponse.json(
27-
{ message: "Your Train Trips!", trains: trains },
28-
{ status: 200 }
29-
);
30-
} catch (error) {
31-
console.log(error.message);
32-
return NextResponse.json(
33-
{
34-
message:
35-
error.message ||
36-
"Something went wrong - Could not fetch your train trips.",
37-
},
38-
{ status: 500 }
39-
);
40-
}
41-
}
42-
43-
export async function POST(req) {
44-
try {
45-
const email = await checkAuth();
46-
47-
req = await req.json();
48-
4913
// Return fields:
5014
// i) email – (Email) - string - format email
5115
// ii) trainNumber – (Train Number) - number
5216
// iii) date – (Departure Date) - string - format yyyy-mm-dd
5317

54-
let { trainID } = req;
18+
let { trainID } = params;
19+
5520
// if train id is not a number
5621
if (!validator.isNumeric(trainID)) {
5722
throw new Error("Please give a valid train trip ID!");
@@ -140,3 +105,62 @@ export async function POST(req) {
140105
);
141106
}
142107
}
108+
109+
export async function DELETE(req, { params }) {
110+
try {
111+
const email = await checkAuth();
112+
113+
let { trainID } = params;
114+
115+
// if train id is not a number
116+
if (!validator.isNumeric(trainID)) {
117+
throw new Error("Please give a valid train trip ID!");
118+
}
119+
trainID = sanitize(trainID).trim();
120+
121+
if (validator.isEmpty(trainID)) {
122+
throw new Error("Please give a train trip ID!");
123+
}
124+
125+
await connectToDatabase(); // redundant but okay
126+
127+
const train = await Train.findOne({
128+
trainID: trainID,
129+
});
130+
131+
if (!train) {
132+
return NextResponse.json(
133+
{ message: "Train Trip not found!" },
134+
{ status: 404 }
135+
);
136+
}
137+
138+
if (train.email !== email) {
139+
return NextResponse.json(
140+
{
141+
message:
142+
"You are not authorized to delete this train trip!",
143+
},
144+
{ status: 401 }
145+
);
146+
}
147+
148+
await Train.deleteOne({
149+
trainID: trainID,
150+
});
151+
152+
return NextResponse.json(
153+
{ message: "Train Trip deleted successfully!" },
154+
{ status: 200 }
155+
);
156+
} catch (error) {
157+
console.log(error.message);
158+
return NextResponse.json(
159+
{
160+
message:
161+
"Something went wrong - Could not delete the train trip.",
162+
},
163+
{ status: 500 }
164+
);
165+
}
166+
}

app/api/trains/delete/route.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

app/api/trains/create/route.js renamed to app/api/trains/route.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,39 @@ import { connectToDatabase } from "@/app/lib/mongodb";
66
import { checkAuth } from "@/app/utils/auth";
77
import { today } from "@/app/utils/date";
88

9+
export async function GET() {
10+
try {
11+
const email = await checkAuth();
12+
13+
await connectToDatabase(); // redundant but okay
14+
15+
// // Delete old entries
16+
// const dateObj = today();
17+
// await Train.deleteMany({
18+
// date: { $lt: dateObj.toISOString().slice(0, 10) },
19+
// });
20+
21+
const trains = await Train.find({
22+
email: email,
23+
});
24+
25+
return NextResponse.json(
26+
{ message: "Your Train Trips!", trains: trains },
27+
{ status: 200 }
28+
);
29+
} catch (error) {
30+
console.log(error.message);
31+
return NextResponse.json(
32+
{
33+
message:
34+
error.message ||
35+
"Something went wrong - Could not fetch your train trips.",
36+
},
37+
{ status: 500 }
38+
);
39+
}
40+
}
41+
942
export async function POST(req) {
1043
try {
1144
const email = await checkAuth();

app/api/trips/find/route.js renamed to app/api/trips/[tripID]/route.js

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,11 @@ import validator from "validator";
55
import { connectToDatabase } from "@/app/lib/mongodb";
66
import { checkAuth } from "@/app/utils/auth";
77
import User from "@/app/models/User";
8-
import { today } from "@/app/utils/date";
98

10-
export async function GET() {
9+
export async function GET(req, { params }) {
1110
try {
1211
const email = await checkAuth();
1312

14-
await connectToDatabase(); // redundant but okay
15-
16-
// // Delete old entries
17-
// const dateObj = today();
18-
// await Trip.deleteMany({
19-
// date: { $lt: dateObj.toISOString().slice(0, 10) },
20-
// });
21-
22-
const trips = await Trip.find({
23-
email: email,
24-
});
25-
26-
return NextResponse.json(
27-
{ message: "Your Trips!", trips: trips },
28-
{ status: 200 }
29-
);
30-
} catch (error) {
31-
console.log(error.message);
32-
return NextResponse.json(
33-
{
34-
message:
35-
error.message ||
36-
"Something went wrong - Could not fetch your trips.",
37-
},
38-
{ status: 500 }
39-
);
40-
}
41-
}
42-
43-
export async function POST(req) {
44-
try {
45-
const email = await checkAuth();
46-
47-
req = await req.json();
48-
4913
// Return fields:
5014
// i) email – (Email) - string - format email
5115

@@ -54,7 +18,8 @@ export async function POST(req) {
5418
// vi) source – (Source) – drop down - 3 options: IIT, KGP, HWH, CCU
5519
// vii) destination – (Destination) – drop down - 3 options: IIT, KGP, HWH, CCU
5620

57-
let { tripID } = req;
21+
let { tripID } = params;
22+
5823
// if trip id is not a number
5924
if (!validator.isNumeric(tripID)) {
6025
throw new Error("Please give a valid trip ID!");
@@ -177,3 +142,56 @@ export async function POST(req) {
177142
);
178143
}
179144
}
145+
146+
export async function DELETE(req, { params }) {
147+
try {
148+
const email = await checkAuth();
149+
150+
let { tripID } = params;
151+
152+
// if trip id is not a number
153+
if (!validator.isNumeric(tripID)) {
154+
throw new Error("Please give a valid trip ID!");
155+
}
156+
tripID = sanitize(tripID).trim();
157+
158+
if (validator.isEmpty(tripID)) {
159+
throw new Error("Please give a trip ID!");
160+
}
161+
162+
await connectToDatabase(); // redundant but okay
163+
164+
const trip = await Trip.findOne({
165+
tripID: tripID,
166+
});
167+
168+
if (!trip) {
169+
return NextResponse.json(
170+
{ message: "Trip not found!" },
171+
{ status: 404 }
172+
);
173+
}
174+
175+
if (trip.email !== email) {
176+
return NextResponse.json(
177+
{ message: "You are not authorized to delete this trip!" },
178+
{ status: 401 }
179+
);
180+
}
181+
182+
await Trip.deleteOne({
183+
tripID: tripID,
184+
});
185+
186+
return NextResponse.json(
187+
{ message: "Trip deleted successfully!" },
188+
{ status: 200 }
189+
);
190+
} catch (error) {
191+
console.log(error.message);
192+
return NextResponse.json(
193+
{ message: "Something went wrong - Could not delete the trip." },
194+
{ status: 500 }
195+
);
196+
}
197+
}

0 commit comments

Comments
 (0)