All files / Zapcard_API/routes user.js

0% Statements 0/41
100% Branches 0/0
0% Functions 0/6
0% Lines 0/41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
/**
* Module Dependencies
*/
const { Router } = require('restify-router');
 
const router = new Router();
/**
* Controllers
*/
const UserController = require('../controllers/user');
 
/**
 * @api {get} /users list all users
 * @apiDescription List all the users in the system
 * @apiGroup Users
 * @apisamplerequest off
 * @apiPermission none
 * @apiHeader Content-Type application/json
 * @apiHeader If-None-Match (optional) the eTag hash from the last request
 * @apiSuccess {Object} response top-level object
 * @apiSuccess {Array} response.users an array of users
 * @apiSuccess {String} response.users.username the username of the user
 * @apiSuccess {String} response.users.name the real name of the user
 * @apiSuccess {String} response.users.about small description about the user
 * @apiSuccess {String} response.users.profilePicture checksum of profile picture file
 * @apiSuccess {String} response.users.email the email of the user
 * @apiExample {curl} Example usage:
 *   curl -H 'Authorization: Bearer xxx' -i http://api.example.com/users
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *     "users": [
 *       {
 *         "username": "john_doe",
 *         "name": "John Doe",
 *         "about": "Sample description",
 *         "profilePicture": "xxxxxxxxxx",
 *         "email": "john@doe.com"
 *       },
 *       {
 *         "username": "jane_doe",
 *         "name": "Jane",
 *         "about": "Sample description",
 *         "profilePicture": "yyyyyyyyyy",
 *         "email": "jane@doe.com"
 *       }
 *     ]
 *   }
 */
router.get('/', async (req, res) => {
  try {
    res.setHeader('content-type', 'application/json');
    res.setHeader('accepts', 'GET');
    res.send(200, await UserController.list());
  } catch (err) {
    res.send(400, err);
  }
});
/**
 * @api {post} /users create a new user
 * @apiDescription Create a new user in the system
 * @apiGroup Users
 * @apisamplerequest off
 * @apiPermission none
 * @apiHeader Content-Type application/json
 * @apiHeader If-None-Match (optional) the eTag hash from the last request
 * @apiSuccess {Object} response top-level object
 * @apiSuccess {Object} response.user the created user object
 * @apiSuccess {String} response.user.username the username of the user
 * @apiSuccess {String} response.user.name the real name of the user
 * @apiSuccess {String} response.user.about small description about the user
 * @apiSuccess {String} response.user.profilePicture checksum of profile picture file
 * @apiSuccess {String} response.user.email the email of the user
 * @apiExample {curl} Example usage:
 *   curl -X POST -H 'Authorization: Bearer xxx' -H "Content-type: application/json" -d '{username: "John", password: "Pass", name:"John Doe", "about": "Sample description"}' 'http://api.example.com/users'
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *     "user": {
 *       "username": "john_doe",
 *       "name": "John Doe",
 *       "about": "Sample description",
 *       "profilePicture": "xxxxxxxxxx",
 *       "email": "john@doe.com"
 *     }
 *   }
 */
router.post('/', async (req, res) => {
  try {
    res.setHeader('content-type', 'application/json');
    res.setHeader('accepts', 'POST');
    const tokenUserObject = await UserController.create(req);
    res.send(201, tokenUserObject);
  } catch (err) {
    res.send(400, err);
  }
});
/**
 * @api {get} /users/:userId retrieve a user by ID
 * @apiDescription Retrieve a user by ID
 * @apiGroup Users
 * @apisamplerequest off
 * @apiPermission none
 * @apiHeader Content-Type application/json
 * @apiHeader Authorization Bearer xxxxx
 * @apiHeader If-None-Match (optional) the eTag hash from the last request
 * @apiSuccess {Object} response top-level object
 * @apiSuccess {Object} response.user the created user object
 * @apiSuccess {String} response.user.username the username of the user
 * @apiSuccess {String} response.user.name the real name of the user
 * @apiSuccess {String} response.user.about small description about the user
 * @apiSuccess {String} response.user.profilePicture checksum of profile picture file
 * @apiSuccess {String} response.user.email the email of the user
 * @apiExample {curl} Example usage:
 *   curl -H 'Authorization: Bearer xxx' -i http://api.example.com/users
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *     "user": {
 *       "username": "john_doe",
 *       "name": "John Doe",
 *       "about": "Sample description",
 *       "profilePicture": "xxxxxxxxxx",
 *       "email": "john@doe.com"
 *     }
 *   }
 */
router.get('/:userId', async (req, res) => {
  try {
    res.setHeader('content-type', 'application/json');
    res.setHeader('accepts', 'GET');
    res.send(200, await UserController.read(req));
  } catch (err) {
    res.send(400, err);
  }
});
/**
 * @api {put} /users/:userId update a user by ID
 * @apiDescription Update a user by ID
 * @apiGroup Users
 * @apisamplerequest off
 * @apiPermission none
 * @apiHeader Content-Type application/json
 * @apiHeader Authorization Bearer xxxxx
 * @apiHeader If-None-Match (optional) the eTag hash from the last request
 * @apiSuccess {Object} response top-level object
 * @apiSuccess {Object} response.user the created user object
 * @apiSuccess {String} response.user.username the username of the user
 * @apiSuccess {String} response.user.name the real name of the user
 * @apiSuccess {String} response.user.about small description about the user
 * @apiSuccess {String} response.user.profilePicture checksum of profile picture file
 * @apiSuccess {String} response.user.email the email of the user
 * @apiExample {curl} Example usage:
 *   curl -X PUT -H 'Authorization: Bearer xxx' -H "Content-type: application/json" -d '{name:"John Doe", "about": "Updated description"}' 'http://api.example.com/users/:userId'
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 *   {
 *     "user": {
 *       "username": "john_doe",
 *       "name": "John Doe",
 *       "about": "Updated description",
 *       "profilePicture": "xxxxxxxxxx",
 *       "email": "john@doe.com"
 *     }
 *   }
 */
router.put('/:userId', async (req, res) => {
  try {
    res.setHeader('content-type', 'application/json');
    res.setHeader('accepts', 'PUT');
    res.send(200, await UserController.update(req));
  } catch (err) {
    res.send(400, err);
  }
});
/**
 * @api {delete} /users/:userId delete a user by ID
 * @apiDescription Delete a user by ID
 * @apiGroup Users
 * @apisamplerequest off
 * @apiPermission none
 * @apiHeader Content-Type application/json
 * @apiHeader Authorization Bearer xxxxx
 * @apiHeader If-None-Match (optional) the eTag hash from the last request
 * @apiExample {curl} Example usage:
 *   curl -X DELETE -H 'Authorization: Bearer xxx' -H "Content-type: application/json" 'http://api.example.com/users/:userId'
 * @apiSuccessExample {json} Success-Response:
 *   HTTP/1.1 200 OK
 */
router.del('/:userId', async (req, res) => {
  try {
    res.setHeader('content-type', 'application/json');
    res.setHeader('accepts', 'DELETE');
    res.send(200, await UserController.del(req));
  } catch (err) {
    res.send(400, err);
  }
});
/**
 * @api {get} /users/:userId/picture retrieve user profile picture
 * @apiDescription Retrieve user profile picture
 * @apiGroup Users
 * @apisamplerequest off
 * @apiPermission none
 * @apiHeader Content-Type application/json
 * @apiHeader Authorization Bearer xxxxx
 * @apiHeader If-None-Match (optional) the eTag hash from the last request
 * @apiExample {curl} Example usage:
 *   curl -X GET -H 'Authorization: Bearer xxx' -H "Content-type: application/json" 'http://api.example.com/users/:userId/picture'
 * @apiSuccessExample {Buffer} Success-Response:
 *   HTTP/1.1 200 OK
 * Buffer
 */
router.get('/:userId/picture', async (req, res) => {
  try {
    res.setHeader('content-type', 'image/png');
    res.setHeader('accepts', 'GET');
    res.send(200, await UserController.picture(req));
  } catch (err) {
    res.send(400, err);
  }
});
 
module.exports = router;