Created
November 1, 2022 17:07
-
-
Save alanboy/6440be92bd27870388287ec9e5b8e2f9 to your computer and use it in GitHub Desktop.
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
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