adds slugify feature for #522 (#523)

This commit is contained in:
Cassie Tarakajian 2018-02-07 16:00:09 -05:00 committed by GitHub
parent e7abb55ee7
commit 3307613aec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 8 deletions

5
package-lock.json generated
View file

@ -30369,6 +30369,11 @@
"version": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz", "version": "https://registry.npmjs.org/slash/-/slash-1.0.0.tgz",
"integrity": "sha1-xB8vbDn8FtHNF61LXYlhFK5HDVU=" "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": { "source-list-map": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.0.tgz", "resolved": "https://registry.npmjs.org/source-list-map/-/source-list-map-2.0.0.tgz",

View file

@ -125,6 +125,7 @@
"s3": "^4.4.0", "s3": "^4.4.0",
"s3-policy": "^0.2.0", "s3-policy": "^0.2.0",
"shortid": "^2.2.6", "shortid": "^2.2.6",
"slugify": "^1.2.9",
"srcdoc-polyfill": "^0.2.0", "srcdoc-polyfill": "^0.2.0",
"url": "^0.11.0", "url": "^0.11.0",
"webpack": "^2.6.1", "webpack": "^2.6.1",

View file

@ -77,13 +77,24 @@ export function updateProject(req, res) {
} }
export function getProject(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') .populate('user', 'username')
.exec((err, project) => { .exec((err, project) => { // eslint-disable-line
if (err) { if (err) {
return res.status(404).send({ message: 'Project with that id does not exist' }); 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);
}); });
} }

View file

@ -1,5 +1,6 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
import shortid from 'shortid'; import shortid from 'shortid';
import slugify from 'slugify';
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
@ -25,7 +26,8 @@ const projectSchema = new Schema({
user: { type: Schema.Types.ObjectId, ref: 'User' }, user: { type: Schema.Types.ObjectId, ref: 'User' },
serveSecure: { type: Boolean, default: false }, serveSecure: { type: Boolean, default: false },
files: { type: [fileSchema] }, files: { type: [fileSchema] },
_id: { type: String, default: shortid.generate } _id: { type: String, default: shortid.generate },
slug: { type: String }
}, { timestamps: true }); }, { timestamps: true });
projectSchema.virtual('id').get(function getProjectId() { projectSchema.virtual('id').get(function getProjectId() {
@ -36,4 +38,10 @@ projectSchema.set('toJSON', {
virtuals: true virtuals: true
}); });
projectSchema.pre('save', function generateSlug(next) {
const project = this;
project.slug = slugify(project.name, '_');
return next();
});
export default mongoose.model('Project', projectSchema); export default mongoose.model('Project', projectSchema);

View file

@ -4,10 +4,6 @@
header does not match `type` header does not match `type`
*/ */
const requestsOfType = type => (req, res, next) => { 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 (req.get('content-type') != null && !req.is(type)) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
console.log('in requests of type error'); console.log('in requests of type error');