Johannes Zellner 11 лет назад
Родитель
Сommit
6dc7f3d6e6
3 измененных файлов с 35 добавлено и 4 удалено
  1. 1 1
      public/index.html
  2. 20 1
      public/index.js
  3. 14 2
      src/things.js

+ 1 - 1
public/index.html

@@ -56,7 +56,7 @@
     <div class="col-md-6 col-md-offset-3">
       <div class="form-group has-success">
         <label class="control-label" for="inputFilter">Filter</label>
-        <input class="form-control" id="inputFilter" type="text" ng-model="filter"/>
+        <input class="form-control" id="inputFilter" type="text" ng-model="filter" ng-enter="fetchThings()"/>
       </div>
     </div>
   </div>

+ 20 - 1
public/index.js

@@ -43,7 +43,13 @@ app.controller('MainController', function ($scope, $http, $timeout) {
     };
 
     $scope.fetchThings = function () {
-        $http.get('/api/things', {}).then(function (result) {
+        var url = '/api/things';
+
+        if ($scope.filter) {
+            url += '?filter=' + encodeURIComponent($scope.filter);
+        }
+
+        $http.get(url, {}).then(function (result) {
             angular.copy(result.data.things, $scope.things);
         }, function (result) {
             console.error('error:', result);
@@ -103,3 +109,16 @@ app.filter('prettyDate', function () {
     };
 });
 
+app.directive('ngEnter', function () {
+    return function (scope, element, attrs) {
+        element.bind('keydown keypress', function (event) {
+            if(event.which === 13) {
+                scope.$apply(function (){
+                    scope.$eval(attrs.ngEnter);
+                });
+
+                event.preventDefault();
+            }
+        });
+    };
+});

+ 14 - 2
src/things.js

@@ -30,7 +30,15 @@ function init(callback) {
 }
 
 function getAll(req, res, next) {
-    g_things.find({}).sort({ createdAt: -1 }).toArray(function (error, result) {
+    var query = {};
+
+    if (req.query && req.query.filter) {
+        query = {
+            tags: { $in: req.query.filter.split(' ') }
+        };
+    }
+
+    g_things.find(query).sort({ createdAt: -1 }).toArray(function (error, result) {
         if (error || !result) return next(new HttpError(500, error));
 
         console.log('done', result);
@@ -52,9 +60,13 @@ function get(req, res, next) {
 function add(req, res, next) {
     console.log('add', req.body);
 
+    var tags = req.body.content.match(/\B#([^ ]+)/g);
+    if (tags === null) tags = [];
+
     var doc = {
         content: req.body.content,
-        createdAt: new Date()
+        createdAt: new Date(),
+        tags: tags
     };
 
     g_things.insertMany([doc], function (error, result) {