Refactored API into seperate chunks
This commit is contained in:
198
api/index.js
198
api/index.js
@@ -9,202 +9,14 @@ const app = express()
|
|||||||
app.use(cors())
|
app.use(cors())
|
||||||
app.use(express.json())
|
app.use(express.json())
|
||||||
|
|
||||||
|
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
const pool = require('./db')
|
|
||||||
|
|
||||||
app.post('/application', async (req, res) => {
|
// Mount route modules
|
||||||
try {
|
const applicationsRouter = require('./routes/applications');
|
||||||
const App = req.body?.App || {};
|
const ranksRouter = require('./routes/ranks');
|
||||||
if (!app) return res.status(400).json({ error: 'Missing App payload' });
|
|
||||||
|
|
||||||
// TODO: replace with current user ID
|
app.use('/application', applicationsRouter);
|
||||||
const memberId = 1;
|
app.use('/ranks', ranksRouter);
|
||||||
|
|
||||||
const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`;
|
|
||||||
const appVersion = 1;
|
|
||||||
|
|
||||||
const params = [memberId, appVersion, JSON.stringify(App)]
|
|
||||||
|
|
||||||
console.log(params)
|
|
||||||
|
|
||||||
await pool.query(sql, params);
|
|
||||||
|
|
||||||
res.sendStatus(201);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Insert failed:', err);
|
|
||||||
res.status(500).json({ error: 'Failed to save application' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/application/all', async (req, res) => {
|
|
||||||
try {
|
|
||||||
const sql = `SELECT
|
|
||||||
member.name AS member_name,
|
|
||||||
app.id,
|
|
||||||
app.member_id,
|
|
||||||
app.submitted_at,
|
|
||||||
app.app_status
|
|
||||||
FROM applications AS app
|
|
||||||
LEFT JOIN members AS member
|
|
||||||
ON member.id = app.member_id;`
|
|
||||||
|
|
||||||
const rows = await pool.query(sql);
|
|
||||||
|
|
||||||
res.status(200).json(rows);
|
|
||||||
} catch {
|
|
||||||
console.error(err);
|
|
||||||
res.status(500);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.get('/application/:id', async (req, res) => {
|
|
||||||
let appID = req.params.id;
|
|
||||||
|
|
||||||
//TODO: Replace with real user Authorization and whatnot
|
|
||||||
// if the application is not "me" and I am not a recruiter, deny access to the application (return 403 or whatever)
|
|
||||||
if (appID === "me")
|
|
||||||
appID = 2;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const conn = await pool.getConnection()
|
|
||||||
|
|
||||||
const application = await conn.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.id = ?;`,
|
|
||||||
[appID]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!Array.isArray(application) || application.length === 0) {
|
|
||||||
conn.release();
|
|
||||||
return res.status(204).json("Application Not Found");
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
return res.status(500).json({ error: 'Failed to load application' });
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
app.post('/application/approve/:id', async (req, res) => {
|
|
||||||
const appID = req.params.id;
|
|
||||||
|
|
||||||
const sql = `
|
|
||||||
UPDATE applications
|
|
||||||
SET approved_at = NOW()
|
|
||||||
WHERE id = ?
|
|
||||||
AND approved_at IS NULL
|
|
||||||
AND denied_at IS NULL
|
|
||||||
`;
|
|
||||||
try {
|
|
||||||
const result = await pool.execute(sql, appID);
|
|
||||||
|
|
||||||
console.log(result);
|
|
||||||
|
|
||||||
if (result.affectedRows === 0) {
|
|
||||||
res.status(400).json('Something went wrong approving the application');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.affectedRows == 1) {
|
|
||||||
res.sendStatus(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Approve failed:', err);
|
|
||||||
res.status(500).json({ error: 'Failed to approve application' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
app.post('/application/deny/:id', async (req, res) => {
|
|
||||||
const appID = req.params.id;
|
|
||||||
|
|
||||||
const sql = `
|
|
||||||
UPDATE applications
|
|
||||||
SET denied_at = NOW()
|
|
||||||
WHERE id = ?
|
|
||||||
AND approved_at IS NULL
|
|
||||||
AND denied_at IS NULL
|
|
||||||
`;
|
|
||||||
try {
|
|
||||||
const result = await pool.execute(sql, appID);
|
|
||||||
|
|
||||||
console.log(result);
|
|
||||||
|
|
||||||
if (result.affectedRows === 0) {
|
|
||||||
res.status(400).json('Something went wrong denying the application');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.affectedRows == 1) {
|
|
||||||
res.sendStatus(200);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Approve failed:', err);
|
|
||||||
res.status(500).json({ error: 'Failed to deny application' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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, () => {
|
app.listen(port, () => {
|
||||||
console.log(`Example app listening on port ${port} `)
|
console.log(`Example app listening on port ${port} `)
|
||||||
|
|||||||
212
api/routes/applications.js
Normal file
212
api/routes/applications.js
Normal file
@@ -0,0 +1,212 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// DB pool (same as used in api/index.js)
|
||||||
|
const pool = require('../db');
|
||||||
|
|
||||||
|
// Keep any in-memory structures if needed (preserved from original file)
|
||||||
|
let applicationData = {
|
||||||
|
app: null,
|
||||||
|
messages: [],
|
||||||
|
status: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
// POST /application
|
||||||
|
router.post('/', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const App = req.body?.App || {};
|
||||||
|
|
||||||
|
// TODO: replace with current user ID
|
||||||
|
const memberId = 1;
|
||||||
|
|
||||||
|
const sql = `INSERT INTO applications (member_id, app_version, app_data) VALUES (?, ?, ?);`;
|
||||||
|
const appVersion = 1;
|
||||||
|
|
||||||
|
const params = [memberId, appVersion, JSON.stringify(App)]
|
||||||
|
|
||||||
|
console.log(params)
|
||||||
|
|
||||||
|
await pool.query(sql, params);
|
||||||
|
|
||||||
|
res.sendStatus(201);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Insert failed:', err);
|
||||||
|
res.status(500).json({ error: 'Failed to save application' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// GET /application/all
|
||||||
|
router.get('/all', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const sql = `SELECT
|
||||||
|
member.name AS member_name,
|
||||||
|
app.id,
|
||||||
|
app.member_id,
|
||||||
|
app.submitted_at,
|
||||||
|
app.app_status
|
||||||
|
FROM applications AS app
|
||||||
|
LEFT JOIN members AS member
|
||||||
|
ON member.id = app.member_id;`
|
||||||
|
|
||||||
|
const rows = await pool.query(sql);
|
||||||
|
|
||||||
|
res.status(200).json(rows);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// GET /application/:id
|
||||||
|
router.get('/:id', async (req, res) => {
|
||||||
|
let appID = req.params.id;
|
||||||
|
|
||||||
|
//TODO: Replace with real user Authorization and whatnot
|
||||||
|
// if the application is not "me" and I am not a recruiter, deny access to the application (return 403 or whatever)
|
||||||
|
if (appID === "me")
|
||||||
|
appID = 2;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const conn = await pool.getConnection()
|
||||||
|
|
||||||
|
const application = await conn.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.id = ?;`,
|
||||||
|
[appID]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!Array.isArray(application) || application.length === 0) {
|
||||||
|
conn.release();
|
||||||
|
return res.status(204).json("Application Not Found");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
return res.status(500).json({ error: 'Failed to load application' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /application/approve/:id
|
||||||
|
router.post('/approve/:id', async (req, res) => {
|
||||||
|
const appID = req.params.id;
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
UPDATE applications
|
||||||
|
SET approved_at = NOW()
|
||||||
|
WHERE id = ?
|
||||||
|
AND approved_at IS NULL
|
||||||
|
AND denied_at IS NULL
|
||||||
|
`;
|
||||||
|
try {
|
||||||
|
const result = await pool.execute(sql, appID);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
|
||||||
|
if (result.affectedRows === 0) {
|
||||||
|
res.status(400).json('Something went wrong approving the application');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.affectedRows == 1) {
|
||||||
|
res.sendStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Approve failed:', err);
|
||||||
|
res.status(500).json({ error: 'Failed to approve application' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /application/deny/:id
|
||||||
|
router.post('/deny/:id', async (req, res) => {
|
||||||
|
const appID = req.params.id;
|
||||||
|
|
||||||
|
const sql = `
|
||||||
|
UPDATE applications
|
||||||
|
SET denied_at = NOW()
|
||||||
|
WHERE id = ?
|
||||||
|
AND approved_at IS NULL
|
||||||
|
AND denied_at IS NULL
|
||||||
|
`;
|
||||||
|
try {
|
||||||
|
const result = await pool.execute(sql, appID);
|
||||||
|
|
||||||
|
console.log(result);
|
||||||
|
|
||||||
|
if (result.affectedRows === 0) {
|
||||||
|
res.status(400).json('Something went wrong denying the application');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.affectedRows == 1) {
|
||||||
|
res.sendStatus(200);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Approve failed:', err);
|
||||||
|
res.status(500).json({ error: 'Failed to deny application' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// POST /application/:id/comment
|
||||||
|
router.post('/: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' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
10
api/routes/ranks.js
Normal file
10
api/routes/ranks.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// Placeholder router for rank-related routes.
|
||||||
|
// Implement rank endpoints here, for example:
|
||||||
|
// router.get('/', async (req, res) => { /* ... */ });
|
||||||
|
// router.post('/change', async (req, res) => { /* ... */ });
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
||||||
Reference in New Issue
Block a user