diff --git a/package-lock.json b/package-lock.json index 08563e71..0aa6eea3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30369,6 +30369,11 @@ "version": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz", "integrity": "sha1-xB8vbDn8FtHNF61LXYlhFK5HDVU=" }, + "slugify": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/slugify/-/slugify-1.2.9.tgz", + "integrity": "sha512-n0cdJ+kN3slJu8SbZXt/EHjljBqF6MxvMGSg/NPpBzoY7yyXoH38wp/ox20a1JaG1KgmdTN5Lf3aS9+xB2Y2aQ==" + }, "source-list-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.0.tgz", diff --git a/package.json b/package.json index a1eaea9a..dc58e150 100644 --- a/package.json +++ b/package.json @@ -125,6 +125,7 @@ "s3": "^4.4.0", "s3-policy": "^0.2.0", "shortid": "^2.2.6", + "slugify": "^1.2.9", "srcdoc-polyfill": "^0.2.0", "url": "^0.11.0", "webpack": "^2.6.1", diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index f1b179a2..df1397a5 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -77,13 +77,24 @@ export function updateProject(req, res) { } export function getProject(req, res) { - Project.findById(req.params.project_id) + const projectId = req.params.project_id; + Project.findById(projectId) .populate('user', 'username') - .exec((err, project) => { + .exec((err, project) => { // eslint-disable-line if (err) { return res.status(404).send({ message: 'Project with that id does not exist' }); + } else if (!project) { + Project.findOne({ slug: projectId }) + .populate('user', 'username') + .exec((innerErr, projectBySlug) => { + if (innerErr || !projectBySlug) { + return res.status(404).send({ message: 'Project with that id does not exist' }); + } + return res.json(projectBySlug); + }); + } else { + return res.json(project); } - return res.json(project); }); } diff --git a/server/models/project.js b/server/models/project.js index 169fbc52..c02c99c4 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -1,5 +1,6 @@ import mongoose from 'mongoose'; import shortid from 'shortid'; +import slugify from 'slugify'; const Schema = mongoose.Schema; @@ -25,7 +26,8 @@ const projectSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: 'User' }, serveSecure: { type: Boolean, default: false }, files: { type: [fileSchema] }, - _id: { type: String, default: shortid.generate } + _id: { type: String, default: shortid.generate }, + slug: { type: String } }, { timestamps: true }); projectSchema.virtual('id').get(function getProjectId() { @@ -36,4 +38,10 @@ projectSchema.set('toJSON', { virtuals: true }); +projectSchema.pre('save', function generateSlug(next) { + const project = this; + project.slug = slugify(project.name, '_'); + return next(); +}); + export default mongoose.model('Project', projectSchema); diff --git a/server/utils/requestsOfType.js b/server/utils/requestsOfType.js index 53f9d44e..20d806d6 100644 --- a/server/utils/requestsOfType.js +++ b/server/utils/requestsOfType.js @@ -4,10 +4,6 @@ header does not match `type` */ const requestsOfType = type => (req, res, next) => { - if (process.env.NODE_ENV === 'development') { - console.log(req); - console.log(req.get('content-type')); - } if (req.get('content-type') != null && !req.is(type)) { if (process.env.NODE_ENV === 'development') { console.log('in requests of type error');