From 41ace1c149cd92c8ec0c7a5fc40f1bb371b42270 Mon Sep 17 00:00:00 2001 From: Christine Date: Fri, 23 Aug 2019 12:22:02 +0100 Subject: [PATCH] added functionality for new card addition Relates #12 --- src/app.js | 7 ++++++- src/controllers/index.js | 1 + src/controllers/newtarot.js | 13 ++++++++++++- src/model/database/queries/postNewCard.js | 8 ++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/model/database/queries/postNewCard.js diff --git a/src/app.js b/src/app.js index 2d99cfb..bdffdfd 100644 --- a/src/app.js +++ b/src/app.js @@ -4,9 +4,12 @@ const favicon = require("serve-favicon"); const exphbs = require("express-handlebars"); const controllers = require("./controllers/index"); const helpers = require("./views/helpers/index"); - +const bodyparse = require('body-parser'); const app = express(); +// post handler +app.use(bodyparse.urlencoded({ extended: false })); + // set up view engine app.set("views", path.join(__dirname, "views")); app.set("view engine", "hbs"); @@ -26,4 +29,6 @@ app.set("port", process.env.PORT || 3000); app.use(express.static(path.join(__dirname, "..", "public"))); app.use(controllers); + + module.exports = app; diff --git a/src/controllers/index.js b/src/controllers/index.js index f9fd697..bcadebf 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -9,6 +9,7 @@ const newtarot = require('./newtarot'); router.get('/', home.get); router.get('/tarot', tarot.tarot); router.get('/newtarot', newtarot.newtarot); +router.post('/newtarot', newtarot.newtarotpost) router.use(error.client); router.use(error.server); diff --git a/src/controllers/newtarot.js b/src/controllers/newtarot.js index 791e370..9f2c272 100644 --- a/src/controllers/newtarot.js +++ b/src/controllers/newtarot.js @@ -1,3 +1,14 @@ +const postNewCard = require("../model/database/queries/postNewCard") + exports.newtarot = (req, res) => { res.render("newtarot") -} \ No newline at end of file +} + +exports.newtarotpost = (req, res) => { + postNewCard.postNewCard(req.body["tarot-title"], req.body["tarot-description"]) + .then(result => { + console.log({ result }); + res.render("newtarot"); + }) +} + diff --git a/src/model/database/queries/postNewCard.js b/src/model/database/queries/postNewCard.js new file mode 100644 index 0000000..f30a5e5 --- /dev/null +++ b/src/model/database/queries/postNewCard.js @@ -0,0 +1,8 @@ +const connection = require("../db_connection"); + +exports.postNewCard = (tarottitle, tarotdescription) => { + return connection.query("INSERT INTO tarot_cards (tarot_heading, tarot_description, tarot_image) VALUES($1,$2,$3)", + [tarottitle, tarotdescription, ""] + ) +} +