SPRINT 2 ‐ Backend - BlueJayBird11/UniJet GitHub Wiki
Right now the backend is run locally with the database.
NOTE THE BACKEND WILL NOT BE ABLE TO RUN QUERIES WITH THE TABLES FIRST SET UP
The backend runs on Expess.js, which also the developers to connect to the database to use queries.
For example, to do a get request:
const results = await db.query('SELECT * FROM passengers');
Or a post request:
const results = await db.query("INSERT INTO passengers (birthDate, email, passwordHash, phoneNumber, firstName, lastName, userStatus, carPool, rating, schedule) \
VALUES ($1, $2, $3, $4, $5, $6, 0, $7, NULL, NULL) returning *",
[req.body.birthDate, req.body.email, req.body.passwordHash, req.body.phoneNumber, req.body.firstName, req.body.lastName, req.body.carPool]);
The reason for the way this post request is formatted is to avoid string concatenation, which would allow SQL injection.
express() is stored as a constant named app
, and to make requests follow the format using this as an example:
app.get("/api/v1/passengers", async (req, res) => {
try {
const results = await db.query('SELECT * FROM passengers');
console.log(results.rows);
res.status(200).json({
status: "success",
results: results.rows.length,
data: {
restaurants: results.rows
},
});
} catch (err) {
console.log(err);
}
});
NOTE: Get requests return a status of 200, post: 201, put: 200, delete: 204
In the backend folder, there is a files named .env
This contains the variables used run the server and access the database, so if the password doesn't match, then it must be updated.
Todo: Making a database user strictly for UniJet is possibly a better option than using the default account
To insert rows into a table (non-relational), follow this format:
INSERT INTO <tableName> (<var1>, <var2>, <var3>, <var4>) VALUES
('ABC123', 'LA', NULL, 4.8),
('XYZ789', 'CA', 3, 4.6);
For relational tables:
INSERT INTO registeredAs (passengerID, driverID) VALUES (1, 1);