-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
212 lines (182 loc) · 5.78 KB
/
index.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
require("dotenv").config();
const MONGODB_URI = process.env.MONGO_URI;
//console.log('MongoDB URI:', process.env.MONGO_URI);
const path = require("path");
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const { table } = require("console");
// Initialize the Express app
const app = express();
// Use CORS middleware
app.use(cors());
// Other middleware (like for parsing JSON, if needed)
app.use((req, res, next) => {
res.set('Cache-Control', 'no-store');
next();
});
app.use(express.json());
app.use("/", express.static("./"));
app.use("/assets", express.static(path.join(__dirname, "assets")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "./index.html"));
});
const linesDb = mongoose.createConnection(`${MONGODB_URI}/lines`, {
useNewUrlParser: true,
useUnifiedTopology: true,
connectTimeoutMS: 30000, // Increase connection timeout
socketTimeoutMS: 45000, // Increase socket timeout
});
linesDb.on("error", console.error.bind(console, "connection error (linesDb):"));
linesDb.once("open", () => console.log("Connected to 'lines' database"));
const entriesDb = mongoose.createConnection(`${MONGODB_URI}/entries`, {
useNewUrlParser: true,
useUnifiedTopology: true,
connectTimeoutMS: 30000, // Increase connection timeout
socketTimeoutMS: 45000, // Increase socket timeout
});
entriesDb.on(
"error",
console.error.bind(console, "connection error (entriesDb):")
);
entriesDb.once("open", () => console.log("Connected to 'entries' database"));
// Route for fetching data from 'lines' database
app.get("/api/lines", async (req, res) => {
console.log("Fetching lines");
const cityFilter = req.query.city;
const query = cityFilter ? { city: cityFilter } : {};
try {
const lines = await Line.find(query).sort({ order: 1 });
res.json(lines);
} catch (error) {
res.status(500).send(error.message);
}
});
app.get("/api/lines/name/:lineName", async (req, res) => {
try {
const lineName = req.params.lineName;
const city = req.query.city;
const query = { name: lineName };
if (city) {
query.city = city;
}
const line = await Line.findOne(query);
if (!line) {
return res.status(404).send("Line not found");
}
res.json(line);
} catch (error) {
res.status(500).send(error.message);
}
});
app.get("/api/entries", async (req, res) => {
const cityFilter = req.query.city;
const query = cityFilter ? { city: cityFilter } : {};
try {
const entries = await Entry.find(query);
res.json(entries);
} catch (error) {
res.status(500).send(error.message);
}
});
app.post("/submit-form", async (req, res) => {
const formData = req.body;
try {
// Check if an entry with the same line, station, location, and direction already exists
let existingEntry = await Entry.findOne({
city: formData.city,
line: formData.line,
station: formData.station,
location: formData.location,
direction: formData.direction,
});
if (existingEntry) {
existingEntry.last_edit = formData.last_edit;
existingEntry.edits = (existingEntry.edits || 0) + 1;
if (existingEntry.state !== formData.state) {
existingEntry.state = formData.state;
}
await existingEntry.save();
res.json({ message: "Existing entry updated", data: existingEntry });
} else {
const newEntry = new Entry({
...formData,
edits: 1,
});
await newEntry.save();
res.json({ message: "New entry created", data: newEntry });
}
} catch (error) {
console.error("Error in form submission:", error);
res.status(500).send(error.message);
}
});
app.patch("/api/entries/:id", async (req, res) => {
const id = req.params.id;
const updatedData = req.body;
console.log("Updated data:", updatedData);
try {
// Find the entry to be updated
const entryToUpdate = await Entry.findById(id);
let duplicateEdits = 0;
if (!entryToUpdate) {
return res.status(404).send("Entry not found");
}
// Check if there's another entry with the same characteristics but a different _id
const duplicateEntry = await Entry.findOne({
_id: { $ne: id },
line: updatedData.line,
station: updatedData.station,
location: updatedData.location,
direction: updatedData.direction,
});
// If a duplicate is found, delete it
if (duplicateEntry) {
duplicateEdits = duplicateEntry.edits;
await Entry.deleteOne({ _id: duplicateEntry._id });
}
// Update the existing entry with the new data
Object.assign(entryToUpdate, updatedData);
entryToUpdate.edits = entryToUpdate.edits + duplicateEdits + 1;
await entryToUpdate.save();
res.json({ message: "Entry updated", data: entryToUpdate });
} catch (error) {
console.error("Error updating entry:", error);
res.status(500).send(error.message);
}
});
app.delete("/api/entries/:id", async (req, res) => {
try {
const id = req.params.id; // Get the ID from the URL
await Entry.findByIdAndDelete(id); // Assuming 'Entry' is your Mongoose model
res.json({ message: "Entry deleted successfully" });
} catch (error) {
res.status(500).send(error.message);
}
});
// Lines
const LinesSchema = new mongoose.Schema({
city: String,
order: String,
name: String,
type: String,
stations: [String],
terminus: [String],
});
const Line = linesDb.model("Line", LinesSchema);
// User entries
const EntrySchema = new mongoose.Schema({
city: String,
line: String,
station: String,
location: String,
direction: String,
state: String,
edits: { type: Number, default: 0 },
last_edit: Date,
});
const Entry = entriesDb.model("Entry", EntrySchema);
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});