Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions server/controllers/collection.controller/listCollections.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ async function getOwnerUserId(req) {
return null;
}

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

function sendSuccess(collections) {
const sendSuccess = (collections) => {
res.status(200).json(collections);
}
};

try {
const owner = await getOwnerUserId(req);

function findCollections(owner) {
if (owner == null) {
sendFailure({ code: 404, message: 'User not found' });
if (!owner) {
sendFailure('404', 'User not found');
}

return Collection.find({ owner }).populate([
const collections = await Collection.find({ owner }).populate([
{ path: 'owner', select: ['id', 'username'] },
{
path: 'items.project',
Expand All @@ -39,10 +41,9 @@ export default function listCollections(req, res) {
}
}
]);
}

return getOwnerUserId(req)
.then(findCollections)
.then(sendSuccess)
.catch(sendFailure);
sendSuccess(collections);
} catch (error) {
sendFailure(error.code || 500, error.message || 'Something went wrong');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Collection from '../../models/collection';

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

Expand Down
11 changes: 5 additions & 6 deletions server/models/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const { Schema } = mongoose;

const collectedProjectSchema = new Schema(
{
project: { type: Schema.Types.ObjectId, ref: 'Project' }
project: { type: Schema.Types.String, ref: 'Project' }
},
{ timestamps: true, _id: true, usePushEach: true }
{ timestamps: true }
);

collectedProjectSchema.virtual('id').get(function getId() {
Expand Down Expand Up @@ -36,7 +36,7 @@ const collectionSchema = new Schema(
owner: { type: Schema.Types.ObjectId, ref: 'User' },
items: { type: [collectedProjectSchema] }
},
{ timestamps: true, usePushEach: true }
{ timestamps: true }
);

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

collectionSchema.pre('save', function generateSlug(next) {
const collection = this;
collection.slug = slugify(collection.name, '_');
return next();
this.slug = slugify(this.name, { lower: true, strict: true });
next();
});

export default mongoose.models.Collection ||
Expand Down
13 changes: 5 additions & 8 deletions server/models/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const fileSchema = new Schema(
fileType: { type: String, default: 'file' },
isSelectedFile: { type: Boolean }
},
{ timestamps: true, _id: true, usePushEach: true }
{ timestamps: true }
);

fileSchema.virtual('id').get(function getFileId() {
Expand All @@ -40,7 +40,7 @@ const projectSchema = new Schema(
_id: { type: String, default: shortid.generate },
slug: { type: String }
},
{ timestamps: true, usePushEach: true }
{ timestamps: true }
);

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

projectSchema.pre('save', function generateSlug(next) {
const project = this;

if (!project.slug) {
project.slug = slugify(project.name, '_');
if (!this.slug) {
this.slug = slugify(this.name, { lower: true, strict: true });
}

return next();
next();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make a difference that we changed this from return next() to just next()?

});

/**
Expand Down