Johannes Zellner 11 лет назад
Родитель
Сommit
5191d89115
6 измененных файлов с 75 добавлено и 20 удалено
  1. 2 0
      app.js
  2. 1 0
      package.json
  3. 4 1
      public/index.css
  4. 10 11
      public/index.html
  5. 26 0
      public/index.js
  6. 32 8
      src/things.js

+ 2 - 0
app.js

@@ -20,6 +20,8 @@ router.get ('/api/things', things.getAll);
 router.get ('/api/things/:id', things.get);
 router.get ('/api/things/:id', things.get);
 router.del ('/api/things/:id', things.del);
 router.del ('/api/things/:id', things.del);
 
 
+router.get ('/api/tags', things.getTags);
+
 app
 app
     .use(json({ strict: true })) // only parse objects and arrays
     .use(json({ strict: true })) // only parse objects and arrays
     .use(serveStatic(__dirname + '/public'))
     .use(serveStatic(__dirname + '/public'))

+ 1 - 0
package.json

@@ -13,6 +13,7 @@
   "author": "",
   "author": "",
   "license": "ISC",
   "license": "ISC",
   "dependencies": {
   "dependencies": {
+    "async": "^1.5.0",
     "body-parser": "^1.14.1",
     "body-parser": "^1.14.1",
     "connect-lastmile": "0.0.13",
     "connect-lastmile": "0.0.13",
     "express": "^4.13.3",
     "express": "^4.13.3",

+ 4 - 1
public/index.css

@@ -18,5 +18,8 @@
 
 
 .thing-title-date,.btn {
 .thing-title-date,.btn {
 	margin: 0;
 	margin: 0;
-	margin-right: 30px;
 }
 }
+
+.modal-content .modal-body {
+	padding-top: 0;
+}

+ 10 - 11
public/index.html

@@ -33,13 +33,11 @@
         <h4 class="modal-title">Add something</h4>
         <h4 class="modal-title">Add something</h4>
       </div>
       </div>
       <div class="modal-body">
       <div class="modal-body">
-        <p>
-          <textarea class="form-control" rows="5" ng-model="addFormData.content" autofocus></textarea>
-        </p>
+        <textarea class="form-control" id="inputContent" rows="5" ng-model="addFormData.content" autofocus></textarea>
       </div>
       </div>
       <div class="modal-footer">
       <div class="modal-footer">
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
-        <button type="button" class="btn btn-primary" ng-click="addThing()">Save</button>
+        <button type="button" class="btn btn-success" ng-click="addThing()">Save</button>
       </div>
       </div>
     </div>
     </div>
   </div>
   </div>
@@ -60,14 +58,15 @@
     </div>
     </div>
   </div>
   </div>
   <div class="row">
   <div class="row">
-    <div class="col-md-12">
+    <div class="col-md-2">
+      <button class="btn btn-primary btn-sm" ng-repeat="tag in tags" ng-click="addFilterTag(tag)">{{tag.name}}</button>
+    </div>
+    <div class="col-md-8">
       <div class="well well-sm thing" ng-repeat="thing in things">
       <div class="well well-sm thing" ng-repeat="thing in things">
-        <div class="container thing-title">
-          <div class="row">
-            <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>
-            </div>
+        <div class="row">
+          <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>
           </div>
           </div>
         </div>
         </div>
         <div marked="thing.content"></div>
         <div marked="thing.content"></div>

+ 26 - 0
public/index.js

@@ -15,12 +15,25 @@ app.config(['markedProvider', function (markedProvider) {
 app.controller('MainController', function ($scope, $http, $timeout) {
 app.controller('MainController', function ($scope, $http, $timeout) {
 
 
     $scope.things = [];
     $scope.things = [];
+    $scope.tags = [];
     $scope.filter = '';
     $scope.filter = '';
     $scope.addFormData = {
     $scope.addFormData = {
         busy: true,
         busy: true,
         content: ''
         content: ''
     };
     };
 
 
+    $scope.addFilterTag = function (tag) {
+        var hashed = '#' + tag.name;
+
+        if ($scope.filter.indexOf(hashed) === -1)  {
+            $scope.filter = $scope.filter + ' ' + hashed;
+        } else {
+            $scope.filter = $scope.filter.replace(hashed, '');
+        }
+
+        $scope.fetchThings();
+    };
+
     $scope.addThing = function () {
     $scope.addThing = function () {
         $scope.addFormData.busy = true;
         $scope.addFormData.busy = true;
 
 
@@ -59,11 +72,24 @@ app.controller('MainController', function ($scope, $http, $timeout) {
         });
         });
     };
     };
 
 
+    $scope.fetchTags = function () {
+        $http.get('/api/tags', {}).then(function (result) {
+            angular.copy(result.data.tags, $scope.tags);
+        }, function (result) {
+            console.error('error:', result);
+        });
+    };
+
 
 
     $scope.fetchThings();
     $scope.fetchThings();
+    $scope.fetchTags();
 
 
     $timeout(function () {
     $timeout(function () {
         $.material.init();
         $.material.init();
+
+        $('#modalAdd').on('shown.bs.modal', function () {
+            $('#inputContent').focus();
+        });
     });
     });
 });
 });
 
 

+ 32 - 8
src/things.js

@@ -4,6 +4,7 @@
 
 
 var MongoClient = require('mongodb').MongoClient,
 var MongoClient = require('mongodb').MongoClient,
     ObjectId = require('mongodb').ObjectID,
     ObjectId = require('mongodb').ObjectID,
+    async = require('async'),
     HttpError = require('connect-lastmile').HttpError,
     HttpError = require('connect-lastmile').HttpError,
     HttpSuccess = require('connect-lastmile').HttpSuccess;
     HttpSuccess = require('connect-lastmile').HttpSuccess;
 
 
@@ -12,11 +13,12 @@ exports = module.exports = {
     getAll: getAll,
     getAll: getAll,
     get: get,
     get: get,
     add: add,
     add: add,
-    del: del
+    del: del,
+    getTags: getTags
 };
 };
 
 
 var databaseUrl = process.env.MONGODB_URL || 'mongodb://127.0.0.1:27017/guacamoly';
 var databaseUrl = process.env.MONGODB_URL || 'mongodb://127.0.0.1:27017/guacamoly';
-var g_db, g_things;
+var g_db, g_things, g_tags;
 
 
 function sanitize(data) {
 function sanitize(data) {
     data = data.replace(/##/g, data);
     data = data.replace(/##/g, data);
@@ -25,7 +27,7 @@ function sanitize(data) {
 
 
 function facelift(data, tags) {
 function facelift(data, tags) {
     tags.forEach(function (tag) {
     tags.forEach(function (tag) {
-        data = data.replace(tag, '[' + tag + '](' + tag + ')', 'g');
+        data = data.replace(tag, '[' + tag.slice(1) + '](#' + tag.slice(1) + ')', 'g');
     });
     });
 
 
     return data;
     return data;
@@ -48,6 +50,7 @@ function init(callback) {
 
 
         g_db = db;
         g_db = db;
         g_things = db.collection('things');
         g_things = db.collection('things');
+        g_tags = db.collection('tags');
 
 
         callback(null);
         callback(null);
     });
     });
@@ -93,19 +96,30 @@ function add(req, res, next) {
         tags: tags
         tags: tags
     };
     };
 
 
-    g_things.insertMany([doc], function (error, result) {
-        if (error || !result) return next(new HttpError(500, error));
+    async.eachSeries(tags, function (tag, callback) {
+        g_tags.update({ name: tag.slice(1) }, {
+            $inc: { usage: 1 },
+            $set: {
+                name: tag.slice(1)
+            }
+        }, { upsert:true }, callback);
+    }, function (error) {
+        if (error) return next(new HttpError(500, error));
 
 
-        console.log('done', result);
+        g_things.insertMany([doc], function (error, result) {
+            if (error || !result) return next(new HttpError(500, error));
+
+            console.log('done', result);
 
 
-        next(new HttpSuccess(201, { id: result._id }));
+            next(new HttpSuccess(201, { id: result._id }));
+        });
     });
     });
 }
 }
 
 
 function del(req, res, next) {
 function del(req, res, next) {
     console.log('del', req.params.id);
     console.log('del', req.params.id);
 
 
-    g_things.deleteOne({ _id: ObjectId(req.params.id) }, function (error, result) {
+    g_things.deleteOne({ _id: new ObjectId(req.params.id) }, function (error, result) {
         if (error || !result) return next(new HttpError(500, error));
         if (error || !result) return next(new HttpError(500, error));
 
 
         console.log('done', result.result);
         console.log('done', result.result);
@@ -113,3 +127,13 @@ function del(req, res, next) {
         next(new HttpSuccess(200, {}));
         next(new HttpSuccess(200, {}));
     });
     });
 }
 }
+
+function getTags(req, res, next) {
+    g_tags.find({}).sort({ createdAt: -1 }).toArray(function (error, result) {
+        if (error || !result) return next(new HttpError(500, error));
+
+        console.log('done', result);
+
+        next(new HttpSuccess(200, { tags: result }));
+    });
+}