diff --git a/app/angular/components/duplicateModelModal.js b/app/angular/components/duplicateModelModal.js index f9b06650..587c2bc7 100644 --- a/app/angular/components/duplicateModelModal.js +++ b/app/angular/components/duplicateModelModal.js @@ -3,7 +3,7 @@ import template from "./duplicateModelModal.html"; const app = angular.module("app.duplicateModelModalController", []); -const Controller = function () { +const Controller = function(ModelAPI) { const $ctrl = this; $ctrl.submitted = false; @@ -11,11 +11,18 @@ const Controller = function () { $ctrl.name = $ctrl.suggestedName; }; - $ctrl.save = function (modelname) { + $ctrl.save = function (newName) { $ctrl.submitted = true; - if (modelname != null && modelname != "") { - $ctrl.close({ - result: modelname, + if (newName != null && newName != "") { + ModelAPI.duplicate($ctrl.modelId, $ctrl.userId, newName).then((newModelResponse) => { + $ctrl.close({ + result: newModelResponse.data, + }); + }).catch(error => { + $ctrl.dismiss({ + result: "error", + reason: error + }); }); } }; @@ -33,6 +40,8 @@ export default app.component("duplicateModelModal", { close: "&", dismiss: "&", suggestedName: "<", + userId: "<", + modelId: "<", }, controller: Controller, }).name; \ No newline at end of file diff --git a/app/angular/conceptual/conceptual.js b/app/angular/conceptual/conceptual.js index 7c720bd1..cc0dbcdf 100644 --- a/app/angular/conceptual/conceptual.js +++ b/app/angular/conceptual/conceptual.js @@ -135,6 +135,32 @@ const controller = function (ModelAPI, $stateParams, $rootScope, $timeout, $uibM }); }; + ctrl.duplicateModel = (model) => { + const modalInstance = $uibModal.open({ + animation: true, + template: ` + `, + controller: function() { + const $ctrl = this; + $ctrl.suggestedName = $filter('translate')("MODEL_NAME (copy)", { name: model.name }); + $ctrl.modelId = model._id; + $ctrl.userId = model.who; + }, + controllerAs: '$ctrl', + }).result; + modalInstance.then((newModel) => { + window.open($state.href('logic', { references: { 'modelid': newModel._id } })); + ctrl.showFeedback(true, "Successfully duplicated!"); + }).catch(error => { + console.error(error); + }); + }; + ctrl.convertModel = (conceptualModel) => { const model = { "name": conceptualModel.name + $filter('translate')("_converted"), diff --git a/app/angular/logic/logic.html b/app/angular/logic/logic.html index 85427dbd..8b607c17 100644 --- a/app/angular/logic/logic.html +++ b/app/angular/logic/logic.html @@ -42,7 +42,7 @@

{
  • -
  • +
  • diff --git a/app/angular/logic/logic.js b/app/angular/logic/logic.js index 03973012..f84b3c85 100644 --- a/app/angular/logic/logic.js +++ b/app/angular/logic/logic.js @@ -204,33 +204,33 @@ const controller = function ( }); } - ctrl.duplicateModel = () => { + ctrl.duplicateModel = (model) => { const modalInstance = $uibModal.open({ animation: true, - template: '', - controller: function () { + template: ` + `, + controller: function() { const $ctrl = this; - $ctrl.suggestedName = $filter('translate')("MODEL_NAME (copy)", { name: ctrl.model.name }); + $ctrl.suggestedName = $filter('translate')("MODEL_NAME (copy)", { name: model.name }); + $ctrl.modelId = model.id; + $ctrl.userId = model.user; }, controllerAs: '$ctrl', - }); - modalInstance.result.then((newName) => { - ctrl.setLoading(true); - const duplicatedModel = { - id: "", - name: newName, - type: ctrl.model.type, - model: JSON.stringify(LogicService.graph), - user: ctrl.model.user, - }; - ModelAPI.saveModel(duplicatedModel).then((newModel) => { - window.open($state.href('logic', { references: { 'modelid': newModel._id } })); - ctrl.showFeedback("Successfully duplicated!", true, 'success'); - ctrl.setLoading(false); - }); + }).result; + modalInstance.then((newModel) => { + window.open($state.href('logic', { references: { 'modelid': newModel._id } })); + ctrl.showFeedback("Successfully duplicated!", true, 'success'); + }).catch(error => { + console.error(error); }); }; + ctrl.shareModel = (model) => { const modalInstance = $uibModal.open({ animation: true, diff --git a/app/angular/service/modelAPI.js b/app/angular/service/modelAPI.js index f4465638..a5c04e55 100644 --- a/app/angular/service/modelAPI.js +++ b/app/angular/service/modelAPI.js @@ -75,6 +75,14 @@ const authService = ($http) => { }); }; + const _duplicate= function (modelId, userId, newName) { + return $http + .post(`/models/${modelId}/duplicate`, {"userId": userId, "newName": newName}) + .then(function (resp) { + return resp; + }); + }; + return { saveModel: _saveModel, getAllModels: _getAllModels, @@ -84,7 +92,8 @@ const authService = ($http) => { renameModel: _renameModel, loadShareOptions: _loadShareOptions, toggleShare: _toggleShare, - getSharedModel: _getSharedModel + getSharedModel: _getSharedModel, + duplicate: _duplicate }; }; diff --git a/app/angular/workspace/workspace.js b/app/angular/workspace/workspace.js index e6ca48cb..db0ecace 100644 --- a/app/angular/workspace/workspace.js +++ b/app/angular/workspace/workspace.js @@ -12,7 +12,6 @@ import shareModelModal from "../components/shareModelModal"; import iconConceptual from "../components/icons/conceptual"; import iconLogic from "../components/icons/logic"; - const ListController = function ( $state, $rootScope, @@ -48,16 +47,18 @@ const ListController = function ( ctrl.loading = loading; }; + const mapData = (model) => { + if (model.type == "conceptual") { + model.typeName = $filter('translate')("Conceptual"); + } else { + model.typeName = $filter('translate')("Logical"); + } + model.authorName = AuthService.loggeduserName; + return model; + }; + const mapListData = (models) => { - return models.map((model) => { - if (model.type == "conceptual") { - model.typeName = $filter('translate')("Conceptual"); - } else { - model.typeName = $filter('translate')("Logical"); - } - model.authorName = AuthService.loggeduserName; - return model; - }); + return models.map(mapData); }; const doDelete = (model) => { @@ -150,28 +151,26 @@ const ListController = function ( ctrl.duplicateModel = (model) => { const modalInstance = $uibModal.open({ animation: true, - template: '', + template: ` + `, controller: function() { const $ctrl = this; $ctrl.suggestedName = $filter('translate')("MODEL_NAME (copy)", { name: model.name }); + $ctrl.modelId = model._id; + $ctrl.userId = model.who; }, controllerAs: '$ctrl', - }); - modalInstance.result.then((newName) => { - showLoading(true); - const duplicatedModel = { - id: "", - name: newName, - type: model.type, - model: model.model, - user: model.who, - }; - ModelAPI.saveModel(duplicatedModel).then((newModel) => { - newModel.authorName = model.authorName; - newModel.typeName = model.typeName; - ctrl.models.push(newModel); - showLoading(false); - }); + }).result; + modalInstance.then((newModel) => { + ctrl.models.push(mapData(newModel)); + ctrl.showFeedback($filter('translate')("Successfully duplicated!"), true, 'success'); + }).catch(error => { + console.error(error); }); }; @@ -188,7 +187,6 @@ const ListController = function ( controllerAs: '$ctrl', }).result; modalInstance.then(() => { - console.log("Successfully share config saved!"); ctrl.showFeedback($filter('translate')("Sharing configuration has been updated successfully!"), true, 'success'); }).catch((reason) => { console.log("Modal dismissed with reason", reason); diff --git a/server_app/model/handler.js b/server_app/model/handler.js index c23d5a3d..1ef469cd 100644 --- a/server_app/model/handler.js +++ b/server_app/model/handler.js @@ -176,6 +176,22 @@ const findSharedModel = async (req, res) => { } }; +const duplicate = async (req, res) => { + try { + const modelId = req.params.modelId; + const { newName, userId } = req.body; + const duplicated = await modelService.duplicate(modelId, userId, newName); + return res.status(200).send(duplicated); + } catch (error) { + if(error.status === 401) { + return res.status(401).json({ auth: false, message: 'You are not authorized to complete this request' }); + } + return res + .status(500) + .send("There's an error while finding your sharing config model request"); + } +}; + module.exports = router .get("/",validateJWT, listAll) .post("/",validateJWT, save) @@ -186,4 +202,5 @@ module.exports = router .post("/share", validateJWT, toggleShare) .get("/:modelId/share/options", validateJWT, findShareOptions) .post("/import",validateJWT, importModel) + .post("/:modelId/duplicate",validateJWT, duplicate) .get("/share/:sharedId", findSharedModel); diff --git a/server_app/model/service.js b/server_app/model/service.js index 0d92f870..fb14115f 100644 --- a/server_app/model/service.js +++ b/server_app/model/service.js @@ -195,6 +195,34 @@ const countAll = async (userId) => { }); }; +const duplicate = async (modelId, userId, newName) => { + console.log(modelId, userId, newName); + return new Promise(async (resolve, reject) => { + try { + + const originalModel = await getById(modelId, userId); + + const duplicatedModel = await save({ + userId: userId, + type: originalModel.type, + model: originalModel.model, + name: newName + }); + + return resolve({ + "_id": duplicatedModel._id, + "type": duplicatedModel.type, + "name": duplicatedModel.name, + "created": duplicatedModel.created, + "who": duplicatedModel.who + }); + } catch (error) { + console.error(error); + reject(error); + } + }); +}; + const modelService = { listAll, getById, @@ -205,7 +233,8 @@ const modelService = { toggleShare, countAll, findShareOptions, - findSharedModel + findSharedModel, + duplicate }; module.exports = modelService;