Преглед на файлове

Implement API route for viewing a created society

Matt Coles преди 9 години
родител
ревизия
469fa577a4
променени са 3 файла, в които са добавени 42 реда и са изтрити 4 реда
  1. 18 0
      README.md
  2. 6 3
      routes/society/create-society.js
  3. 18 1
      routes/society/view-society.js

+ 18 - 0
README.md

@@ -11,6 +11,7 @@ Server for Integrated Project, powered by Express.js and Redis, listens only on
11 11
     * [/user/register/](#userregister)
12 12
     * [/user/auth/](#userauth)
13 13
     * [/society/create/](#societycreate)
14
+    * [/society/view/](#societyview)
14 15
 
15 16
 ### Installation
16 17
 Instructions are for OSX El Capitan at time of writing.
@@ -106,6 +107,7 @@ database and a response will be sent looking like this:
106 107
         "name": "FooBarSociety",
107 108
         "admins": ["FooBar", "BarFoo", "FarBoo"],
108 109
         "description": "A description of the FooBarSociety society.",
110
+        "users": ["FooBar", "BarFoo", "FarBoo"] // At this point the users will simply be the admin list
109 111
     }, // An object representing the society
110 112
     "error": 0
111 113
 }
@@ -114,3 +116,19 @@ The error codes are as follows, `1` indicates a malformed request, `2` indicates
114 116
 that a society with that name already exists, and `3` indicates that the user
115 117
 does not have authorisation to create that society. (Note that the admin list
116 118
 must contain the username that is creating it.)
119
+
120
+### /society/view/:society\_name
121
+To view a created society, :society\_name, a `GET` request should be sent with
122
+no data. The response will then be formed as follows:
123
+```javascript
124
+{
125
+    "society": { // Society object containing information about the society
126
+        "name": "FooBarSociety",
127
+        "admins": ["FooBar", "BarFoo", "FarBoo"],
128
+        "description": "A description of the FooBarSociety society.",
129
+        "users": ["FooBar", "BarFoo", "FarBoo"]
130
+    },
131
+    "error": 0 // Error code if an error occured, 0 indicates no error.
132
+}
133
+```
134
+The error codes are as follows, `1` indicates that the society does not exist.

+ 6 - 3
routes/society/create-society.js

@@ -18,6 +18,7 @@ var perform = function(req,res) {
18 18
     society_name = decodeURIComponent(society_name);
19 19
     admins = decodeURIComponent(admins);
20 20
     description = decodeURIComponent(description);
21
+    var admins_str = admins;
21 22
     admins = JSON.parse(admins);
22 23
     redis.hget(soc_query, "name", function (err, result) {
23 24
       if (result) {
@@ -28,13 +29,15 @@ var perform = function(req,res) {
28 29
           var username = result;
29 30
           if (admins.indexOf(result) !== -1) {
30 31
             redis.hset(soc_query, "name", society_name);
31
-            redis.hset(soc_query, "admins", JSON.stringify(admins));
32
+            redis.hset(soc_query, "admins", admins_str);
32 33
             redis.hset(soc_query, "description", description);
34
+            redis.hset(soc_query, "users", admins_str);
33 35
             res.send({"success": 1,
34 36
                       "society" : {
35 37
                        "name": society_name,
36
-                       "admins": JSON.stringify(admins),
37
-                       "description": description
38
+                       "admins": admins,
39
+                       "description": description,
40
+                       "users": admins
38 41
                       },
39 42
                       "error": 0});
40 43
           } else {

+ 18 - 1
routes/society/view-society.js

@@ -1,3 +1,6 @@
1
+var Redis = require("ioredis");
2
+var redis = new Redis();
3
+
1 4
 module.exports = {
2 5
   perform: function (a,b) {
3 6
     perform(a,b);
@@ -5,5 +8,19 @@ module.exports = {
5 8
 }
6 9
 
7 10
 var perform = function (req, res) {
8
-  res.send("Attempting to view society: " + req.params.societyid);
11
+  var auth_key = req.body.auth || req.query.auth;
12
+
13
+  redis.hgetall("society:" + req.params.societyid).then(function (result) {
14
+    if (result.name) {
15
+      result.users = JSON.parse(result.users);
16
+      result.admins = JSON.parse(result.admins);
17
+      res.send({
18
+        "society": result,
19
+        "error": 0
20
+      });
21
+    } else {
22
+      res.send({"society": {},
23
+               "error": 1});
24
+    }
25
+  });
9 26
 };