From d8c985b8a32c326d925bd2d65d7c508b824a6a5c Mon Sep 17 00:00:00 2001 From: LiamLiden Date: Sat, 16 Jun 2018 14:15:49 -0500 Subject: [PATCH 1/3] Implemented SEARCH. Must Test --- api/tasks.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/api/tasks.js b/api/tasks.js index 7e0d071..11845d7 100644 --- a/api/tasks.js +++ b/api/tasks.js @@ -139,6 +139,68 @@ module.exports = Router() }) ) + /** + * @swagger + * /tasks/{taskId}: + * search: + * summary: Get a single task that is requrested via Id + * parameters: + * - name: taskId + * in: path + * description: The unique ID of a task. + * schema: + * type: string + * format: objectId + * responses: + * 200: + * description: A task + * headers: + * Link: + * schema: + * type: string + * description: > + * Standard HTTP Link header. All URIs relative to the /tasks endpoint. + * content: + * application/json: + * schema: + * $ref: "#/components/schemas/Task" + * 400: + * $ref: "#/components/responses/BadRequest" + * 404: + * $ref: "#/components/responses/NotFound" + */ + .search("/:taskId", (request, response) => + runWithDB(async db => { + validateRequest(request, { + paramSchemaProps: { + taskId: { type: "string", format: "objectId" } + } + }) + + const tasksCollection = db.collection("tasks") + + let taskIdValue = null + if (taskId) { + try { + taskIdValue = base64ToId(taskId) + } catch (error) { + throw new HTTPError(400, 'Invalid request: Path Params.taskId does not match the format "objectId"') + } + } + + const { taskId } = request.params + const searchResult = await tasksCollection.findOne( + { _id: new ObjectId(taskId) } + ) + + if (!searchResult.value) { + throw new HTTPError(404, `No task with id "${taskId}"`) + } else { + response.status(200).send(searchResult) + } + }) + ) + /** * @swagger * /tasks/{taskId}: From fc140c4a37f11fc8d70ef00036fa2f863383be69 Mon Sep 17 00:00:00 2001 From: LiamLiden Date: Sat, 16 Jun 2018 14:45:00 -0500 Subject: [PATCH 2/3] pacing changes --- api/tasks.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/api/tasks.js b/api/tasks.js index 11845d7..acc454a 100644 --- a/api/tasks.js +++ b/api/tasks.js @@ -143,7 +143,7 @@ module.exports = Router() * @swagger * /tasks/{taskId}: * search: - * summary: Get a single task that is requrested via Id + * summary: Get a single task that is requested via Id * parameters: * - name: taskId * in: path @@ -154,16 +154,10 @@ module.exports = Router() * responses: * 200: * description: A task - * headers: - * Link: - * schema: - * type: string - * description: > - * Standard HTTP Link header. All URIs relative to the /tasks endpoint. * content: * application/json: * schema: - * $ref: "#/components/schemas/Task" + * $ref: "#/components/schemas/Task" * 400: * $ref: "#/components/responses/BadRequest" * 404: From 418d273624f7bdb79666c2e72d89dc1fccbd48f0 Mon Sep 17 00:00:00 2001 From: LiamLiden Date: Sat, 16 Jun 2018 15:41:43 -0500 Subject: [PATCH 3/3] Changed all SEARCH to GET and fixed error. Tested and all responses work --- api/tasks.js | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/api/tasks.js b/api/tasks.js index acc454a..f7296fb 100644 --- a/api/tasks.js +++ b/api/tasks.js @@ -142,7 +142,7 @@ module.exports = Router() /** * @swagger * /tasks/{taskId}: - * search: + * get: * summary: Get a single task that is requested via Id * parameters: * - name: taskId @@ -163,7 +163,7 @@ module.exports = Router() * 404: * $ref: "#/components/responses/NotFound" */ - .search("/:taskId", (request, response) => + .get("/:taskId", (request, response) => runWithDB(async db => { validateRequest(request, { paramSchemaProps: { @@ -173,21 +173,12 @@ module.exports = Router() const tasksCollection = db.collection("tasks") - let taskIdValue = null - if (taskId) { - try { - taskIdValue = base64ToId(taskId) - } catch (error) { - throw new HTTPError(400, 'Invalid request: Path Params.taskId does not match the format "objectId"') - } - } - const { taskId } = request.params const searchResult = await tasksCollection.findOne( { _id: new ObjectId(taskId) } ) - if (!searchResult.value) { + if (!searchResult) { throw new HTTPError(404, `No task with id "${taskId}"`) } else { response.status(200).send(searchResult)