Procházet zdrojové kódy

Implement route for creating new societies

Matt Coles před 9 roky
rodič
revize
8fcfaca431
4 změnil soubory, kde provedl 74 přidání a 1 odebrání
  1. 29 0
      README.md
  2. 1 0
      index.js
  3. 1 0
      package.json
  4. 43 1
      routes/society/create-society.js

+ 29 - 0
README.md

@@ -84,3 +84,32 @@ existing authentication key for that account. Note that you do not need to use
84 84
 The error codes are as follows, `1` indicates the username could not be found,
85 85
 `2` indicates that the password is invalid and `3` indicates that the login
86 86
 request was malformed.
87
+
88
+### /society/create/
89
+To create a new society, a `POST` request should be sent with the following
90
+data:
91
+```javascript
92
+{
93
+    "society": "FooBarSociety", // The name of the society to be created.
94
+    "admins": ["FooBar", "BarFoo", "FarBoo"], // List of initial admins to be added, this list MUST include the user creating the society
95
+    "description": "A description of the FooBarSociety society.",
96
+    "auth": "$2a$10$.X9YrNyd2R7b2ycAumHn.ONiINs2bCkRDupugu6sjZkUkPmXSaSra"
97
+}
98
+```
99
+If the society does not already exist, the new values will be added to the
100
+database and a response will be sent looking like this:
101
+```javascript
102
+{
103
+    "success": 1, // Indicates if a society was successfully created.
104
+    "society": {
105
+        "name": "FooBarSociety",
106
+        "admins": ["FooBar", "BarFoo", "FarBoo"],
107
+        "description": "A description of the FooBarSociety society.",
108
+    }, // An object representing the society
109
+    "error": 0
110
+}
111
+```
112
+The error codes are as follows, `1` indicates a malformed request, `2` indicates
113
+that a society with that name already exists, and `3` indicates that the user
114
+does not have authorisation to create that society. (Note that the admin list
115
+must contain the username that is creating it.)

+ 1 - 0
index.js

@@ -10,6 +10,7 @@ var certi = fs.readFileSync('server.crt');
10 10
 app.use(bodyParser.json()); // for parsing application/json
11 11
 app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
12 12
 app.use('/', route_manager);
13
+app.use('/source', require('extraverse')('/source', { ignore: ['config'] }));
13 14
 
14 15
 // app.listen(3000, function () {
15 16
 //     console.log('Example app listening on port 3000!');

+ 1 - 0
package.json

@@ -12,6 +12,7 @@
12 12
     "bcrypt-nodejs": "0.0.3",
13 13
     "body-parser": "^1.15.0",
14 14
     "express": "^4.13.4",
15
+    "extraverse": "^1.0.27",
15 16
     "ioredis": "^1.15.1"
16 17
   }
17 18
 }

+ 43 - 1
routes/society/create-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,44 @@ module.exports = {
5 8
 }
6 9
 
7 10
 var perform = function(req,res) {
8
-  res.send("Attempted to create society");
11
+  var society_name = req.body.society || req.query.society;
12
+  var auth_key = req.body.auth || req.query.auth;
13
+  var admins = req.body.admins || req.query.admins;
14
+  var description = req.body.description || req.query.description;
15
+
16
+  if (society_name && auth_key && admins && description) {
17
+    var soc_query = "society:" + society_name;
18
+    society_name = decodeURIComponent(society_name);
19
+    admins = decodeURIComponent(admins);
20
+    description = decodeURIComponent(description);
21
+    admins = JSON.parse(admins);
22
+    redis.hget(soc_query, "name", function (err, result) {
23
+      if (result) {
24
+        res.send({"success": 0,
25
+                  "error": 2});
26
+      } else {
27
+        redis.get("auth-key:" + auth_key, function (err,result) {
28
+          var username = result;
29
+          if (admins.indexOf(result) !== -1) {
30
+            redis.hset(soc_query, "name", society_name);
31
+            redis.hset(soc_query, "admins", JSON.stringify(admins));
32
+            redis.hset(soc_query, "description", description);
33
+            res.send({"success": 1,
34
+                      "society" : {
35
+                       "name": society_name,
36
+                       "admins": JSON.stringify(admins),
37
+                       "description": description
38
+                      },
39
+                      "error": 0});
40
+          } else {
41
+            res.send({"success": 0,
42
+                      "error": 3});
43
+          }
44
+        });
45
+      }
46
+    });
47
+  } else {
48
+    res.send({"success": 0,
49
+              "error": 1})
50
+  }
9 51
 }