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

Implement API route for viewing a created society

Matt Coles лет назад: 9
Родитель
Сommit
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
     * [/user/register/](#userregister)
11
     * [/user/register/](#userregister)
12
     * [/user/auth/](#userauth)
12
     * [/user/auth/](#userauth)
13
     * [/society/create/](#societycreate)
13
     * [/society/create/](#societycreate)
14
+    * [/society/view/](#societyview)
14
 
15
 
15
 ### Installation
16
 ### Installation
16
 Instructions are for OSX El Capitan at time of writing.
17
 Instructions are for OSX El Capitan at time of writing.
106
         "name": "FooBarSociety",
107
         "name": "FooBarSociety",
107
         "admins": ["FooBar", "BarFoo", "FarBoo"],
108
         "admins": ["FooBar", "BarFoo", "FarBoo"],
108
         "description": "A description of the FooBarSociety society.",
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
     }, // An object representing the society
111
     }, // An object representing the society
110
     "error": 0
112
     "error": 0
111
 }
113
 }
114
 that a society with that name already exists, and `3` indicates that the user
116
 that a society with that name already exists, and `3` indicates that the user
115
 does not have authorisation to create that society. (Note that the admin list
117
 does not have authorisation to create that society. (Note that the admin list
116
 must contain the username that is creating it.)
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
     society_name = decodeURIComponent(society_name);
18
     society_name = decodeURIComponent(society_name);
19
     admins = decodeURIComponent(admins);
19
     admins = decodeURIComponent(admins);
20
     description = decodeURIComponent(description);
20
     description = decodeURIComponent(description);
21
+    var admins_str = admins;
21
     admins = JSON.parse(admins);
22
     admins = JSON.parse(admins);
22
     redis.hget(soc_query, "name", function (err, result) {
23
     redis.hget(soc_query, "name", function (err, result) {
23
       if (result) {
24
       if (result) {
28
           var username = result;
29
           var username = result;
29
           if (admins.indexOf(result) !== -1) {
30
           if (admins.indexOf(result) !== -1) {
30
             redis.hset(soc_query, "name", society_name);
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
             redis.hset(soc_query, "description", description);
33
             redis.hset(soc_query, "description", description);
34
+            redis.hset(soc_query, "users", admins_str);
33
             res.send({"success": 1,
35
             res.send({"success": 1,
34
                       "society" : {
36
                       "society" : {
35
                        "name": society_name,
37
                        "name": society_name,
36
-                       "admins": JSON.stringify(admins),
37
-                       "description": description
38
+                       "admins": admins,
39
+                       "description": description,
40
+                       "users": admins
38
                       },
41
                       },
39
                       "error": 0});
42
                       "error": 0});
40
           } else {
43
           } else {

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

1
+var Redis = require("ioredis");
2
+var redis = new Redis();
3
+
1
 module.exports = {
4
 module.exports = {
2
   perform: function (a,b) {
5
   perform: function (a,b) {
3
     perform(a,b);
6
     perform(a,b);
5
 }
8
 }
6
 
9
 
7
 var perform = function (req, res) {
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
 };