add rename project via api

This commit is contained in:
oleg 2015-12-04 22:54:21 +03:00
parent 19969bc034
commit 6b8404a3ba
3 changed files with 71 additions and 1 deletions

7
db.js
View File

@ -49,6 +49,13 @@ exports.init = function(dbPath, params, callback) {
Steppy( Steppy(
function() { function() {
// quit if we already have ids and numbers
if (_(builds).all(function(build) {
return build.id && build.number;
})) {
return callback();
}
if (builds.length > 1) { if (builds.length > 1) {
throw new Error('Build put hooks work only with single build'); throw new Error('Build put hooks work only with single build');
} }

View File

@ -97,6 +97,47 @@ module.exports = function(app) {
); );
}); });
router.patch('/api/projects/:name', function(req, res, next) {
var projectName = req.params.name,
newProjectName = req.body.name;
Steppy(
function() {
logger.log(
'Rename project "%s" to "%s"', projectName, newProjectName
);
if (!newProjectName) throw new Error('new project name is not set');
var curProject = _(app.projects).findWhere({name: projectName});
if (!curProject) {
throw new Error('Project "' + projectName + '" not found');
}
this.pass(curProject);
var newProject = _(app.projects).findWhere({name: newProjectName});
if (newProject) {
throw new Error(
'Project name "' + newProjectName + '" already used'
);
}
project.rename({
baseDir: app.config.paths.projects,
name: projectName,
newName: newProjectName
}, this.slot());
},
function(err, curProject) {
curProject.name = newProjectName;
res.statusCode = 204;
res.end();
},
next
);
});
return function(req, res) { return function(req, res) {
Steppy( Steppy(
@ -113,7 +154,7 @@ module.exports = function(app) {
bodyString += data; bodyString += data;
}); });
req.on('end', function() { req.on('end', function() {
var body = bodyString ? JSON.parse(bodyString) : null; var body = bodyString ? JSON.parse(bodyString) : {};
stepCallback(null, body); stepCallback(null, body);
}); });
req.on('error', stepCallback); req.on('error', stepCallback);

View File

@ -168,3 +168,25 @@ exports.remove = function(params, callback) {
callback callback
); );
}; };
exports.rename = function(params, callback) {
Steppy(
function() {
fs.rename(
path.join(params.baseDir, params.name),
path.join(params.baseDir, params.newName),
this.slot()
);
db.builds.multiUpdate(
{start: {projectName: params.name, descCreateDate: ''}},
function(build) {
build.project.name = params.newName;
return build;
},
this.slot()
);
},
callback
);
};