basic 'site page', model and controller for FindById creation

This commit is contained in:
Sean Clarke 2020-01-04 18:02:15 -05:00
parent 191e7e825a
commit ff98c8c4fa
9 changed files with 61 additions and 16 deletions

4
app.js
View File

@ -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) {

12
controllers/sites.js Normal file
View File

@ -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 });
});
}
}

View File

@ -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;

View File

@ -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 trees </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 worlds 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 trees </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 worlds 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){

27
model/Site.js Normal file
View File

@ -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;

0
model/Visit.js Normal file
View File

8
routes/sites.js Normal file
View File

@ -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;

View File

@ -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;

7
views/site.pug Normal file
View File

@ -0,0 +1,7 @@
extends layout
block content
h1= site.site
p #{site.description}
include footer.pug