From 181dc5ea63ae7ca17e3bdef1bb227a2c96f539a6 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 22 Sep 2017 22:30:00 -0500 Subject: [PATCH] "Mark Prayed" works --- src/api/db/request.js | 125 +++++++++--------- src/api/routes/request.js | 8 +- src/app/src/api/index.js | 7 +- src/app/src/components/Dashboard.vue | 2 +- src/app/src/components/request/NewRequest.vue | 5 +- .../components/request/RequestListItem.vue | 10 +- src/app/src/store/action-types.js | 4 +- src/app/src/store/index.js | 51 ++++--- src/app/src/store/mutation-types.js | 2 + 9 files changed, 127 insertions(+), 87 deletions(-) diff --git a/src/api/db/request.js b/src/api/db/request.js index f8210d6..9fe683f 100644 --- a/src/api/db/request.js +++ b/src/api/db/request.js @@ -32,59 +32,16 @@ const requestNotFound = { export default function (pool) { return { /** - * Get the current requests for a user (i.e., their complete current journal) - * @param {string} userId The Id of the user - * @return The requests that make up the current journal + * Add a history entry for this request + * @param {string} requestId The Id of the request + * @param {string} status The status for this history entry + * @param {string} updateText The updated text for the request (pass blank if no update) */ - journal: async userId => (await pool.query(`${journalSql} ORDER BY "asOf" DESC`, [ userId ])).rows, - - /** - * Get the least-recently-updated prayer request for the given user - * @param {string} userId The Id of the current user - * @return The least-recently-updated request for the given user - */ - oldest: async userId => (await pool.query(`${journalSql} ORDER BY "asOf" LIMIT 1`, [ userId ])).rows[0], - - /** - * Get the "current" version of a request by its Id - * @param {string} requestId The Id of the request to retrieve - * @param {string} userId The Id of the user to which the request belongs - * @return The request, or a request-like object indicating that the request was not found - */ - byId: async (userId, requestId) => { - const reqs = await pool.query(`${currentRequestSql} - WHERE "requestId" = $1 - AND "userId" = $2 - GROUP BY request."requestId"`, - [ requestId, userId ]) - return (0 < req.rowCount) ? reqs.rows[0] : requestNotFound - }, - - /** - * Get a prayer request, including its full history, by its Id - * @param {string} userId The Id of the user to which the request belongs - * @param {string} requestId The Id of the request to retrieve - * @return The request, or a request-like object indicating that the request was not found - */ - fullById: async (userId, requestId) => { - const reqResults = await pool.query(` - SELECT "requestId", "enteredOn" - FROM mpj.request - WHERE "requestId" = $1 - AND "userId" = $2`, - [ requestId, userId ]) - if (0 === reqResults.rowCount) { - return requestNotFound - } - const req = reqResults.rows[0] - const history = await pool.query(` - SELECT "asOf", "status", COALESCE("text", '') AS "text" - FROM mpj.history - WHERE "requestId" = $1 - ORDER BY "asOf"`, - [ requestId ]) - req.history = history.rows - return req + addHistory: async (requestId, status, updateText) => { + const asOf = Date.now() + await pool.query(` + INSERT INTO mpj.history ("requestId", "asOf", "status", "text") VALUES ($1, $2, $3, NULLIF($4, ''))`, + [ requestId, asOf, status, updateText ]) }, /** @@ -121,17 +78,61 @@ export default function (pool) { }, /** - * Add a history entry for this request - * @param {string} requestId The Id of the request - * @param {string} status The status for this history entry - * @param {string} updateText The updated text for the request (pass blank if no update) + * Get the "current" version of a request by its Id + * @param {string} requestId The Id of the request to retrieve + * @param {string} userId The Id of the user to which the request belongs + * @return The request, or a request-like object indicating that the request was not found */ - addHistory: async (requestId, status, updateText) => { - const asOf = Date.now() - await pool.query(` - INSERT INTO mpj.history ("requestId", "asOf", "status", "text") VALUES ($1, $2, $3, NULLIF($4, ''))`, - [ requestId, asOf, status, updateText ]) - } + byId: async (userId, requestId) => { + const reqs = await pool.query(`${currentRequestSql} + WHERE "requestId" = $1 + AND "userId" = $2 + GROUP BY request."requestId"`, + [ requestId, userId ]) + return (0 < reqs.rowCount) ? reqs.rows[0] : requestNotFound + }, + /** + * Get a prayer request, including its full history, by its Id + * @param {string} userId The Id of the user to which the request belongs + * @param {string} requestId The Id of the request to retrieve + * @return The request, or a request-like object indicating that the request was not found + */ + fullById: async (userId, requestId) => { + const reqResults = await pool.query(` + SELECT "requestId", "enteredOn" + FROM mpj.request + WHERE "requestId" = $1 + AND "userId" = $2`, + [ requestId, userId ]) + if (0 === reqResults.rowCount) { + return requestNotFound + } + const req = reqResults.rows[0] + const history = await pool.query(` + SELECT "asOf", "status", COALESCE("text", '') AS "text" + FROM mpj.history + WHERE "requestId" = $1 + ORDER BY "asOf"`, + [ requestId ]) + req.history = history.rows + return req + }, + + /** + * Get the current requests for a user (i.e., their complete current journal) + * @param {string} userId The Id of the user + * @return The requests that make up the current journal + */ + journal: async userId => (await pool.query(`${journalSql} ORDER BY "asOf" DESC`, [ userId ])).rows, + + /** + * Get the least-recently-updated prayer request for the given user + * @param {string} userId The Id of the current user + * @return The least-recently-updated request for the given user + */ + oldest: async userId => (await pool.query(`${journalSql} ORDER BY "asOf" LIMIT 1`, [ userId ])).rows[0] + + } } diff --git a/src/api/routes/request.js b/src/api/routes/request.js index a320aca..bc9199e 100644 --- a/src/api/routes/request.js +++ b/src/api/routes/request.js @@ -16,15 +16,15 @@ export default function (checkJwt) { // Add a request history entry (prayed, updated, answered, etc.) .post('/:id/history', checkJwt, async (ctx, next) => { const body = ctx.request.body - const result = await db.request.addHistory(ctx.params.id, body.status, body.updateText) - ctx.status(('Not Found' === result.text) ? 404 : 204) + await db.request.addHistory(ctx.params.id, body.status, body.updateText) + ctx.response.status = 204 await next() }) // Get a journal-style request by its Id .get('/:id', checkJwt, async (ctx, next) => { const req = await db.request.byId(ctx.state.user.sub, ctx.params.id) if ('Not Found' === req.text) { - ctx.status(404) + ctx.response.status = 404 } else { ctx.body = req } @@ -34,7 +34,7 @@ export default function (checkJwt) { .get('/:id/full', checkJwt, async (ctx, next) => { const req = await db.request.fullById(ctx.state.user.sub, ctx.params.id) if ('Not Found' === req.text) { - ctx.status(404) + ctx.response.status = 404 } else { ctx.body = req } diff --git a/src/app/src/api/index.js b/src/app/src/api/index.js index 00ae5e6..09360ca 100644 --- a/src/app/src/api/index.js +++ b/src/app/src/api/index.js @@ -35,6 +35,11 @@ export default { * Mark a prayer request as having been prayed * @param {string} requestId The Id of the request */ - markPrayed: requestId => http.post('request/${requestId}/history', { status: 'Prayed', updateText: '' }) + markPrayed: requestId => http.post(`request/${requestId}/history`, { status: 'Prayed', updateText: '' }), + + /** + * Get a prayer request + */ + getPrayerRequest: requestId => http.get(`request/${requestId}`) } diff --git a/src/app/src/components/Dashboard.vue b/src/app/src/components/Dashboard.vue index 6ab0e8e..7d05967 100644 --- a/src/app/src/components/Dashboard.vue +++ b/src/app/src/components/Dashboard.vue @@ -9,7 +9,7 @@ el-col(:span='4'): strong Actions el-col(:span='16'): strong Request el-col(:span='4'): strong As Of - request-list-item(v-for="request in journal" v-bind:request="request" v-bind:key="request.requestId") + request-list-item(v-for="request in journal" :request="request" :key="request.requestId")