Skip to content

Commit

Permalink
Updated failing tests and commented some code
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryConnollyLIT committed Aug 7, 2019
1 parent 81d45b4 commit ac2b4cc
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 490 deletions.
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ Followed by routes such as:
/fetch/allByPartitionKey
/fetch/ByPartitionAndSortKeys
/fetch/ByPartitionAndActivity
/fetch/allByActivity
/fetch/allByLocation
/remove
/pushCollarData

Expand All @@ -65,8 +63,6 @@ Followed by routes such as:
/fetch/allByPartitionKey?partitionKey=abc3
/fetch/ByPartitionAndSortKeys?partitionKey=abc1&sortKey=1
/fetch/ByPartitionAndActivity?barking=low
/fetch/allByActivity?activity=low
/fetch/allByLocation?location=37901
/remove?partitionKey=abc3&sortKey=11

The following route requires a request object:
Expand All @@ -91,9 +87,8 @@ http://ec2-52-91-239-59.compute-1.amazonaws.com:3000/api/fetch/ByPartitionAndSor

http://ec2-52-91-239-59.compute-1.amazonaws.com:3000/api/fetch/ByPartitionAndActivity?barking=low

http://ec2-52-91-239-59.compute-1.amazonaws.com:3000/api/allByLocation?location=37901

http://ec2-52-91-239-59.compute-1.amazonaws.com:3000/api/fetch/allByActivity?activity=low


----------
## Running Locally
Expand Down Expand Up @@ -131,15 +126,6 @@ http://localhost:3000/api/fetch/ByPartitionAndActivity?barking=low
Returns all responses with a specific level of barking
Takes 'barking' as a query param

http://localhost:3000/api/fetch/allByActivity?activity=low

Returns all responses with a specific level of activity
Takes 'activity' as a query param

http://localhost:3000/api/fetch/allByLocation?location=37901

Returns all responses within a specific zipcode
Takes 'location' as a query param

http://localhost:3000/api/fetch/ByPartitionAndSortKeys?partitionKey=abc1&sortKey=1

Expand Down
40 changes: 38 additions & 2 deletions controllers/docClientController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const AWS = require('aws-sdk');
const handleResponse = require('./handleResponse');

// A function to create a random sort key and appends the activity type on the end
function createSortKey(activityType) {
let randomNum = '';
const characters = '0123456789';
Expand All @@ -12,12 +13,14 @@ function createSortKey(activityType) {
return sortKey;
}

// Function that controls the Get, Post and Delete operations on the db
function docClientController() {
const docClient = new AWS.DynamoDB.DocumentClient();
const table = 'code-challenge-203';
const handle = handleResponse();
const queryObj = {};

// Retrieves a specific db enrty based on Partiton and Sort keys
function get(req, res) {
if (req.query.partitionKey && req.query.sortKey) {
queryObj.partitionKey = req.query.partitionKey;
Expand All @@ -40,12 +43,45 @@ function docClientController() {
});
} else {
res.json({
message: 'Partition Key and Sort Key params required, i.e. partitionKey=abc2&sortKey=3',
message: 'Partition Key and Sort Key params required',
statusCode: 400
});
}
}

// TODO - Flesh out further querying routes
// function query(req, res) {
// if (req.body.partitionKey && req.query.bottom && req.query.top) {
// const params = {
// TableName: table,
// KeyConditionExpression: '#pk = :key and #d between :top and :bottom',
// ExpressionAttributeNames: {
// '#pk': 'partitionKey',
// '#d': 'duration'
// },
// ExpressionAttributeValues: {
// ':key': req.body.partitionKey,
// ':bottom': req.query.bottom,
// ':top': req.query.top
// }
// };

// docClient.query(params, (err, data) => {
// if (err) {
// handle.handleError(err, res);
// } else {
// handle.handleSuccess(data.Item, res);
// }
// });
// } else {
// res.json({
// message: 'Activity Type and Duration params Range Required',
// statusCode: 400
// });
// }
// }

// Function that checks the requst object, adds timestamp and sort key and posts it to the db
function post(req, res) {
const currentDate = new Date().toISOString();

Expand Down Expand Up @@ -92,6 +128,7 @@ function docClientController() {
}
}

// A function that removes a specific db entry bases on Partition and Sort Keyss
function remove(req, res) {
if (req.query.partitionKey && req.query.sortKey) {
queryObj.partitionKey = req.query.partitionKey;
Expand All @@ -105,7 +142,6 @@ function docClientController() {
},
};

// TODO do proper codes for delete 204
docClient.delete(params, (err, data) => {
if (err) {
handle.handleError(err, res);
Expand Down
1 change: 1 addition & 0 deletions controllers/handleResponse.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Functions that deal with the db responses
function handleResponse() {
function handleSuccess(data, res) {
res.json({
Expand Down
48 changes: 6 additions & 42 deletions controllers/scanController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const AWS = require('aws-sdk');
const handleResponse = require('./handleResponse');

// a controller function for all the scan actions
function scanController() {
const docClient = new AWS.DynamoDB.DocumentClient();
const table = 'code-challenge-203';
const handle = handleResponse();
const queryObj = {};

// a function returns everything in the table
function get(req, res) {
const params = {
TableName: table,
Expand All @@ -19,6 +21,7 @@ function scanController() {
}
});
}
// A function that returns everything in the table with the specified Activity Type
function getAllByActivityType(req, res) {
if (req.query.activityType
&& (req.query.activityType === 'BARK'
Expand All @@ -42,6 +45,7 @@ function scanController() {
});
}
}
// A function that returns everything that matches the supplied Partition Key and Activity Type
function getByPartitionAndActivity(req, res) {
if ((req.query.partitionKey && req.query.activityType) && (req.query.activityType === 'BARK'
|| req.query.activityType === 'PHYSICAL_ACTIVITY'
Expand Down Expand Up @@ -69,47 +73,7 @@ function scanController() {
});
}
}
function getByActivity(req, res) {
if (req.query.activity) {
queryObj.activity = req.query.activity;
const params = {
TableName: table,
ExpressionAttributeValues: { ':a': queryObj.activity },
FilterExpression: 'activity = :a'
};

docClient.scan(params, (err, data) => {
if (err) handle.handleError(err, res);
else handle.handleSuccess(data.Items, res);
});
} else {
res.json({
message: 'Activity value required: i.e. activity=low',
statusCode: 400
});
}
}
function getByLocation(req, res) {
if (req.query.location) {
queryObj.location = req.query.location;
const params = {
TableName: table,
ExpressionAttributeNames: { '#location': 'location' },
ExpressionAttributeValues: { ':l': queryObj.location },
FilterExpression: '#location = :l'
};

docClient.scan(params, (err, data) => {
if (err) handle.handleError(err, res);
else handle.handleSuccess(data.Items, res);
});
} else {
res.json({
message: 'Location (zipcode) value required: i.e. location=37901',
statusCode: 400
});
}
}
// A function that returns everything in a specific partition
function getByCollar(req, res) {
if (req.query.partitionKey) {
queryObj.partitionKey = req.query.partitionKey;
Expand All @@ -131,7 +95,7 @@ function scanController() {
}
}
return {
get, getAllByActivityType, getByPartitionAndActivity, getByActivity, getByLocation, getByCollar
get, getAllByActivityType, getByPartitionAndActivity, getByCollar
};
}

Expand Down
108 changes: 16 additions & 92 deletions documentation/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,94 +226,15 @@
}
}
},
"/fetch/allByActivity": {
"get": {
"tags": [
"Dog Collar Requests"
],
"summary": "Gets a specific Dog Collar Responses from the DB",
"parameters": [
{
"in": "query",
"name": "Values",
"required": true,
"description": "Cat with id",
"schema": {
"type": "object",
"properties": {
"activity": {
"type": "string",
"enum": [
"low",
"medium",
"high"
]
}
},
"example": {
"activity": "high"
}
},
"style": "form",
"explode": "true"
}
],
"responses": {
"200": {
"description": "success"
},
"404": {
"description": "Failed. Cat not found."
}
}
}
},
"/fetch/allBylocation": {
"get": {
"tags": [
"Dog Collar Requests"
],
"summary": "Gets a specific Dog Collar Responses from the DB",
"parameters": [
{
"in": "query",
"name": "Values",
"required": true,
"description": "Cat with id",
"schema": {
"type": "object",
"properties": {
"location": {
"type": "string"
}
},
"example": {
"location": "90210"
}
},
"style": "form",
"explode": "true"
}
],
"responses": {
"200": {
"description": "success"
},
"404": {
"description": "Failed. Cat not found."
}
}
}
},
"/pushCollarData": {
"post": {
"tags": [
"Dog Collar Requests"
],
"summary": "Gets a specific Dog Collar Responses from the DB",
"summary": "Posts a Dog Collar Response to the DB",
"requestBody": {
"required": true,
"description": "Cat with id",
"description": "Requires a request body in the following format:",
"content": {
"application/json": {
"schema": {
Expand Down Expand Up @@ -355,7 +276,7 @@
"explode": "true"
},
"responses": {
"200": {
"202": {
"description": "success"
},
"400": {
Expand All @@ -372,13 +293,13 @@
"tags": [
"Dog Collar Requests"
],
"summary": "Gets a specific Dog Collar Responses from the DB",
"summary": "Removes a specific Dog Collar Responses from the DB",
"parameters": [
{
"in": "query",
"name": "Values",
"required": true,
"description": "Cat with id",
"description": "Requires a Partition and Sort key:",
"schema": {
"type": "object",
"properties": {
Expand All @@ -390,21 +311,24 @@
}
},
"example": {
"partitionKey": "abc1",
"sortKey": "20"
"partitionKey": "12345-12345-1ab2cd3",
"sortKey": "7530742165588_BARK"
}
},
"style": "form",
"explode": "true"
}
],
"responses": {
"200": {
"description": "success"
},
"404": {
"description": "Failed. Cat not found."
}
"200": {
"description": "success"
},
"400": {
"description": "valid query parameter required"
},
"500": {
"description": "server side error"
}
}
}
}
Expand Down
Loading

0 comments on commit ac2b4cc

Please sign in to comment.