Skip to content

Commit edcd351

Browse files
authored
Merge pull request #3188 from processing/mongoose-collections-update
Update Collections with Mongoose Upgrade
2 parents 5e8cc73 + 5840f2f commit edcd351

File tree

4 files changed

+26
-29
lines changed

4 files changed

+26
-29
lines changed

server/controllers/collection.controller/listCollections.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@ async function getOwnerUserId(req) {
1414
return null;
1515
}
1616

17-
export default function listCollections(req, res) {
18-
function sendFailure({ code = 500, message = 'Something went wrong' }) {
17+
export default async function listCollections(req, res) {
18+
const sendFailure = ({ code = 500, message = 'Something went wrong' }) => {
1919
res.status(code).json({ success: false, message });
20-
}
20+
};
2121

22-
function sendSuccess(collections) {
22+
const sendSuccess = (collections) => {
2323
res.status(200).json(collections);
24-
}
24+
};
25+
26+
try {
27+
const owner = await getOwnerUserId(req);
2528

26-
function findCollections(owner) {
27-
if (owner == null) {
28-
sendFailure({ code: 404, message: 'User not found' });
29+
if (!owner) {
30+
sendFailure('404', 'User not found');
2931
}
3032

31-
return Collection.find({ owner }).populate([
33+
const collections = await Collection.find({ owner }).populate([
3234
{ path: 'owner', select: ['id', 'username'] },
3335
{
3436
path: 'items.project',
@@ -39,10 +41,9 @@ export default function listCollections(req, res) {
3941
}
4042
}
4143
]);
42-
}
4344

44-
return getOwnerUserId(req)
45-
.then(findCollections)
46-
.then(sendSuccess)
47-
.catch(sendFailure);
45+
sendSuccess(collections);
46+
} catch (error) {
47+
sendFailure(error.code || 500, error.message || 'Something went wrong');
48+
}
4849
}

server/controllers/collection.controller/removeProjectFromCollection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Collection from '../../models/collection';
22

3-
export default function addProjectToCollection(req, res) {
3+
export default function removeProjectFromCollection(req, res) {
44
const owner = req.user._id;
55
const { id: collectionId, projectId } = req.params;
66

server/models/collection.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const { Schema } = mongoose;
66

77
const collectedProjectSchema = new Schema(
88
{
9-
project: { type: Schema.Types.ObjectId, ref: 'Project' }
9+
project: { type: Schema.Types.String, ref: 'Project' }
1010
},
11-
{ timestamps: true, _id: true, usePushEach: true }
11+
{ timestamps: true }
1212
);
1313

1414
collectedProjectSchema.virtual('id').get(function getId() {
@@ -36,7 +36,7 @@ const collectionSchema = new Schema(
3636
owner: { type: Schema.Types.ObjectId, ref: 'User' },
3737
items: { type: [collectedProjectSchema] }
3838
},
39-
{ timestamps: true, usePushEach: true }
39+
{ timestamps: true }
4040
);
4141

4242
collectionSchema.virtual('id').get(function getId() {
@@ -48,9 +48,8 @@ collectionSchema.set('toJSON', {
4848
});
4949

5050
collectionSchema.pre('save', function generateSlug(next) {
51-
const collection = this;
52-
collection.slug = slugify(collection.name, '_');
53-
return next();
51+
this.slug = slugify(this.name, { lower: true, strict: true });
52+
next();
5453
});
5554

5655
export default mongoose.models.Collection ||

server/models/project.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const fileSchema = new Schema(
1616
fileType: { type: String, default: 'file' },
1717
isSelectedFile: { type: Boolean }
1818
},
19-
{ timestamps: true, _id: true, usePushEach: true }
19+
{ timestamps: true }
2020
);
2121

2222
fileSchema.virtual('id').get(function getFileId() {
@@ -40,7 +40,7 @@ const projectSchema = new Schema(
4040
_id: { type: String, default: shortid.generate },
4141
slug: { type: String }
4242
},
43-
{ timestamps: true, usePushEach: true }
43+
{ timestamps: true }
4444
);
4545

4646
projectSchema.virtual('id').get(function getProjectId() {
@@ -52,13 +52,10 @@ projectSchema.set('toJSON', {
5252
});
5353

5454
projectSchema.pre('save', function generateSlug(next) {
55-
const project = this;
56-
57-
if (!project.slug) {
58-
project.slug = slugify(project.name, '_');
55+
if (!this.slug) {
56+
this.slug = slugify(this.name, { lower: true, strict: true });
5957
}
60-
61-
return next();
58+
next();
6259
});
6360

6461
/**

0 commit comments

Comments
 (0)