summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrés Manelli <andresmanelli@gmail.com>2018-04-15 21:34:15 -0300
committerAndrés Manelli <andresmanelli@gmail.com>2018-04-16 00:18:44 -0300
commit4861da52a031ca6ee05755df894caef9d062be42 (patch)
tree7b6a6a11a7d8e6adcb997e6a606c6f7bff0e4b0e
parent4505e6edc98cb9c5de88a1b516af98baf68bd688 (diff)
downloadwekan-4861da52a031ca6ee05755df894caef9d062be42.tar.gz
wekan-4861da52a031ca6ee05755df894caef9d062be42.tar.bz2
wekan-4861da52a031ca6ee05755df894caef9d062be42.zip
Fix checklists REST API (#1562)
-rw-r--r--models/checklists.js112
1 files changed, 54 insertions, 58 deletions
diff --git a/models/checklists.js b/models/checklists.js
index 9946f98e..006eccab 100644
--- a/models/checklists.js
+++ b/models/checklists.js
@@ -106,94 +106,90 @@ if (Meteor.isServer) {
if (Meteor.isServer) {
JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
- try {
- Authentication.checkUserId( req.userId);
- const paramCardId = req.params.cardId;
+ Authentication.checkUserId( req.userId);
+ const paramCardId = req.params.cardId;
+ const checklists = Checklists.find({ cardId: paramCardId }).map(function (doc) {
+ return {
+ _id: doc._id,
+ title: doc.title,
+ };
+ });
+ if (checklists) {
JsonRoutes.sendResult(res, {
code: 200,
- data: Checklists.find({ cardId: paramCardId }).map(function (doc) {
- return {
- _id: doc._id,
- title: doc.title,
- };
- }),
+ data: checklists,
});
- }
- catch (error) {
+ } else {
JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
+ code: 500,
});
}
});
JsonRoutes.add('GET', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
- try {
- Authentication.checkUserId( req.userId);
- const paramChecklistId = req.params.checklistId;
- const paramCardId = req.params.cardId;
+ Authentication.checkUserId( req.userId);
+ const paramChecklistId = req.params.checklistId;
+ const paramCardId = req.params.cardId;
+ const checklist = Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId });
+ if (checklist) {
+ checklist.items = ChecklistItems.find({checklistId: checklist._id}).map(function (doc) {
+ return {
+ _id: doc._id,
+ title: doc.title,
+ };
+ });
JsonRoutes.sendResult(res, {
code: 200,
- data: Checklists.findOne({ _id: paramChecklistId, cardId: paramCardId }),
+ data: checklist,
});
- }
- catch (error) {
+ } else {
JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
+ code: 500,
});
}
- });
+});
JsonRoutes.add('POST', '/api/boards/:boardId/cards/:cardId/checklists', function (req, res) {
- try {
- Authentication.checkUserId( req.userId);
- const paramCardId = req.params.cardId;
+ Authentication.checkUserId( req.userId);
- const checklistToSend = {};
- checklistToSend.cardId = paramCardId;
- checklistToSend.title = req.body.title;
- checklistToSend.items = [];
- const id = Checklists.insert(checklistToSend);
+ const paramCardId = req.params.cardId;
+ const id = Checklists.insert({
+ title: req.body.title,
+ cardId: paramCardId,
+ sort: 0,
+ });
+ if (id) {
const checklist = Checklists.findOne({_id: id});
- req.body.items.forEach(function (item) {
- checklist.addItem(item);
- }, this);
-
-
+ req.body.items.forEach(function (item, idx) {
+ ChecklistItems.insert({
+ cardId: paramCardId,
+ checklistId: id,
+ title: item.title,
+ sort: idx,
+ });
+ });
JsonRoutes.sendResult(res, {
code: 200,
data: {
_id: id,
},
});
- }
- catch (error) {
+ } else {
JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
+ code: 400,
});
}
});
JsonRoutes.add('DELETE', '/api/boards/:boardId/cards/:cardId/checklists/:checklistId', function (req, res) {
- try {
- Authentication.checkUserId( req.userId);
- const paramCommentId = req.params.commentId;
- const paramCardId = req.params.cardId;
- Checklists.remove({ _id: paramCommentId, cardId: paramCardId });
- JsonRoutes.sendResult(res, {
- code: 200,
- data: {
- _id: paramCardId,
- },
- });
- }
- catch (error) {
- JsonRoutes.sendResult(res, {
- code: 200,
- data: error,
- });
- }
+ Authentication.checkUserId( req.userId);
+ const paramChecklistId = req.params.checklistId;
+ Checklists.remove({ _id: paramChecklistId });
+ JsonRoutes.sendResult(res, {
+ code: 200,
+ data: {
+ _id: paramChecklistId,
+ },
+ });
});
}