diff --git a/db.js b/db.js index ed5dd17..862efff 100644 --- a/db.js +++ b/db.js @@ -49,6 +49,13 @@ exports.init = function(dbPath, params, callback) { Steppy( 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) { throw new Error('Build put hooks work only with single build'); } diff --git a/httpApi.js b/httpApi.js index 191217d..c20a21e 100644 --- a/httpApi.js +++ b/httpApi.js @@ -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) { Steppy( @@ -113,7 +154,7 @@ module.exports = function(app) { bodyString += data; }); req.on('end', function() { - var body = bodyString ? JSON.parse(bodyString) : null; + var body = bodyString ? JSON.parse(bodyString) : {}; stepCallback(null, body); }); req.on('error', stepCallback); diff --git a/lib/project.js b/lib/project.js index e3f9ec3..612534c 100644 --- a/lib/project.js +++ b/lib/project.js @@ -168,3 +168,25 @@ exports.remove = function(params, 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 + ); +};