diff --git a/handlers/courses/get.js b/handlers/courses/get.js new file mode 100644 index 0000000..500157c --- /dev/null +++ b/handlers/courses/get.js @@ -0,0 +1,15 @@ +const {createResponse} = require('../../helpers'); +const db = require('../../services/db'); + +module.exports.default = async () => { + try { + const courses = await db.get('eg_courses', {}); + + return createResponse(200, { + courses: courses + }) + } catch (err) { + const statusCode = (err && err.statusCode) || 500; + return createResponse(statusCode, err.message || err); + } +}; diff --git a/handlers/hello/get.js b/handlers/hello/get.js deleted file mode 100644 index ed6be6c..0000000 --- a/handlers/hello/get.js +++ /dev/null @@ -1,14 +0,0 @@ -'use strict'; - -module.exports.hello = async (event, context) => { - return { - statusCode: 200, - body: JSON.stringify({ - message: 'Go Serverless v1.0! Your function executed successfully!', - input: event, - }), - }; - - // Use this code if you don't use the http event with the LAMBDA-PROXY integration - // return { message: 'Go Serverless v1.0! Your function executed successfully!', event }; -}; diff --git a/serverless.yml b/serverless.yml index bde8879..f5f4420 100644 --- a/serverless.yml +++ b/serverless.yml @@ -57,11 +57,12 @@ provider: functions: hello: - handler: handlers/hello/get.hello + handler: handlers/courses/get.default events: - http: - path: hello + path: courses method: get + cors: true # The following are a few example events you can configure # NOTE: Please make sure to change your handler code to work with those events @@ -107,6 +108,20 @@ functions: # NewOutput: # Description: "Description for the output" # Value: "Some output value" +resources: + coursesTable: + Type: AWS::DynamoDB::Table + Properties: + TableName: eg_courses + AttributeDefinitions: + - AttributeName: name + AttributeType: s + KeySchema: + - AttributeName: name + KeyType: HASH + Tags: + - Key: app + Value: ecs-grader plugins: - serverless-offline \ No newline at end of file diff --git a/services/db.js b/services/db.js index 541125e..a398104 100644 --- a/services/db.js +++ b/services/db.js @@ -25,7 +25,7 @@ const queryByID = async (tableName, idKeyName, id) => { // querying by id can be used when we are querying a table // that has a sortKey, but we don't need to use the sortKey const res = await dynamoClient.query({ - TableName: "gt_answers", + TableName: tableName, KeyConditionExpression: "#id = :idValue", ExpressionAttributeNames: { "#id": idKeyName, diff --git a/validators/schemas.js b/validators/schemas.js index 7f7402f..1b73ca3 100644 --- a/validators/schemas.js +++ b/validators/schemas.js @@ -1,29 +1,12 @@ -const answerSchema = { - "type": "object", - "properties": { - "user_id": {"type": "string"}, - "question_id": {"type": "string"}, - "is_correct": {"type": "boolean"} - }, - "required": ["user_id", "question_id", "is_correct"] -}; - -const userSchema = { - "type": "object", - "properties": { - "id": {"type": "string"}, - "score": {"type": "number"} - }, - "required": ["user_id", "question_id", "is_correct"] -}; - -const loginFollowUpSchema = { - "type": "object", - "properties": { - "accessToken": {"type": "string"} - }, - "required": ["accessToken"] -}; +// const answerSchema = { +// "type": "object", +// "properties": { +// "user_id": {"type": "string"}, +// "question_id": {"type": "string"}, +// "is_correct": {"type": "boolean"} +// }, +// "required": ["user_id", "question_id", "is_correct"] +// }; module.exports = { answerSchema,