Skip to content

Commit 463394c

Browse files
committed
adds slugify feature for #522
1 parent e7abb55 commit 463394c

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
"s3": "^4.4.0",
126126
"s3-policy": "^0.2.0",
127127
"shortid": "^2.2.6",
128+
"slugify": "^1.2.9",
128129
"srcdoc-polyfill": "^0.2.0",
129130
"url": "^0.11.0",
130131
"webpack": "^2.6.1",

server/controllers/project.controller.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,24 @@ export function updateProject(req, res) {
7777
}
7878

7979
export function getProject(req, res) {
80-
Project.findById(req.params.project_id)
80+
const projectId = req.params.project_id;
81+
Project.findById(projectId)
8182
.populate('user', 'username')
82-
.exec((err, project) => {
83+
.exec((err, project) => { // eslint-disable-line
8384
if (err) {
8485
return res.status(404).send({ message: 'Project with that id does not exist' });
86+
} else if (!project) {
87+
Project.findOne({ slug: projectId })
88+
.populate('user', 'username')
89+
.exec((innerErr, projectBySlug) => {
90+
if (innerErr || !projectBySlug) {
91+
return res.status(404).send({ message: 'Project with that id does not exist' });
92+
}
93+
return res.json(projectBySlug);
94+
});
95+
} else {
96+
return res.json(project);
8597
}
86-
return res.json(project);
8798
});
8899
}
89100

server/models/project.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import mongoose from 'mongoose';
22
import shortid from 'shortid';
3+
import slugify from 'slugify';
34

45
const Schema = mongoose.Schema;
56

@@ -25,7 +26,8 @@ const projectSchema = new Schema({
2526
user: { type: Schema.Types.ObjectId, ref: 'User' },
2627
serveSecure: { type: Boolean, default: false },
2728
files: { type: [fileSchema] },
28-
_id: { type: String, default: shortid.generate }
29+
_id: { type: String, default: shortid.generate },
30+
slug: { type: String }
2931
}, { timestamps: true });
3032

3133
projectSchema.virtual('id').get(function getProjectId() {
@@ -36,4 +38,10 @@ projectSchema.set('toJSON', {
3638
virtuals: true
3739
});
3840

41+
projectSchema.pre('save', function generateSlug(next) {
42+
const project = this;
43+
project.slug = slugify(project.name, '_');
44+
return next();
45+
});
46+
3947
export default mongoose.model('Project', projectSchema);

server/utils/requestsOfType.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
header does not match `type`
55
*/
66
const requestsOfType = type => (req, res, next) => {
7-
if (process.env.NODE_ENV === 'development') {
8-
console.log(req);
9-
console.log(req.get('content-type'));
10-
}
117
if (req.get('content-type') != null && !req.is(type)) {
128
if (process.env.NODE_ENV === 'development') {
139
console.log('in requests of type error');

0 commit comments

Comments
 (0)