Adding request works (mostly)
Everything works except adding the new request back to the list; this proof of concept is a go, and will be merged to master
This commit is contained in:
@@ -6,17 +6,30 @@ const env = process.env.NODE_ENV || 'dev'
|
||||
|
||||
if ('dev' === env) require('babel-register')
|
||||
|
||||
const app = require('./index').default
|
||||
const db = require('./db').default
|
||||
const json = require('./json.mjs').default
|
||||
|
||||
const fullEnv = ('dev' === env) ? 'Development' : 'Production'
|
||||
|
||||
/** Configuration for the application */
|
||||
const appConfig = require('./appsettings.json')
|
||||
const { port } = json('./appsettings.json')
|
||||
|
||||
/** Express app */
|
||||
const app = require('./index').default
|
||||
/**
|
||||
* Log a start-up message for the app
|
||||
* @param {string} status The status to display
|
||||
*/
|
||||
const startupMsg = (status) => {
|
||||
console.log(chalk`{reset myPrayerJournal ${status} | Port: {bold ${port}} | Mode: {bold ${fullEnv}}}`)
|
||||
}
|
||||
|
||||
// Ensure the database exists...
|
||||
require('./db').default.verify().then(() =>
|
||||
// ...and start it up!
|
||||
app.listen(appConfig.port, () => {
|
||||
console.log(chalk`{reset myPrayerJournal | Port: {bold ${appConfig.port}} | Mode: {bold ${fullEnv}}}`)
|
||||
}))
|
||||
// Ensure the database exists before starting up
|
||||
db.verify()
|
||||
.then(() => app.listen(port, () => startupMsg('ready')))
|
||||
.catch(err => {
|
||||
console.log(chalk`\n{reset {bgRed.white.bold || Error connecting to PostgreSQL }}`)
|
||||
for (let key of Object.keys(err)) {
|
||||
console.log(chalk`${key}: {reset {bold ${err[key]}}}`)
|
||||
}
|
||||
console.log('')
|
||||
startupMsg('failed')
|
||||
})
|
||||
|
||||
@@ -18,6 +18,6 @@ const query = (text, params) => pool.query(text, params)
|
||||
|
||||
export default {
|
||||
query: query,
|
||||
request: request(query),
|
||||
request: request(pool),
|
||||
verify: ddl(query).ensureDatabase
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import { Pool } from 'pg'
|
||||
import cuid from 'cuid'
|
||||
|
||||
export default function (query) {
|
||||
export default function (pool) {
|
||||
return {
|
||||
/**
|
||||
* Get the current requests for a user (i.e., their complete current journal)
|
||||
@@ -11,24 +11,55 @@ export default function (query) {
|
||||
* @return The requests that make up the current journal
|
||||
*/
|
||||
journal: async userId =>
|
||||
(await query('SELECT "requestId" FROM request WHERE "userId" = $1', [userId])).rows,
|
||||
(await pool.query({
|
||||
name: 'journal',
|
||||
text: `
|
||||
SELECT
|
||||
request."requestId",
|
||||
(SELECT "text"
|
||||
FROM mpj.history
|
||||
WHERE history."requestId" = request."requestId"
|
||||
AND "text" IS NOT NULL
|
||||
ORDER BY "asOf" DESC
|
||||
LIMIT 1) AS "text",
|
||||
(SELECT "asOf"
|
||||
FROM mpj.history
|
||||
WHERE history."requestId" = request."requestId"
|
||||
ORDER BY "asOf" DESC
|
||||
LIMIT 1) AS "asOf"
|
||||
FROM mpj.request
|
||||
WHERE "userId" = $1
|
||||
GROUP BY request."requestId"`
|
||||
}, [userId])).rows,
|
||||
|
||||
/**
|
||||
* Add a new prayer request
|
||||
* @param {string} userId The Id of the user
|
||||
* @param {string} requestText The text of the request
|
||||
* @return {string} The Id of the created request
|
||||
* @return The created request
|
||||
*/
|
||||
addNew: async (userId, requestText) => {
|
||||
const id = cuid()
|
||||
const enteredOn = Date.now()
|
||||
await query(`
|
||||
BEGIN;
|
||||
INSERT INTO request ("requestId", "enteredOn", "userId") VALUES ($1, $2, $3);
|
||||
INSERT INTO history ("requestId", "asOf", "status", "text") VALUES ($1, $2, 'Created', $4);
|
||||
COMMIT;`,
|
||||
[ id, enteredOn, userId, requestText ])
|
||||
return id
|
||||
;(async () => {
|
||||
const client = await pool.connect()
|
||||
try {
|
||||
await client.query('BEGIN')
|
||||
await client.query(
|
||||
'INSERT INTO mpj.request ("requestId", "enteredOn", "userId") VALUES ($1, $2, $3)',
|
||||
[ id, enteredOn, userId ])
|
||||
await client.query(
|
||||
`INSERT INTO mpj.history ("requestId", "asOf", "status", "text") VALUES ($1, $2, 'Created', $3)`,
|
||||
[ id, enteredOn, requestText ])
|
||||
await client.query('COMMIT')
|
||||
} catch (e) {
|
||||
await client.query('ROLLBACK')
|
||||
throw e
|
||||
} finally {
|
||||
client.release()
|
||||
}
|
||||
return { requestId: id, text: requestText, asOf: enteredOn }
|
||||
})().catch(e => console.error(e.stack))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
import Koa from 'koa'
|
||||
import bodyParser from 'koa-bodyparser'
|
||||
import morgan from 'koa-morgan'
|
||||
import send from 'koa-send'
|
||||
import serveFrom from 'koa-static'
|
||||
@@ -16,6 +17,8 @@ export default app
|
||||
.use(morgan('dev'))
|
||||
// Serve the Vue files from /public
|
||||
.use(serveFrom('public'))
|
||||
// Parse the body into ctx.request.body, if present
|
||||
.use(bodyParser())
|
||||
// Tie in all the routes
|
||||
.use(router.routes())
|
||||
.use(router.allowedMethods())
|
||||
|
||||
12
src/api/json.mjs
Normal file
12
src/api/json.mjs
Normal file
@@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
import fs from 'fs'
|
||||
|
||||
/**
|
||||
* Read and parse a JSON file
|
||||
* @param {string} path The path to the file
|
||||
* @param {string} encoding The encoding of the file (defaults to UTF-8)
|
||||
* @return {*} The parsed contents of the file
|
||||
*/
|
||||
export default (path, encoding = 'utf-8') =>
|
||||
JSON.parse(fs.readFileSync(path, encoding))
|
||||
@@ -8,8 +8,7 @@ const router = new Router()
|
||||
export default function (checkJwt) {
|
||||
|
||||
router.post('/', checkJwt, async (ctx, next) => {
|
||||
const newId = await db.request.addNew(ctx.state.user.sub, ctx.body.requestText)
|
||||
ctx.body = { id: newId }
|
||||
ctx.body = await db.request.addNew(ctx.state.user.sub, ctx.request.body.requestText)
|
||||
await next()
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user