From 571a65986c07a8b9501e9c14fbfc10bf922244ee Mon Sep 17 00:00:00 2001 From: Sean Clarke Date: Tue, 7 Jan 2020 17:41:30 -0500 Subject: [PATCH] visits added to site page --- controllers/sites.js | 7 +++++-- database/build.js | 2 +- database/fill-sites.js | 5 ++++- database/fill-visits.js | 10 +++++----- views/index.pug | 6 +++--- views/site.pug | 12 ++++++++++++ 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/controllers/sites.js b/controllers/sites.js index 39df5bc..fd0bb1a 100644 --- a/controllers/sites.js +++ b/controllers/sites.js @@ -18,8 +18,11 @@ module.exports = { // if(err) throw err; if(err) res.render('index', { title: 'Site is not found!'}); else { - console.log("Site->Controller: findBySlug queried: " + site.site); - res.render('site', { title: site.site, site: site }); + Visit.findBySite(site.id, function(err, visits){ + if(err) res.render('index', { title: 'Site:Visits has caused an error!'}); + console.log("Site->Controller: findBySlug queried: " + site.site); + res.render('site', { title: site.site, site: site, visits: visits }); + }); } }); }, diff --git a/database/build.js b/database/build.js index 38df76c..322fa31 100644 --- a/database/build.js +++ b/database/build.js @@ -21,7 +21,7 @@ con.connect(function(err) { }); con.query("CREATE TABLE sites(\ - id int NOT NULL AUTO_INCREMENT,\ + id int NOT NULL UNIQUE,\ category varchar(255),\ in_danger bool,\ date_inscribed int,\ diff --git a/database/fill-sites.js b/database/fill-sites.js index 1b4bfd4..af73afb 100644 --- a/database/fill-sites.js +++ b/database/fill-sites.js @@ -10,6 +10,9 @@ var arr = []; val.query.row.forEach(function(site){ var s = []; + // Changed ID to the UNESCO unique number. + // This is done in order to associate a visit to a site. + s.push(site.unique_number); s.push(site.category); s.push(site.danger != '' || false); s.push(site.date_inscribed); @@ -38,7 +41,7 @@ con.connect(function(err) { if (err) throw err; console.log("Connected!"); - sql = "INSERT INTO sites (category, in_danger, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique, img_url, states, slug) VALUES ?"; + sql = "INSERT INTO sites (id, category, in_danger, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique, img_url, states, slug) VALUES ?"; con.query(sql, [arr], function(err, res){ if (err) throw err; diff --git a/database/fill-visits.js b/database/fill-visits.js index 8385e71..f71f5fa 100644 --- a/database/fill-visits.js +++ b/database/fill-visits.js @@ -2,14 +2,14 @@ var con = require('./db'); sql = "INSERT INTO visits (date, img, site_id) VALUES ?"; val = [ - ['10-15-2019','https://seanland.ca', '1'], - ['10-17-2019','https://seanland.ca', '1'], - ['01-17-2020','https://seanland.ca', '134'], - ['11-23-2019','https://seanland.ca', '15'] + ['10-15-2019','https://seanland.ca', '203'], + ['10-17-2019','https://seanland.ca', '169'], + ['01-17-2020','https://seanland.ca', '520'], + ['11-23-2019','https://seanland.ca', '520'] ]; con.query(sql, [val], function(err, res){ if (err) throw err; - console.log("visits" + ": records inserted: " + res.affectedRows); + console.log("visit records inserted: " + res.affectedRows); process.exit(); }); diff --git a/views/index.pug b/views/index.pug index 279089a..7654585 100644 --- a/views/index.pug +++ b/views/index.pug @@ -1,7 +1,7 @@ extends layout block content - h1= title + h1(id='title')= title h2= "Sites Visited: " a(href="/visits") #{visits.length} @@ -13,7 +13,7 @@ block content h2= "Visits" each visit in visits - sitesVisited.push(visit.id); - h3= visit.site + h3 #[a(href="/sites/" + visit.slug) #{visit.site}] //- Commmand list of all the states h5 - var states = visit.states.split(',') @@ -25,7 +25,7 @@ block content h2= "Remaining" each site in sites if !(sitesVisited.includes(site.id)) - h3= site.site + h3 #[a(href="/sites/" + site.slug) #{site.site}] //- Commmand list of all the states h5 - var states = site.states.split(',') diff --git a/views/site.pug b/views/site.pug index 64f4747..7479dec 100644 --- a/views/site.pug +++ b/views/site.pug @@ -9,4 +9,16 @@ block content h5= site.category .content p #{site.description} + + //- - var states = visit.states.split(',') + //- - for (var i = 0; i < states.length; i++) + //- | #{states[i]} + //- if (i + 1 < states.length) + //- | , + + if visits + each visit in visits + p #{visit.date}: #{visit.img} at the site: #{visit.site_id} + + include footer.pug