Просмотр исходного кода

Update to include method for getting all societies

Matt Coles лет назад: 9
Родитель
Сommit
e95378546e
3 измененных файлов с 41 добавлено и 4 удалено
  1. 31 0
      presenters/society-controller.js
  2. 9 3
      routes/society/view-society.js
  3. 1 1
      utils/route-manager.js

+ 31 - 0
presenters/society-controller.js

132
     });
132
     });
133
   },
133
   },
134
 
134
 
135
+  get_all_societies: function (complete) {
136
+    var self = this;
137
+    var stream = redis.scanStream({
138
+      match: "society:*"
139
+    });
140
+    var society_names = [];
141
+    stream.on('data', function (keys) {
142
+      keys.map(function(key) {
143
+        society_names.push(key.split(":")[1]);
144
+      });
145
+    });
146
+    stream.on('end', function () {
147
+      var soc_objects = [];
148
+      for (var ii = 0; ii < society_names.length; ii++) {
149
+        self.get_society(society_names[ii], function(response) {
150
+          soc_objects.push(response.society);
151
+          if (soc_objects.length === society_names.length) {
152
+            soc_objects.sort(function(a, b) {
153
+              var textA = a.name.toLowerCase();
154
+              var textB = b.name.toLowerCase();
155
+              return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
156
+            });
157
+            complete({
158
+              "societies": soc_objects
159
+            });
160
+          }
161
+        });
162
+      }
163
+    })
164
+  },
165
+
135
   leave_society: function (soc_name, auth, complete) {
166
   leave_society: function (soc_name, auth, complete) {
136
     // permissions_controller.user_is_in_society(auth, soc_name, function)
167
     // permissions_controller.user_is_in_society(auth, soc_name, function)
137
   }
168
   }

+ 9 - 3
routes/society/view-society.js

7
 }
7
 }
8
 
8
 
9
 var perform = function (req, res) {
9
 var perform = function (req, res) {
10
-  society_controller.get_society(req.params.societyid, function (result) {
11
-    res.send(result);
12
-  })
10
+  if (req.params.societyid) {
11
+    society_controller.get_society(req.params.societyid, function (result) {
12
+      res.send(result);
13
+    });
14
+  } else {
15
+    society_controller.get_all_societies(function (result) {
16
+      res.send(result);
17
+    });
18
+  }
13
 };
19
 };

+ 1 - 1
utils/route-manager.js

14
 router.all('/user/auth/', login.perform);
14
 router.all('/user/auth/', login.perform);
15
 
15
 
16
 router.all('/society/create', soc_create.perform);
16
 router.all('/society/create', soc_create.perform);
17
-router.all('/society/view/:societyid', soc_view.perform);
17
+router.all('/society/view/(:societyid)?', soc_view.perform);