generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge dev into main #33
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d560285
created catalog table
8b92d59
AT - created the published schedule table
arralia a499026
fixed enum name
arralia 1155340
removed brackets around exist conditionals
arralia 0e83312
added create table users statement
e2e0b73
added drop table if exists
19035e2
updated drop table statement
d68a4d2
Made CRUD SQL queries for Published Schedule Table
SubinQKim 847c958
added catalog CRUD queries
chloecheng8 589e537
Attempt to figure out ENV variable issue with workflows
leeaj8-uci 9f350b0
fix typo
leeaj8-uci 559dab7
try moving env variables
leeaj8-uci f810b9d
Revert changes
leeaj8-uci 34eff36
Merge pull request #14 from ctc-uci/8-create-catalog-table
ThatMegamind 80ac134
Merge pull request #15 from ctc-uci/7-create-published-schedule-table
ThatMegamind fe99a58
Merge pull request #16 from ctc-uci/9-create-user-table
ThatMegamind 5ad36f1
Merge pull request #17 from ctc-uci/10-make-crud-sql-queries-for-publ…
ThatMegamind 32a94b0
Merge pull request #18 from ctc-uci/11-make-crud-sql-queries-for-cata…
ThatMegamind c042554
Make CRUD SQL queries for Users Table (#25)
ctc-devops 57cc996
fixed published schedule types
michellelin1 8c703e9
added connection to db
michellelin1 15d3d25
23-make-backend-routes-for-users (#26)
ctc-devops a2965f7
Make backend routes for Catelog (#27)
ctc-devops e6e02f1
Set up the nodeMailer route and the transporter (#28)
ctc-devops 699f437
Make Backend Routes for Published Schedule (#29)
ctc-devops 39cccb0
Minor DB Updates (#32)
ctc-devops File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,36 @@ | ||
const express = require('express'); | ||
const cors = require('cors'); | ||
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}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// toCamel, isArray, and isObject are helper functions used within utils only | ||
const toCamel = (s) => { | ||
return s.replace(/([-_][a-z])/g, ($1) => { | ||
return $1.toUpperCase().replace('-', '').replace('_', ''); | ||
}); | ||
}; | ||
|
||
const isArray = (a) => { | ||
return Array.isArray(a); | ||
}; | ||
|
||
const isISODate = (str) => { | ||
try { | ||
const ISOString = str.toISOString(); | ||
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(ISOString)) return false; | ||
const d = new Date(ISOString); | ||
return d.toISOString() === ISOString; | ||
} catch (err) { | ||
return false; | ||
} | ||
}; | ||
|
||
const isObject = (o) => { | ||
return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o); | ||
}; | ||
|
||
// Database columns are in snake case. JavaScript is suppose to be in camel case | ||
// This function converts the keys from the sql query to camel case so it follows JavaScript conventions | ||
const keysToCamel = (data) => { | ||
if (isObject(data)) { | ||
const newData = {}; | ||
Object.keys(data).forEach((key) => { | ||
newData[toCamel(key)] = keysToCamel(data[key]); | ||
}); | ||
return newData; | ||
} | ||
if (isArray(data)) { | ||
return data.map((i) => { | ||
return keysToCamel(i); | ||
}); | ||
} | ||
if ( | ||
typeof data === 'string' && | ||
data.length > 0 && | ||
data[0] === '{' && | ||
data[data.length - 1] === '}' | ||
) { | ||
let parsedList = data.replaceAll('"', ''); | ||
parsedList = parsedList.slice(1, parsedList.length - 1).split(','); | ||
return parsedList; | ||
} | ||
return data; | ||
}; | ||
|
||
module.exports = { keysToCamel }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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 { host, title, eventType, subject, description, year } = req.body; | ||
try { | ||
const returnedData = await db.query( | ||
`INSERT INTO catalog (id, host, title, event_type, subject, description, year) | ||
VALUES (nextval('catalog_id_seq'), $1, $2, $3, $4, $5, $6) | ||
RETURNING id;`, | ||
[host, title, eventType, subject, description, year], | ||
); | ||
res.status(201).json({ id: returnedData[0].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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unexpected console statement.