Skip to content

Instantly share code, notes, and snippets.

@alanboy
Created November 1, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanboy/6440be92bd27870388287ec9e5b8e2f9 to your computer and use it in GitHub Desktop.
Save alanboy/6440be92bd27870388287ec9e5b8e2f9 to your computer and use it in GitHub Desktop.
const { User, sequelize } = require('../models')
async function usersHandler(event, context) {
const { routeKey } = event
const userId = event?.pathParameters?.id
switch (routeKey) {
case 'GET /api/users':
return {
statusCode: 200,
body: JSON.stringify(await getAllUsers()),
}
case 'GET /api/users/{id}':
return {
statusCode: 200,
body: JSON.stringify(await getUser(userId)),
}
case 'POST /api/users':
return {
statusCode: 200,
body: JSON.stringify(await createUser(event, context)),
}
default:
return {
statusCode: 404,
body: JSON.stringify({
message: 'Not found',
}),
}
}
}
async function getUser(id) {
await sequelize.sync();
return await User.findByPk(id)
}
async function getAllUsers(context) {
await sequelize.sync();
let organizationId = 1
return await User.findAll({ where: { organizationId } })
}
async function createUser(event, context) {
await sequelize.sync();
const { firstName, lastName, email, role, organizationId } = JSON.parse(
event.body
)
return await User.create({
firstName,
lastName,
email,
role,
organizationId,
})
}
exports.getUser = getUser
exports.getAllUsers = getAllUsers
exports.createUser = createUser
exports.usersHandler = usersHandler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment