-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
191 lines (172 loc) · 6.71 KB
/
server.js
File metadata and controls
191 lines (172 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const app = express();
// Middleware
app.use(express.json());
app.use(cors());
// MongoDB connection
mongoose.connect('mongodb://127.0.0.1:27017/loginDB')
.then(() => console.log("Database connected successfully!"))
.catch(err => console.log("Connection error:", err));
// --- User Model ---
const UserSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
const User = mongoose.model('User', UserSchema);
// --- Booking Model (Updated with Phone) ---
const BookingSchema = new mongoose.Schema({
userEmail: { type: String, required: true },
phone: { type: String, required: true },
vehicleName: { type: String, required: true },
pickupDate: { type: String, required: true },
pickupTime: { type: String, required: true },
status: { type: String, default: "Pending" },
bookingDate: { type: Date, default: Date.now }
});
const Booking = mongoose.model('Booking', BookingSchema);
// --- Vehicle Model (Updated with Fuel and Transmission) ---
const VehicleSchema = new mongoose.Schema({
name: { type: String, required: true },
type: { type: String, required: true },
price: { type: Number, required: true },
img: { type: String, required: true },
status: { type: String, default: "Available" },
// 👈 මේ පේළි දෙක අලුතින් එකතු කළා
fuel: { type: String, default: "Petrol" },
transmission: { type: String, default: "Auto" }
});
const Vehicle = mongoose.model('Vehicle', VehicleSchema);
// --- Register Route ---
app.post('/register', async (req, res) => {
try {
const { email, password } = req.body;
const newUser = new User({ email, password });
await newUser.save();
res.status(201).send({ message: "Registration successful! Please login." });
} catch (err) {
res.status(400).send({ message: "This email is already in use!" });
}
});
// --- Login Route ---
app.post('/login', async (req, res) => {
try {
const { email, password } = req.body;
const user = await User.findOne({ email: email, password: password });
if (user) {
res.status(200).send({ message: "Logged in successfully!" });
} else {
res.status(401).send({ message: "Invalid email or password!" });
}
} catch (err) {
res.status(500).send({ message: "Server error occurred!" });
}
});
// --- Booking Route (POST) ---
app.post('/api/book', async (req, res) => {
try {
const { userEmail, phone, vehicleName, pickupDate, pickupTime } = req.body;
const newBooking = new Booking({
userEmail, phone, vehicleName, pickupDate, pickupTime
});
await newBooking.save();
res.status(201).send({ message: "Ride reserved successfully!" });
} catch (err) {
res.status(500).send({ message: "Booking failed! Please try again." });
}
});
// --- Fetch My Bookings (GET) ---
app.get('/api/my-bookings/:email', async (req, res) => {
try {
const email = req.params.email;
const userBookings = await Booking.find({ userEmail: email });
res.status(200).json(userBookings);
} catch (err) {
res.status(500).send({ message: "Error fetching bookings!" });
}
});
// --- Fetch ALL Bookings for Admin (GET) ---
app.get('/api/admin/all-bookings', async (req, res) => {
try {
const allBookings = await Booking.find().sort({ bookingDate: -1 });
res.status(200).json(allBookings);
} catch (err) {
res.status(500).send({ message: "Error fetching all bookings!" });
}
});
// --- Update Booking Status (PATCH) ---
app.patch('/api/book/status/:id', async (req, res) => {
try {
const { status } = req.body;
await Booking.findByIdAndUpdate(req.params.id, { status: status });
res.status(200).send({ message: `Booking ${status} successfully!` });
} catch (err) {
res.status(500).send({ message: "Failed to update status!" });
}
});
// --- Sync Vehicle Status with Home Page ---
app.patch('/api/admin/vehicle/status-by-name', async (req, res) => {
try {
const { name, status } = req.body;
await Vehicle.findOneAndUpdate({ name: name }, { status: status });
res.status(200).send({ message: "Vehicle availability updated!" });
} catch (err) {
res.status(500).send({ message: "Failed to update vehicle status" });
}
});
// --- Fixed Delete Logic to Clear Red Tag ---
app.delete('/api/book/:id', async (req, res) => {
try {
const booking = await Booking.findById(req.params.id);
if (booking) {
// බුකින් එක අයින් කරන විට වාහනය නැවත Available කරයි
await Vehicle.findOneAndUpdate({ name: booking.vehicleName }, { status: "Available" });
await Booking.findByIdAndDelete(req.params.id);
res.status(200).send({ message: "Deleted successfully and vehicle made available" });
} else {
res.status(404).send({ message: "Booking not found" });
}
} catch (err) {
res.status(500).send({ message: "Error deleting booking" });
}
});
// --- Vehicle APIs ---
app.get('/api/vehicles', async (req, res) => {
try {
const vehicles = await Vehicle.find();
res.json(vehicles);
} catch (err) {
res.status(500).json({ message: "Error fetching vehicles" });
}
});
app.post('/api/admin/add-vehicle', async (req, res) => {
try {
const { name, type, price, img, fuel, transmission } = req.body; // 👈 දත්ත මෙහිදී ලබා ගනී
const newVehicle = new Vehicle({ name, type, price, img, fuel, transmission });
await newVehicle.save();
res.status(201).json({ message: "Vehicle added successfully!" });
} catch (err) {
res.status(500).json({ message: "Error adding vehicle" });
}
});
app.patch('/api/admin/vehicle/:id', async (req, res) => {
try {
const { price, name } = req.body;
await Vehicle.findByIdAndUpdate(req.params.id, { name, price });
res.json({ message: "Vehicle updated successfully!" });
} catch (err) {
res.status(500).json({ message: "Update failed" });
}
});
app.delete('/api/admin/vehicle/:id', async (req, res) => {
try {
await Vehicle.findByIdAndDelete(req.params.id);
res.json({ message: "Vehicle removed from inventory!" });
} catch (err) {
res.status(500).json({ message: "Error deleting vehicle" });
}
});
app.listen(5000, () => {
console.log("Server is running on port 5000...");
});