Selaa lähdekoodia

handle new repo creation

Benoît Hubert 8 vuotta sitten
vanhempi
commit
3c786b1182
5 muutettua tiedostoa jossa 63 lisäystä ja 7 poistoa
  1. 1 1
      html/index.mustache.html
  2. 1 1
      js/ws-forms.js
  3. 20 3
      lib/ExampleStore.js
  4. 9 0
      lib/repoTmpl.json
  5. 32 2
      sandboxApp.js

+ 1 - 1
html/index.mustache.html

@@ -72,7 +72,7 @@
               {{/example}}
               </section>
               <section id="add-example" class="inline-block">
-                <button class="add-btn inline-block h-collapsed fast in"><span class="icon-plus rounded"></span></button>
+                <button class="add-btn inline-block h-collapsed fast{{#repo}} in{{/repo}}"><span class="icon-plus rounded"></span></button>
                 <form class="inline-block h-collapsed">
                     <input type="text" name="title" class="input-sm" value="" placeholder="{{_.exampleName}}" required />
                     <button type="button" id="add-example-cancel" class="icon-cross rounded red"></button><!--

+ 1 - 1
js/ws-forms.js

@@ -91,7 +91,7 @@
   }
 
   makeFormView('add-repo', function(title) {
-    return rp.post('/collections', { title: title });
+    return rp.post('/repos', { title: title });
   });
 
   makeFormView('add-example', function(title) {

+ 20 - 3
lib/ExampleStore.js

@@ -1,6 +1,11 @@
-const { scandirAsync } = require('./fsio');
-var Promise   = require('bluebird');
-var _         = require('lodash');
+const {
+  scandirAsync
+}               = require('./fsio');
+const Promise   = require('bluebird');
+const _         = require('lodash');
+const beautify  = require('json-beautify');
+const fs        = require('fs');
+
 function ExampleStore(path) {
   this.rootPath = path;
   this.repos = [];
@@ -31,6 +36,18 @@ ExampleStore.prototype.loadRepository = function(repoPath) {
   // .then(() => console.log(this.repos[0]))
 };
 
+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);
+  
+  return fs.mkdirAsync(fullPath)
+  .then(() => fs.writeFileAsync(repoConfigFile, repoConfig))
+  .then(() => self.repos.push(repoDescriptor));
+}
+
 ExampleStore.prototype.loadExample = function(repo, slug) {
   const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
   repo.examples.push(Object.assign({ slug }, exampleConfig));

+ 9 - 0
lib/repoTmpl.json

@@ -0,0 +1,9 @@
+{
+  "defaultCategory": "non-classe",
+  "categories": [
+    {
+      "slug": "non-classe",
+      "title": "Non classé"
+    }
+  ]
+}

+ 32 - 2
sandboxApp.js

@@ -4,7 +4,7 @@
 var express       = require('express');
 var bodyParser    = require('body-parser');
 var slug          = require('slug');
-var beautify      = require("json-beautify");
+var beautify      = require('json-beautify');
 var _             = require('lodash');
 var path          = require('path');
 var Mustache      = require('mustache');
@@ -13,6 +13,7 @@ var fs            = require('fs');
 var Promise       = require('bluebird'); Promise.promisifyAll(fs);
 var sandboxTpml   = fs.readFileSync(__dirname + '/html/template.mustache.html').toString();
 var exampleTmpl   = require('./lib/exampleTmpl.json');
+var repoTmpl      = require('./lib/repoTmpl.json');
 var ExampleStore  = require('./lib/ExampleStore');
 var isTesting     = process.env.NODE_ENV === 'testing';
 var examplesDir   = ! isTesting ? __dirname + '/exemples' :
@@ -121,6 +122,35 @@ console.log('### checkRepoExists', repoSlug);
   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);
+
+  // Prevent duplicate title
+  if(existingRepo) {
+    return res.status(409).send("La collection '" + title + "' existe déjà !");
+  }
+
+  // Prepare config
+  var config = Object.assign({
+    title
+  }, repoTmpl);
+
+  exStore.addRepository(repoSlug, config)
+  .then(repo => res.json(repo));
+
+});
+
 /**
  * Create a new example for specified repo
  */
@@ -138,7 +168,7 @@ app.post('/:repoSlug/examples',
     // Prevent duplicate title
     var existingTitle = _.find(req.repo.examples, { title: title });
     if(existingTitle) {
-      res.status(400).send("L'exemple '" + title + "' existe déjà !");
+      return res.status(409).send("L'exemple '" + title + "' existe déjà !");
     }
     var exampleSlug = slug(req.body.title.toLowerCase());