Integrated application chat system
This commit is contained in:
125
api/index.js
125
api/index.js
@@ -44,35 +44,42 @@ app.post('/application', async (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
app.get('/application/me', async (req, res) => {
|
||||
try {
|
||||
// TODO: replace with current user ID
|
||||
const applicationId = 1;
|
||||
// app.get('/application/me', async (req, res) => {
|
||||
// try {
|
||||
// // TODO: replace with current user ID
|
||||
// const applicationId = 1;
|
||||
|
||||
const rows = await pool.query(
|
||||
`SELECT app.*,
|
||||
member.name AS member_name
|
||||
FROM applications AS app
|
||||
INNER JOIN members AS member ON member.id = app.member_id
|
||||
WHERE app.member_id = ?;`,
|
||||
[applicationId]
|
||||
);
|
||||
// const rows = await pool.query(
|
||||
// `SELECT app.*,
|
||||
// member.name AS member_name
|
||||
// FROM applications AS app
|
||||
// INNER JOIN members AS member ON member.id = app.member_id
|
||||
// WHERE app.member_id = ?;`,
|
||||
// [applicationId]
|
||||
// );
|
||||
|
||||
if (!Array.isArray(rows) || rows.length === 0) {
|
||||
return res.sendStatus(204);
|
||||
}
|
||||
// if (!Array.isArray(rows) || rows.length === 0) {
|
||||
// return res.sendStatus(204);
|
||||
// }
|
||||
|
||||
return res.status(200).json(rows[0]);
|
||||
} catch (err) {
|
||||
console.error('Query failed:', err);
|
||||
return res.status(500).json({ error: 'Failed to load application' });
|
||||
}
|
||||
});
|
||||
// return res.status(200).json(rows[0]);
|
||||
// } catch (err) {
|
||||
// console.error('Query failed:', err);
|
||||
// return res.status(500).json({ error: 'Failed to load application' });
|
||||
// }
|
||||
// });
|
||||
|
||||
app.get('/application/:id', async (req, res) => {
|
||||
const appID = req.params.id;
|
||||
let appID = req.params.id;
|
||||
|
||||
//TODO: Replace with real user Authorization and whatnot
|
||||
if (appID === "me")
|
||||
appID = 1;
|
||||
|
||||
try {
|
||||
const rows = await pool.query(
|
||||
const conn = await pool.getConnection()
|
||||
|
||||
const application = await conn.query(
|
||||
`SELECT app.*,
|
||||
member.name AS member_name
|
||||
FROM applications AS app
|
||||
@@ -81,11 +88,29 @@ app.get('/application/:id', async (req, res) => {
|
||||
[appID]
|
||||
);
|
||||
|
||||
if (!Array.isArray(rows) || rows.length === 0) {
|
||||
return res.send(404).json("Application Not Found");
|
||||
if (!Array.isArray(application) || application.length === 0) {
|
||||
conn.release();
|
||||
return res.status(204).json("Application Not Found");
|
||||
}
|
||||
|
||||
return res.status(200).json(rows[0]);
|
||||
const comments = await conn.query(`SELECT app.id AS comment_id,
|
||||
app.post_content,
|
||||
app.poster_id,
|
||||
app.post_time,
|
||||
app.last_modified,
|
||||
member.name AS poster_name
|
||||
FROM application_comments AS app
|
||||
INNER JOIN members AS member ON member.id = app.poster_id
|
||||
WHERE app.application_id = ?;`,
|
||||
[appID]);
|
||||
|
||||
conn.release()
|
||||
|
||||
const output = {
|
||||
application: application[0],
|
||||
comments,
|
||||
}
|
||||
return res.status(200).json(output);
|
||||
}
|
||||
catch (err) {
|
||||
console.error('Query failed:', err);
|
||||
@@ -115,12 +140,6 @@ app.get('/application/all', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/application/message', (req, res) => {
|
||||
const data = req.body;
|
||||
applicationData.messages.push(data);
|
||||
res.status(200).send();
|
||||
});
|
||||
|
||||
app.post('/application/approve/:id', async (req, res) => {
|
||||
const appID = req.params.id;
|
||||
|
||||
@@ -179,6 +198,46 @@ app.post('/application/deny/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/application/:id/comment', async (req, res) => {
|
||||
const appID = req.params.id;
|
||||
const data = req.body.message;
|
||||
const user = 1;
|
||||
|
||||
const sql = `INSERT INTO application_comments(
|
||||
application_id,
|
||||
poster_id,
|
||||
post_content
|
||||
)
|
||||
VALUES(?, ?, ?);`
|
||||
|
||||
try {
|
||||
const conn = await pool.getConnection();
|
||||
|
||||
const result = await conn.query(sql, [appID, user, data])
|
||||
console.log(result)
|
||||
if (result.affectedRows !== 1) {
|
||||
conn.release();
|
||||
throw new Error("Insert Failure")
|
||||
}
|
||||
|
||||
const getSQL = `SELECT app.id AS comment_id,
|
||||
app.post_content,
|
||||
app.poster_id,
|
||||
app.post_time,
|
||||
app.last_modified,
|
||||
member.name AS poster_name
|
||||
FROM application_comments AS app
|
||||
INNER JOIN members AS member ON member.id = app.poster_id
|
||||
WHERE app.id = ?; `;
|
||||
const comment = await conn.query(getSQL, [result.insertId])
|
||||
res.status(201).json(comment[0]);
|
||||
|
||||
} catch (err) {
|
||||
console.error('Comment failed:', err);
|
||||
res.status(500).json({ error: 'Could not post comment' });
|
||||
}
|
||||
})
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Example app listening on port ${port}`)
|
||||
console.log(`Example app listening on port ${port} `)
|
||||
})
|
||||
Reference in New Issue
Block a user