Johannes Zellner 11 лет назад
Родитель
Сommit
6dcf44bf4a
5 измененных файлов с 82 добавлено и 6 удалено
  1. 2 3
      CloudronManifest.json
  2. 1 0
      app.js
  3. 20 1
      public/index.html
  4. 32 0
      public/index.js
  5. 27 2
      src/routes.js

+ 2 - 3
CloudronManifest.json

@@ -10,11 +10,10 @@
   "addons": {
     "mongodb": {}
   },
-  "minBoxVersion": "0.0.72",
-  "targetBoxVersion": "0.0.72",
+  "targetBoxVersion": "0.4.0",
   "manifestVersion": 1,
   "website": "http://nebulon.de",
   "contactEmail": "johannes@nebulon.de",
   "icon": "logo.png",
-  "mediaLinks": [ ]
+  "mediaLinks": []
 }

+ 1 - 0
app.js

@@ -18,6 +18,7 @@ router.del = router.delete;
 router.post('/api/things', routes.add);
 router.get ('/api/things', routes.getAll);
 router.get ('/api/things/:id', routes.get);
+router.put ('/api/things/:id', routes.put);
 router.del ('/api/things/:id', routes.del);
 
 router.get ('/api/tags', routes.getTags);

+ 20 - 1
public/index.html

@@ -46,6 +46,24 @@
   </div>
 </div>
 
+<div class="modal fade" id="modalEdit">
+  <div class="modal-dialog">
+    <div class="modal-content">
+      <div class="modal-header">
+        <button type="button" class="close">×</button>
+        <h4 class="modal-title">Edit</h4>
+      </div>
+      <div class="modal-body">
+        <textarea class="form-control" id="inputContent" rows="5" ng-model="editFormData.content" autofocus></textarea>
+      </div>
+      <div class="modal-footer">
+        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
+        <button type="button" class="btn btn-success" ng-click="editThing()">Save</button>
+      </div>
+    </div>
+  </div>
+</div>
+
 <div class="modal fade" id="modalSettings">
   <div class="modal-dialog">
     <div class="modal-content">
@@ -104,8 +122,9 @@
           <div class="col-md-12 thing-title-date">
             {{thing.createdAt | prettyDateOffset}}
             <button class="btn btn-xs btn-default" ng-click="deleteThing(thing._id)">delete</button>
+            <button class="btn btn-xs btn-default" ng-click="showEditThing(thing._id, thing.content)">edit</button>
         </div>
-        <div class="content" marked="thing.content"></div>
+        <div class="content" marked="thing.richContent"></div>
       </div>
     </div>
   </div>

+ 32 - 0
public/index.js

@@ -22,6 +22,12 @@ app.controller('MainController', function ($scope, $http, $timeout, $document) {
         content: ''
     };
 
+    $scope.editFormData = {
+        busy: true,
+        id: '',
+        content: ''
+    };
+
     $scope.settings = {
         title: 'Holy Guacamoly',
         backgroundUrl: ''
@@ -39,6 +45,14 @@ app.controller('MainController', function ($scope, $http, $timeout, $document) {
         $scope.fetchThings();
     };
 
+    $scope.showEditThing = function (id, content) {
+        $scope.editFormData.busy = false;
+        $scope.editFormData.id = id;
+        $scope.editFormData.content = content;
+
+        $('#modalEdit').modal('show');
+    };
+
     $scope.addThing = function () {
         $scope.addFormData.busy = true;
 
@@ -56,6 +70,24 @@ app.controller('MainController', function ($scope, $http, $timeout, $document) {
         });
     };
 
+    $scope.editThing = function () {
+        $scope.editFormData.busy = true;
+
+        $http.put('/api/things/' + $scope.editFormData.id, { content: $scope.editFormData.content }, {}).then(function (result) {
+            $scope.editFormData.busy = false;
+            $scope.editFormData.id = '';
+            $scope.editFormData.content = '';
+
+            $('#modalEdit').modal('hide');
+
+            $scope.fetchThings();
+            $scope.fetchTags();
+        }, function (result) {
+            console.error('error:', result);
+            $scope.editFormData.busy = false;
+        });
+    };
+
     $scope.deleteThing = function (id) {
         $http.delete('/api/things/' + id, {}).then(function (result) {
             $scope.fetchThings();

+ 27 - 2
src/routes.js

@@ -14,6 +14,7 @@ exports = module.exports = {
     getAll: getAll,
     get: get,
     add: add,
+    put: put,
     del: del,
     getTags: getTags,
     settingsSave: settingsSave,
@@ -97,7 +98,7 @@ function getAll(req, res, next) {
         async.each(result, function (thing, callback) {
             var tags = extractTags(thing.content);
             facelift(thing.content, tags, function (data) {
-                thing.content = data;
+                thing.richContent = data;
 
                 callback(null);
             });
@@ -137,7 +138,31 @@ function add(req, res, next) {
     }, function (error) {
         if (error) return next(new HttpError(500, error));
 
-        g_things.insertMany([doc], function (error, result) {
+        g_things.insert(doc, function (error, result) {
+            if (error || !result) return next(new HttpError(500, error));
+
+            next(new HttpSuccess(201, { id: result._id }));
+        });
+    });
+}
+
+function put(req, res, next) {
+    console.log('put', req.body);
+
+    var tags = extractTags(req.body.content);
+    var data = sanitize(req.body.content);
+
+    async.eachSeries(tags, function (tag, callback) {
+        g_tags.update({ name: tag }, {
+            $inc: { usage: 1 },
+            $set: {
+                name: tag
+            }
+        }, { upsert:true }, callback);
+    }, function (error) {
+        if (error) return next(new HttpError(500, error));
+
+        g_things.update({_id: new ObjectId(req.params.id) }, { $set: { content: data, tags: tags, modifiedAt: new Date() } }, function (error, result) {
             if (error || !result) return next(new HttpError(500, error));
 
             next(new HttpSuccess(201, { id: result._id }));