diff --git a/src/controllers/aliases.js b/src/controllers/aliases.js new file mode 100644 index 0000000..de4ebe6 --- /dev/null +++ b/src/controllers/aliases.js @@ -0,0 +1,26 @@ +const Model = require('../models/alias'); + +/** + * @class Aliases + * @classdesc Aliases controller + */ +class Aliases { + /** + * @static + * Find and return id of entity with given alias + * + * @param {string} alias - alias of entity + * @returns {Promise} + */ + static async get(alias) { + const id = await Model.get(alias); + console.log('id', id); + if (!id) { + throw new Error('Entity with given alias does not exist'); + } + + return id; + } +} + +module.exports = Aliases; diff --git a/src/models/alias.js b/src/models/alias.js new file mode 100644 index 0000000..49867ae --- /dev/null +++ b/src/models/alias.js @@ -0,0 +1,45 @@ +const {aliases: aliasesDb} = require('../utils/database/index'); +const getHashFromString = require('../utils/hash'); + +/** + * @typedef {Object} AliasData + * @property {string} type - entity type + * @property {string} hash - entity hash + * @property {string} id - entity id + * + */ + +/** + * @class Alias + * @property {string} type - entity type + * @property {string} id - entity title + */ +class Alias { + /** + * Find and return alias with given alias + * @param {string} aliasName - alias of entity + * @returns {Promise} + */ + static async get(aliasName) { + const hash = getHashFromString(aliasName.slice(1)).toString(); + const data = await aliasesDb.findOne({hash}); + + return new Alias(data); + } + + /** + * @constructor + * + * @param {AliasData} data + */ + constructor(data = {}) { + if (data === null) { + data = {}; + } + this.id = data.id; + this.type = data.type; + this.hash = data.hash; + } +} + +module.exports = Alias; diff --git a/src/models/page.js b/src/models/page.js index a9f3dfb..a05a7ad 100644 --- a/src/models/page.js +++ b/src/models/page.js @@ -68,11 +68,11 @@ class Page { * @param {PageData} pageData */ set data(pageData) { - const {body, parent} = pageData; + const {body, parent, uri} = pageData; this.body = body || this.body; this.title = this.extractTitleFromBody(); - this.uri = this._id; + this.uri = uri || ''; this._parent = parent || this._parent; } @@ -137,7 +137,10 @@ class Page { */ async save() { if (!this._id) { - const insertedRow = await pagesDb.insert(this.data); + const insertedRow = await pagesDb.insert({ + ...this.data, + uri: this.title.toLowerCase().split(' ').join('-') + }); this._id = insertedRow._id; } else { diff --git a/src/routes/aliases.js b/src/routes/aliases.js new file mode 100644 index 0000000..d3b6e73 --- /dev/null +++ b/src/routes/aliases.js @@ -0,0 +1,27 @@ +const express = require('express'); +const router = express.Router(); +const Aliases = require('../controllers/aliases'); + +/** + * GET /* + * + * Return document with given alias + */ +router.get('*', async (req, res) => { + try { + console.log('url ', req.originalUrl); + const id = await Aliases.get(req.originalUrl); + + res.json({ + success: true, + // result: page.data + }); + } catch (err) { + res.status(400).json({ + success: false, + error: err.message + }); + } +}); + +module.exports = router; diff --git a/src/routes/index.js b/src/routes/index.js index 6ad5d78..3828882 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -3,6 +3,7 @@ const router = express.Router(); const home = require('./home'); const pages = require('./pages'); +const aliases = require('./aliases'); const api = require('./api'); const pagesMiddleware = require('./middlewares/pages'); @@ -10,5 +11,6 @@ const pagesMiddleware = require('./middlewares/pages'); router.use('/', pagesMiddleware, home); router.use('/', pagesMiddleware, pages); router.use('/api', api); +router.use('/', aliases); module.exports = router; diff --git a/src/utils/hash.js b/src/utils/hash.js new file mode 100644 index 0000000..fbbea55 --- /dev/null +++ b/src/utils/hash.js @@ -0,0 +1,10 @@ +/** + * Function for getting hash from stringToHash + * + * @param stringToHash - stringToHash to encode + * @returns {string} - hash index + */ +module.exports = function getHashFromString(stringToHash) { + return stringToHash.split('').reduce((previousHashValue, currentChar) => + (((previousHashValue << 5) - previousHashValue) + currentChar.charCodeAt(0)) | 0, 0); +}; diff --git a/src/views/components/aside.twig b/src/views/components/aside.twig index 2ad0a5c..6f4d69d 100644 --- a/src/views/components/aside.twig +++ b/src/views/components/aside.twig @@ -1,7 +1,7 @@
{% for firstLevelPage in menu %}
- + {{ firstLevelPage.title }} {% if firstLevelPage.children is not empty %}