Skip to content

Commit

Permalink
Merge pull request #524 from brmodeloweb/fix/duplicate-model
Browse files Browse the repository at this point in the history
Fix/duplicate model
  • Loading branch information
miltonbsn authored Apr 24, 2024
2 parents 00cbd4e + c461d2f commit fc59ebc
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 55 deletions.
19 changes: 14 additions & 5 deletions app/angular/components/duplicateModelModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ import template from "./duplicateModelModal.html";

const app = angular.module("app.duplicateModelModalController", []);

const Controller = function () {
const Controller = function(ModelAPI) {
const $ctrl = this;
$ctrl.submitted = false;

$ctrl.$onInit = () => {
$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
});
});
}
};
Expand All @@ -33,6 +40,8 @@ export default app.component("duplicateModelModal", {
close: "&",
dismiss: "&",
suggestedName: "<",
userId: "<",
modelId: "<",
},
controller: Controller,
}).name;
26 changes: 26 additions & 0 deletions app/angular/conceptual/conceptual.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ const controller = function (ModelAPI, $stateParams, $rootScope, $timeout, $uibM
});
};

ctrl.duplicateModel = (model) => {
const modalInstance = $uibModal.open({
animation: true,
template: `<duplicate-model-modal
suggested-name="$ctrl.suggestedName"
close="$close(result)"
dismiss="$dismiss(reason)"
user-id=$ctrl.userId
model-id=$ctrl.modelId>
</duplicate-model-modal>`,
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"),
Expand Down
2 changes: 1 addition & 1 deletion app/angular/logic/logic.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ <h2 class="h4"><icon-logic title="{{'Logic model' | translate}}"></icon-logic> {
<li><a data-ng-click="$ctrl.zoomIn()" title="{{ 'Zoom in (Z +)' | translate }}"><i class="fa fa-search-plus"></i></a></li>
<li><a data-ng-click="$ctrl.zoomNone()" title="{{ 'Zoom 100% (Z 0)' | translate }}"><i class="fa fa-search"></i></a></li>
<li class="divider"><a data-ng-click="$ctrl.zoomOut()" title="{{ 'Zoom out (Z -)' | translate }}"><i class="fa fa-search-minus"></i></a></li>
<li><a data-ng-click="$ctrl.duplicateModel()" title="{{ 'Duplicate model' | translate }}"><i class="fa fa-files-o"></i></a></li>
<li><a data-ng-click="$ctrl.duplicateModel($ctrl.model)" title="{{ 'Duplicate model' | translate }}"><i class="fa fa-files-o"></i></a></li>
<li><a data-ng-click="$ctrl.generateSQL()" title="{{ 'Generate SQL' | translate }}"><i class="fa fa-database"></i></a></li>
<li class="divider"><a data-ng-click="$ctrl.shareModel($ctrl.model)" title="{{ 'Share' | translate }}"><i class="fa fa-share-alt"></i></a></li>
<li class="divider"><a data-ng-click="$ctrl.print()" title="{{ 'Print (CTRL P)' | translate }}"><i class="fa fa-print"></i></a></li>
Expand Down
38 changes: 19 additions & 19 deletions app/angular/logic/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,33 +204,33 @@ const controller = function (
});
}

ctrl.duplicateModel = () => {
ctrl.duplicateModel = (model) => {
const modalInstance = $uibModal.open({
animation: true,
template: '<duplicate-model-modal suggested-name="$ctrl.suggestedName" close="$close(result)" dismiss="$dismiss(reason)"></duplicate-model-modal>',
controller: function () {
template: `<duplicate-model-modal
suggested-name="$ctrl.suggestedName"
close="$close(result)"
dismiss="$dismiss(reason)"
user-id=$ctrl.userId
model-id=$ctrl.modelId>
</duplicate-model-modal>`,
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,
Expand Down
11 changes: 10 additions & 1 deletion app/angular/service/modelAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -84,7 +92,8 @@ const authService = ($http) => {
renameModel: _renameModel,
loadShareOptions: _loadShareOptions,
toggleShare: _toggleShare,
getSharedModel: _getSharedModel
getSharedModel: _getSharedModel,
duplicate: _duplicate
};
};

Expand Down
54 changes: 26 additions & 28 deletions app/angular/workspace/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -150,28 +151,26 @@ const ListController = function (
ctrl.duplicateModel = (model) => {
const modalInstance = $uibModal.open({
animation: true,
template: '<duplicate-model-modal suggested-name="$ctrl.suggestedName" close="$close(result)" dismiss="$dismiss(reason)"></duplicate-model-modal>',
template: `<duplicate-model-modal
suggested-name="$ctrl.suggestedName"
close="$close(result)"
dismiss="$dismiss(reason)"
user-id=$ctrl.userId
model-id=$ctrl.modelId>
</duplicate-model-modal>`,
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);
});
};

Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions server_app/model/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
31 changes: 30 additions & 1 deletion server_app/model/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -205,7 +233,8 @@ const modelService = {
toggleShare,
countAll,
findShareOptions,
findSharedModel
findSharedModel,
duplicate
};

module.exports = modelService;

0 comments on commit fc59ebc

Please sign in to comment.