|
| 1 | +const AWS = require('aws-sdk'); |
| 2 | + |
| 3 | +const { |
| 4 | + AWS_ACCESS_KEY_ID, |
| 5 | + AWS_SECRET_ACCESS_KEY, |
| 6 | + AWS_REGION |
| 7 | +} = process.env; |
| 8 | + |
| 9 | +AWS.config.update({ |
| 10 | + accessKeyId: AWS_ACCESS_KEY_ID, |
| 11 | + secretAccessKey: AWS_SECRET_ACCESS_KEY, |
| 12 | + region: AWS_REGION, |
| 13 | + endpoint: `https://dynamodb.${AWS_REGION}.amazonaws.com`, |
| 14 | +}); |
| 15 | + |
| 16 | +const dynamoDB = new AWS.DynamoDB(); |
| 17 | +const docClient = new AWS.DynamoDB.DocumentClient(); |
| 18 | + |
| 19 | +function validatedBody(body, ...fields) { |
| 20 | + for (const field of fields) { |
| 21 | + if (!Object.prototype.hasOwnProperty.call(body, field)) { |
| 22 | + throw new Error(`Missing request body parameter: ${field}.`); |
| 23 | + } |
| 24 | + } |
| 25 | + return body; |
| 26 | +} |
| 27 | + |
| 28 | +const TableName = 'Drivers'; |
| 29 | +const PrimaryKey = 'driverID'; |
| 30 | + |
| 31 | +exports.createDriversTable = async () => { |
| 32 | + return dynamoDB.createTable({ |
| 33 | + TableName, |
| 34 | + KeySchema: [{ |
| 35 | + AttributeName: PrimaryKey, |
| 36 | + KeyType: 'HASH', |
| 37 | + }], |
| 38 | + AttributeDefinitions: [{ |
| 39 | + AttributeName: PrimaryKey, |
| 40 | + AttributeType: 'S', |
| 41 | + }], |
| 42 | + ProvisionedThroughput: { |
| 43 | + ReadCapacityUnits: 10, |
| 44 | + WriteCapacityUnits: 10, |
| 45 | + } |
| 46 | + }).promise(); |
| 47 | +}; |
| 48 | + |
| 49 | +exports.createDriver = async (body) => { |
| 50 | + const { |
| 51 | + driverID, |
| 52 | + rideStatus, |
| 53 | + lastLocation |
| 54 | + } = validatedBody(body, 'driverID', 'rideStatus', 'lastLocation'); |
| 55 | + |
| 56 | + return docClient.put({ |
| 57 | + TableName, |
| 58 | + Item: { |
| 59 | + driverID, |
| 60 | + rideStatus, |
| 61 | + lastLocation, |
| 62 | + } |
| 63 | + }).promise(); |
| 64 | +}; |
| 65 | + |
| 66 | +exports.readDriver = async (body) => { |
| 67 | + const { driverID } = validatedBody(body, 'driverID'); |
| 68 | + return docClient.get({ TableName, Key: { driverID } }).promise(); |
| 69 | +}; |
| 70 | + |
| 71 | +exports.updateDriver = async (body) => { |
| 72 | + const { |
| 73 | + driverID, |
| 74 | + rideStatus, |
| 75 | + lastLocation |
| 76 | + } = validatedBody(body, 'driverID', 'rideStatus', 'lastLocation'); |
| 77 | + |
| 78 | + return docClient.update({ |
| 79 | + TableName, |
| 80 | + Key: { driverID }, |
| 81 | + UpdateExpression: 'SET #loc.#lon = :lonVal, #loc.#lat = :latVal, #rideStatus= :r', |
| 82 | + ExpressionAttributeNames: { |
| 83 | + '#loc': 'lastLocation', |
| 84 | + '#lon': 'longitude', |
| 85 | + '#lat': 'latitude', |
| 86 | + '#rideStatus': 'rideStatus', |
| 87 | + }, |
| 88 | + ExpressionAttributeValues: { |
| 89 | + ':r': rideStatus, |
| 90 | + ':lonVal': lastLocation.longitude, |
| 91 | + ':latVal': lastLocation.latitude, |
| 92 | + }, |
| 93 | + ReturnValues: 'UPDATED_NEW' |
| 94 | + }).promise(); |
| 95 | +}; |
| 96 | + |
| 97 | +exports.deleteDriver = async (body) => { |
| 98 | + const { driverID } = validatedBody(body, 'driverID'); |
| 99 | + return docClient.delete({ TableName, Key: { driverID } }).promise(); |
| 100 | +}; |
0 commit comments