From ff98c8c4faa5798188fdc24ccad3608e6306ceee Mon Sep 17 00:00:00 2001 From: Sean Clarke Date: Sat, 4 Jan 2020 18:02:15 -0500 Subject: [PATCH] basic 'site page', model and controller for FindById creation --- app.js | 4 ++-- controllers/sites.js | 12 ++++++++++++ database/db.js | 4 ++-- database/fill-database.js | 6 +++--- model/Site.js | 27 +++++++++++++++++++++++++++ model/Visit.js | 0 routes/sites.js | 8 ++++++++ routes/users.js | 9 --------- views/site.pug | 7 +++++++ 9 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 controllers/sites.js create mode 100644 model/Site.js create mode 100644 model/Visit.js create mode 100644 routes/sites.js delete mode 100644 routes/users.js create mode 100644 views/site.pug diff --git a/app.js b/app.js index ab7aed4..1298832 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ var cookieParser = require('cookie-parser'); var logger = require('morgan'); var indexRouter = require('./routes/index'); -var usersRouter = require('./routes/users'); +var sitesRouter = require('./routes/sites'); var app = express(); @@ -20,7 +20,7 @@ app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', indexRouter); -app.use('/users', usersRouter); +app.use('/sites', sitesRouter); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/controllers/sites.js b/controllers/sites.js new file mode 100644 index 0000000..ea1487a --- /dev/null +++ b/controllers/sites.js @@ -0,0 +1,12 @@ +var Site = require('../model/Site'); + +module.exports = { + // Find site by ID export + findById: function(req, res){ + Site.findById(req.params.id, function(err, site){ + if(err) throw err; + console.log("Site->Controller: findById queried: " + site.site); + res.render('site', { title: site.site, site: site }); + }); + } +} diff --git a/database/db.js b/database/db.js index 448bde7..2cd317b 100644 --- a/database/db.js +++ b/database/db.js @@ -4,8 +4,8 @@ var mysql = require('mysql'); var con = mysql.createConnection({ host: "localhost", user: "root", - password: "root" - // database: "unesco" + password: "root", + database: "unesco" }); module.exports = con; diff --git a/database/fill-database.js b/database/fill-database.js index dbca5d7..e8e3710 100644 --- a/database/fill-database.js +++ b/database/fill-database.js @@ -9,10 +9,10 @@ con.connect(function(err) { console.log('"unesco" Database selected.'); }); - sql = "INSERT INTO sites (category, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique) VALUES ?"; + sql = "INSERT INTO sites (category, in_danger, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique) VALUES ?"; val = [ - ['Natural','2007','https://whc.unesco.org/en/list/1133','49.0097222222', '22.3388888889', '<p><span>This transboundary property stretches over 12 countries. Since the end of the last Ice Age, European Beech spread from a few isolated refuge areas in the Alps, Carpathians</span><span>, Dinarides</span><span>, Mediterranean and Pyrenees over a short period of a few thousand years in a process that is still ongoing. The successful expansion across a whole continent is related to the tree’s </span><span>adaptability and tolerance of different climatic, geographical and physical conditions. </span></p>', 'Ancient and Primeval Beech Forests of the Carpathians and Other Regions of Europe', '2152'], - ['Cultural','2014','https://whc.unesco.org/en/list/1459','-18.2500000000', '-69.5916666667', '<p>This site is an extensive Inca communication, trade and defence network of roads covering 30,000 km. Constructed by the Incas over several centuries and partly based on pre-Inca infrastructure, this extraordinary network through one of the world’s most extreme geographical terrains linked the snow-capped peaks of the Andes – at an altitude of more than 6,000 m – to the coast, running through hot rainforests, fertile valleys and absolute deserts. It reached its maximum expansion in the 15th century, when it spread across the length a significance', 'Qhapaq Ñan, Andean Road System', '2003'] + ['Natural', true, '2007','https://whc.unesco.org/en/list/1133','49.0097222222', '22.3388888889', '<p><span>This transboundary property stretches over 12 countries. Since the end of the last Ice Age, European Beech spread from a few isolated refuge areas in the Alps, Carpathians</span><span>, Dinarides</span><span>, Mediterranean and Pyrenees over a short period of a few thousand years in a process that is still ongoing. The successful expansion across a whole continent is related to the tree’s </span><span>adaptability and tolerance of different climatic, geographical and physical conditions. </span></p>', 'Ancient and Primeval Beech Forests of the Carpathians and Other Regions of Europe', '2152'], + ['Cultural', false, '2014','https://whc.unesco.org/en/list/1459','-18.2500000000', '-69.5916666667', '<p>This site is an extensive Inca communication, trade and defence network of roads covering 30,000 km. Constructed by the Incas over several centuries and partly based on pre-Inca infrastructure, this extraordinary network through one of the world’s most extreme geographical terrains linked the snow-capped peaks of the Andes – at an altitude of more than 6,000 m – to the coast, running through hot rainforests, fertile valleys and absolute deserts. It reached its maximum expansion in the 15th century, when it spread across the length a significance', 'Qhapaq Ñan, Andean Road System', '2003'] ]; con.query(sql, [val], function(err, res){ diff --git a/model/Site.js b/model/Site.js new file mode 100644 index 0000000..3b95a8e --- /dev/null +++ b/model/Site.js @@ -0,0 +1,27 @@ +var con = require('../database/db'); + +var Site = function(data){ + this.id = data.id; + this.category = data.category; + this.in_danger = data.in_danger; + this.date_inscribed = data.date_inscribed; + this.unesco_url = data.unesco_url; + this.latitude = data.latitude; + this.longitude = data.longitude; + this.description = data.description; + this.site = data.site; + this.unesco_unique = data.unesco_unique; +}; + +// Finding the site by ID. +Site.findById = function(id, callback){ + var sql = "SELECT * FROM sites WHERE id=? limit 1"; + + con.query(sql, id, function(err, result){ + if (err) return callback(err); + console.log("Site.findById: " + result[0].site + " retrieved!"); + callback(err, new Site(result[0])); + }); +}; + +module.exports = Site; diff --git a/model/Visit.js b/model/Visit.js new file mode 100644 index 0000000..e69de29 diff --git a/routes/sites.js b/routes/sites.js new file mode 100644 index 0000000..e1cef3f --- /dev/null +++ b/routes/sites.js @@ -0,0 +1,8 @@ +var express = require('express'); +var router = express.Router(); + +var siteController = require('../controllers/sites'); + +router.get('/:id', siteController.findById); + +module.exports = router; diff --git a/routes/users.js b/routes/users.js deleted file mode 100644 index 623e430..0000000 --- a/routes/users.js +++ /dev/null @@ -1,9 +0,0 @@ -var express = require('express'); -var router = express.Router(); - -/* GET users listing. */ -router.get('/', function(req, res, next) { - res.send('respond with a resource'); -}); - -module.exports = router; diff --git a/views/site.pug b/views/site.pug new file mode 100644 index 0000000..1a0e2d3 --- /dev/null +++ b/views/site.pug @@ -0,0 +1,7 @@ +extends layout + +block content + h1= site.site + p #{site.description} + + include footer.pug