generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
238cfea
commit 84aff04
Showing
6 changed files
with
187 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const isNumeric = (value, errorMessage) => { | ||
if (!/^\d+$/.test(value)) { | ||
throw new Error(errorMessage); | ||
} | ||
}; | ||
|
||
const isBoolean = (value, errorMessage) => { | ||
if (![true, false, 'true', 'false'].includes(value)) { | ||
throw new Error(errorMessage); | ||
} | ||
}; | ||
|
||
const isZipCode = (value, errorMessage) => { | ||
if (!/(^\d{5}$)|(^\d{5}-\d{4}$)/.test(value)) { | ||
throw new Error(errorMessage); | ||
} | ||
}; | ||
|
||
const isAlphaNumeric = (value, errorMessage) => { | ||
if (!/^[0-9a-zA-Z]+$/.test(value)) { | ||
throw new Error(errorMessage); | ||
} | ||
}; | ||
|
||
const isPhoneNumber = (value, errorMessage) => { | ||
if (!/^\d+$/.test(value) || value.length > 15) { | ||
throw new Error(errorMessage); | ||
} | ||
}; | ||
|
||
// 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 = { | ||
isNumeric, | ||
isBoolean, | ||
isZipCode, | ||
isAlphaNumeric, | ||
isPhoneNumber, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const express = require('express'); | ||
const { keysToCamel } = require('../common/utils'); | ||
const { db } = require('../server/db'); | ||
|
||
const gameRouter = express.Router(); | ||
|
||
gameRouter.get('/rps/:memberId', async (req, res) => { | ||
try { | ||
const { memberId } = req.params | ||
const [{move}] = await db.query(`SELECT move FROM rock_paper_scissors WHERE member_id = $1 LIMIT 1;`, [memberId]); | ||
res.status(200).json({ memberId, move }); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
gameRouter.get('/hangman/:memberId', async (req, res) => { | ||
try { | ||
const { memberId } = req.params | ||
const [{phrase}] = await db.query(`SELECT phrase FROM hangman_phrases WHERE member_id = $1 LIMIT 1;`, [memberId]); | ||
res.status(200).json({ memberId, phrase }); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
gameRouter.get('/truthslies/:memberId', async (req, res) => { | ||
try { | ||
const { memberId } = req.params | ||
const truths = await db.query(`SELECT truth FROM truths WHERE member_id = $1 LIMIT 2;`, [memberId]); | ||
const [{lie}] = await db.query(`SELECT lie FROM lies WHERE member_id = $1 LIMIT 1;`, [memberId]); | ||
res.status(200).json({ | ||
truths: truths.map(({truth}) => (truth)), | ||
lie | ||
}); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
|
||
module.exports = gameRouter; |
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,40 @@ | ||
const express = require('express'); | ||
const { keysToCamel } = require('../common/utils'); | ||
const { db } = require('../server/db'); | ||
|
||
const memberRouter = express.Router(); | ||
|
||
memberRouter.get('/', async (req, res) => { | ||
try { | ||
const basicUserInfo = await db.query(`SELECT id, member_name, member_year, project FROM members;`); | ||
res.status(200).json(keysToCamel(basicUserInfo)); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
|
||
memberRouter.get('/ids', async (req, res) => { | ||
try { | ||
const ids = await db.query(`SELECT id FROM members;`); | ||
res.status(200).json(ids.map(({id})=>(id))); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
memberRouter.get('/:member_id', async (req, res) => { | ||
try { | ||
const { member_id } = req.params; | ||
const memberInfo = await db.query(`SELECT * FROM members WHERE id = $1;`, [member_id]); | ||
res.status(200).json(keysToCamel(memberInfo)); | ||
} catch (err) { | ||
console.log(err); | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
|
||
module.exports = memberRouter; |
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