diff --git a/common/models/peer.json b/common/models/peer.json index 556d9ca5..0a16c2b9 100644 --- a/common/models/peer.json +++ b/common/models/peer.json @@ -14,6 +14,14 @@ "hasManyRelation": "profiles" } ] + }, + "PatchMany": { + "relations": [ + { + "model": "topic", + "hasManyRelation": "topics" + } + ] } }, "hidden": [ diff --git a/server/middleware.json b/server/middleware.json index 6fd043dd..8d2098ca 100644 --- a/server/middleware.json +++ b/server/middleware.json @@ -6,7 +6,7 @@ "compression": {}, "cors": { "params": { - "origin": false, + "origin": true, "credentials": true, "maxAge": 86400 } @@ -47,4 +47,4 @@ "final:after": { "strong-error-handler": {} } -} +} \ No newline at end of file diff --git a/server/mixins/hasOne.js b/server/mixins/hasOne.js index 280fb71b..7148ad99 100644 --- a/server/mixins/hasOne.js +++ b/server/mixins/hasOne.js @@ -3,9 +3,8 @@ module.exports = function (Model, options) { options.relations.forEach(function (element) { var modelName = element.model; var relation = element.hasManyRelation; - var methodName = 'patch' + relation; - Model.post = function (id, data, cb) { + Model.postOne = function (id, data, cb) { Model.findById(id, function (err, modelInstance) { var relatedTo = modelInstance[relation]; relatedTo.count(function (err, count) { @@ -28,7 +27,7 @@ module.exports = function (Model, options) { }); }; - Model.patch = function (id, data, cb) { + Model.patchOne = function (id, data, cb) { Model.findById(id, function (err, modelInstance) { var relatedTo = modelInstance[relation]; relatedTo(function (err, instances) { @@ -50,7 +49,7 @@ module.exports = function (Model, options) { }); }; - Model.get = function (id, cb) { + Model.getOne = function (id, cb) { Model.findById(id, function (err, modelInstance) { if (err) { cb(err); @@ -64,7 +63,7 @@ module.exports = function (Model, options) { }); }; - Model.delete = function (id, cb) { + Model.deleteOne = function (id, cb) { Model.findById(id, function (err, modelInstance) { var relatedTo = modelInstance[relation]; relatedTo(function (err, instances) { @@ -87,7 +86,7 @@ module.exports = function (Model, options) { }; Model.remoteMethod( - 'post', + 'postOne', { accepts: [ { arg: 'id', type: 'string', required: true }, @@ -100,7 +99,7 @@ module.exports = function (Model, options) { ); Model.remoteMethod( - 'patch', + 'patchOne', { accepts: [ { arg: 'id', type: 'string', required: true }, @@ -113,7 +112,7 @@ module.exports = function (Model, options) { ); Model.remoteMethod( - 'get', + 'getOne', { accepts: [ { arg: 'id', type: 'string', required: true }, @@ -124,7 +123,7 @@ module.exports = function (Model, options) { } ); Model.remoteMethod( - 'delete', + 'deleteOne', { accepts: [ { arg: 'id', type: 'string', required: true } diff --git a/server/mixins/patchMany.js b/server/mixins/patchMany.js new file mode 100644 index 00000000..51aa1fff --- /dev/null +++ b/server/mixins/patchMany.js @@ -0,0 +1,86 @@ +module.exports = function (Model, options) { + 'use strict'; + options.relations.forEach(function (element) { + var modelName = element.model; + var relation = element.hasManyRelation; + + Model.linkMany = function (id, data, cb) { + Model.findById(id, function (err, modelInstance) { + if (err) { + cb(err); + } else { + var relatedModel = modelInstance[relation]; + var targetIds = data.targetIds; + if (targetIds.constructor === Array) { + targetIds.forEach(function (id) { + relatedModel.add(id, function (err, instanceData) { + if (err) { + console.log(err); + cb(err); + } else { + console.log(instanceData); + } + }); + }, this); + cb(null, true); + } else { + cb(err, "Please input Array targetIds:[]"); + } + } + }); + }; + Model.unlinkMany = function (id, data, cb) { + Model.findById(id, function (err, modelInstance) { + if (err) { + cb(err); + } else { + var relatedModel = modelInstance[relation]; + var targetIds = data.targetIds; + if (targetIds.constructor === Array) { + targetIds.forEach(function (id) { + relatedModel.remove(id, function (err, instanceData) { + if (err) { + console.log(err); + cb(err); + } else { + console.log(instanceData); + } + }); + }, this); + cb(null, true); + } else { + cb(err, "Please input Array targetIds:[]"); + } + } + }); + }; + + + Model.remoteMethod( + 'linkMany', + { + accepts: [ + { arg: 'id', type: 'string', required: true }, + { arg: 'data', type: 'object', required: true, http: { source: 'body' } } + ], + description: "Links " + relation + " (please provide targetIds:[])", + returns: { arg: 'Object', type: 'object', root: true }, + http: { path: '/:id/' + relation + '/rel', verb: 'patch' } + } + ); + Model.remoteMethod( + 'unlinkMany', + { + accepts: [ + { arg: 'id', type: 'string', required: true }, + { arg: 'data', type: 'object', required: true, http: { source: 'body' } } + ], + description: "Unlinks " + relation + " (please provide targetIds:[])", + returns: { arg: 'Object', type: 'object', root: true }, + http: { path: '/:id/' + relation + '/rel', verb: 'delete' } + } + ); + }, this); + + +}; \ No newline at end of file