Преглед на файлове

Fix duplicate repo folder creation

Benoît Hubert преди 8 години
родител
ревизия
4180d10a99
променени са 2 файла, в които са добавени 22 реда и са изтрити 20 реда
  1. 4 0
      lib/ExampleStore.js
  2. 18 20
      sandboxApp.js

+ 4 - 0
lib/ExampleStore.js

@@ -38,11 +38,15 @@ ExampleStore.prototype.loadRepository = function(repoPath) {
 };
 
 ExampleStore.prototype.addRepository = function(repoPath, repoDescriptor) {
+
   // Prepare files to write
   const self = this;
   const fullPath = this.rootPath + '/' + repoPath;
   const repoConfigFile = fullPath + '/repo-config.json';
   const repoConfig = beautify(repoDescriptor, null, 2, 100);
+
+  // Add repo path to descriptor
+  repoDescriptor.path = repoPath;
   
   return fs.mkdirAsync(fullPath)
   .then(() => fs.writeFileAsync(repoConfigFile, repoConfig))

+ 18 - 20
sandboxApp.js

@@ -155,30 +155,28 @@ function checkRepoExists(req, res, next) {
 /**
  * Create a new repo
  */
-app.post('/repos', function(req, res) {
-
-  // Check for title and extract params
-  if(! req.body || ! req.body.title) {
-    res.status(400).send('Le titre ne peut pas être vide !');
-  }
-  const { title } = req.body;
-  const repoSlug  = slug(title.toLowerCase());
-  const existingRepo = exStore.getRepo(repoSlug);
+app.post('/repos',
+  checkBodyPropsExist('title'),
+  function(req, res) {
 
-  // Prevent duplicate title
-  if(existingRepo) {
-    return res.status(409).send("La collection '" + title + "' existe déjà !");
-  }
+    const { title } = req.body;
+    const repoSlug  = slug(title.toLowerCase());
+    const existingRepo = exStore.getRepo(repoSlug);
+    // Prevent duplicate title
+    if(existingRepo) {
+      return res.status(409).send("La collection '" + title + "' existe déjà !");
+    }
 
-  // Prepare config
-  var config = Object.assign({
-    title
-  }, repoTmpl);
+    // Prepare config
+    var config = Object.assign({
+      title
+    }, repoTmpl);
 
-  exStore.addRepository(repoSlug, config)
-  .then(repo => res.json(config));
+    exStore.addRepository(repoSlug, config)
+    .then(repo => res.json(config));
 
-});
+  }
+);
 
 app.post('/:repoSlug/examples/:exampleSlug/file',
   checkRepoExists,