Browse Source

slugs created and implemented

master
Sean Clarke 2 months ago
parent
commit
e69a1e03f8
6 changed files with 38 additions and 4 deletions
  1. +10
    -0
      controllers/sites.js
  2. +1
    -0
      database/build.js
  3. +8
    -2
      database/fill-sites.js
  4. +16
    -0
      model/Site.js
  5. +2
    -1
      routes/sites.js
  6. +1
    -1
      views/index.pug

+ 10
- 0
controllers/sites.js View File

@@ -13,6 +13,16 @@ module.exports = {
}
});
},
findBySlug: function(req, res){
Site.findBySlug(req.params.slug, function(err, site){
// 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 });
}
});
},
getHome: function(req, res){
Site.getAll(function(err, sites){
Visit.findUniqueSites(function(err, uniqueSites){


+ 1
- 0
database/build.js View File

@@ -33,6 +33,7 @@ con.connect(function(err) {
unesco_unique int UNIQUE,\
img_url varchar(255),\
states varchar(255),\
slug varchar(255) UNIQUE,\
PRIMARY KEY (id)\
)", function(err, res){
if (err) throw err;


+ 8
- 2
database/fill-sites.js View File

@@ -22,7 +22,13 @@ val.query.row.forEach(function(site){
s.push(site.unique_number);
s.push(site.image_url);
s.push(site.states);
// console.log(site);
// Generating a slug.
var slug = htmlToText.fromString(htmlToText.fromString(site.site), { wordwrap: false });
var punctuationless = slug.replace(/[.',\/#!$%\^&\*;:{}=\-_`~()]/g,"");
var finalString = punctuationless.replace(/\s{2,}/g," ");
slug = finalString.toLowerCase().split(" ").join("-");
s.push(slug);
console.log(slug);
arr.push(s);
});

@@ -32,7 +38,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) VALUES ?";
sql = "INSERT INTO sites (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;


+ 16
- 0
model/Site.js View File

@@ -13,6 +13,7 @@ var Site = function(data){
this.unesco_unique = data.unesco_unique;
this.img_url = data.img_url
this.states = data.states;
this.slug = data.slug;
};

// Finding the site by ID.
@@ -30,6 +31,21 @@ Site.findById = function(id, callback){
});
};

// Finding the site by slug.
Site.findBySlug = function(slug, callback){
var sql = "SELECT * FROM sites WHERE slug=? limit 1";

con.query(sql, slug, function(err, result){
if (err) return callback(err);
// if not site is found.
if (result[0] == undefined) callback(404);
else {
console.log("Site.findBySlug: " + result[0].site + " retrieved!");
callback(err, new Site(result[0]));
}
});
};

// Finding all sites by IDs.
Site.findAllById = function(ids, callback){
var sql = "SELECT * FROM sites WHERE id in (?)";


+ 2
- 1
routes/sites.js View File

@@ -4,6 +4,7 @@ var router = express.Router();
var siteController = require('../controllers/sites');

router.get('/', siteController.getAll);
router.get('/:id', siteController.findById);
// router.get('/:id', siteController.findById);
router.get('/:slug', siteController.findBySlug);

module.exports = router;

+ 1
- 1
views/index.pug View File

@@ -34,7 +34,7 @@ block content
if (i + 1 < states.length)
| ,
else
h3= "Removed!"
//- h3= "Removed!"

include footer.pug

Loading…
Cancel
Save