Skip to content

Commit

Permalink
Merge branch 'dev' into 19-make-backend-routes-for-published-schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatMegamind authored Jan 3, 2024
2 parents 7e90068 + e6e02f1 commit d56deec
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 55 deletions.
13 changes: 13 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@ const publishedScheduleRouter = require('./routes/publishedSchedule');

require('dotenv').config();

// routes
const users = require('./routes/users');

const email = require('./routes/nodeMailer');

const app = express();

const catalogRouter = require('./routes/catalog');

const PORT = process.env.PORT || 3001;

app.use(
cors({
origin: `${process.env.REACT_APP_HOST}:${process.env.REACT_APP_PORT}`,
credentials: true,
}),
);

// app.use(cors({ origin: 'http://localhost:3000', credentials: true }));

// add all routes under here
app.use(express.json()); // for req.body
app.use('/published-schedule', publishedScheduleRouter);
app.use('/users', users);
app.use('/catalog', catalogRouter);
app.use('/nodeMailer', email);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
"eslint-plugin-prettier": "^4.0.0",
"express": "^4.17.1",
"express-promise-router": "^4.1.1",
"nodemailer": "^6.9.7",
"nodemon": "^2.0.14",
"pg": "^8.8.0",
"pg-promise": "^10.12.1",
"prettier": "^2.4.1"
},
"devDependencies": {
"eslint-config-prettier": "^8.3.0",
"lint-staged": "^11.2.6",
"lint-staged": "^11.2.6",
"husky": "^7.0.4"
"husky": "^7.0.4",
"lint-staged": "^11.2.6"
}
}
92 changes: 92 additions & 0 deletions routes/catalog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const express = require('express');

const { db } = require('../server/db');

const catalogRouter = express.Router();
const { keysToCamel } = require('../common/utils');

// -- GET - Returns all data from the catalog table
catalogRouter.get('/', async (req, res) => {
try {
const allInfo = await db.query(`SELECT * from catalog;`);
res.status(200).json(keysToCamel(allInfo));
} catch (err) {
res.status(500).send(err.message);
}
});

// -- GET/:id - Returns the row that matches the id
catalogRouter.get('/:id', async (req, res) => {
try {
const { id } = req.params;
const allUsers = await db.query(`SELECT * FROM catalog WHERE id = $1;`, [id]);
res.status(200).json(keysToCamel(allUsers));
} catch (err) {
res.status(500).send(err.message);
}
});

// -- POST - Adds a new row to the catalog table
catalogRouter.post('/', async (req, res) => {
const { id, host, title, eventType, subject, description, year } = req.body;
try {
await db.query(
`INSERT INTO catalog (id, host, title, event_type, subject, description, year)
VALUES ($1, $2, $3, $4, $5, $6, $7);`,
[id, host, title, eventType, subject, description, year],
);
res.status(201).json({ id, status: 'Success' });
} catch (err) {
res.status(500).json({
status: 'Failed',
msg: err.message,
});
}
});

// -- PUT - Updates an existing row given an id
// -- All fields are optional
catalogRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { host, title, eventType, subject, description, year } = req.body;

const updatedCatalog = await db.query(
`UPDATE catalog SET
${host ? 'host = $(host), ' : ''}
${title ? 'title = $(title),' : ''}
${eventType ? 'event_type = $(eventType), ' : ''}
${subject ? 'subject = $(subject), ' : ''}
${description ? 'description = $(description), ' : ''}
${year ? 'year = $(year), ' : ''}
id = '${id}'
WHERE id = '${id}'
RETURNING *;`,
{
host,
title,
eventType,
subject,
description,
year,
id,
},
);
res.status(200).send(keysToCamel(updatedCatalog));
} catch (err) {
res.status(500).send(err.message);
}
});

// -- DELETE - deletes an existing row given an id
catalogRouter.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
const delUser = await db.query(`DELETE FROM catalog WHERE id = $1 RETURNING *;`, [id]);
res.status(200).send(keysToCamel(delUser));
} catch (err) {
res.status(500).send(err.message);
}
});

module.exports = catalogRouter;
37 changes: 37 additions & 0 deletions routes/nodeMailer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const express = require('express');
// const cors = require('cors');
const transporter = require('../transporter');
// TODO: add verifyToken

const emailRouter = express();
// emailRouter.use(
// cors({
// origin: 'http://localhost:3000',
// methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
// credentials: true,
// }),
// );

emailRouter.use(express.json());

emailRouter.post('/send', (req, res) => {
const { email, messageHtml, subject } = req.body;
console.log('req.body', req.body);
console.log('email', email);
const mail = {
from: `${process.env.REACT_APP_EMAIL_FIRST_NAME} ${process.env.REACT_APP_EMAIL_LAST_NAME} ${process.env.REACT_APP_EMAIL_USERNAME}`,
to: email,
subject,
html: messageHtml,
};

transporter.sendMail(mail, (err) => {
if (err) {
res.status(500).send(`Transporter Error: ${err}`);
} else {
res.status(200).send('Transporter Backend Successfully Sent');
}
});
});

module.exports = emailRouter;
68 changes: 68 additions & 0 deletions routes/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const express = require('express');
const { keysToCamel } = require('../common/utils');
const { db } = require('../server/db');

const userRouter = express.Router();

userRouter.get('/', async (req, res) => {
try {
const allUsers = await db.query(`SELECT * FROM users;`);
res.status(200).json(keysToCamel(allUsers));
} catch (err) {
res.status(500).send(err.message);
}
});

userRouter.get('/pending-accounts', async (req, res) => {
try {
const pendingAccounts = await db.query(`SELECT * FROM users WHERE approved = FALSE;`);
res.status(200).json(keysToCamel(pendingAccounts));
} catch (err) {
res.status(500).send(err.message);
}
});

userRouter.post('/', async (req, res) => {
try {
const { id, email, type, approved } = req.body;
await db.query(`INSERT INTO users (id, email, "type", approved) VALUES ($1, $2, $3, $4);`, [
id,
email,
type,
approved,
]);
res.status(201).json({
id,
});
} catch (err) {
res.status(500).json({
status: 'Failed',
msg: err.message,
});
}
});

userRouter.put('/:uid', async (req, res) => {
try {
const { uid } = req.params;
const updatedApproval = await db.query(
`UPDATE users SET approved = TRUE WHERE id = $1 RETURNING *;`,
[uid],
);
return res.status(200).send(keysToCamel(updatedApproval));
} catch (err) {
return res.status(500).send(err.message);
}
});

userRouter.delete('/:uid', async (req, res) => {
try {
const { uid } = req.params;
const deletedUser = await db.query(`DELETE FROM users WHERE id = $1 RETURNING *;`, [uid]);
res.status(200).send(keysToCamel(deletedUser));
} catch (err) {
res.status(500).send(err.message);
}
});

module.exports = userRouter;
22 changes: 0 additions & 22 deletions server/queries/catalog.sql

This file was deleted.

29 changes: 0 additions & 29 deletions server/queries/users.sql

This file was deleted.

18 changes: 18 additions & 0 deletions transporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const nodemailer = require('nodemailer');

require('dotenv').config();

// sender information
const transport = {
host: 'smtp.gmail.com', // e.g. smtp.gmail.com
auth: {
user: process.env.REACT_APP_EMAIL_USERNAME,
pass: process.env.REACT_APP_EMAIL_PASSWORD,
},
from: process.env.REACT_APP_EMAIL_USERNAME,
secure: true,
};

const transporter = nodemailer.createTransport(transport);

module.exports = transporter;
Loading

0 comments on commit d56deec

Please sign in to comment.