|
1 | 1 | var express = require('express');
|
2 |
| -var app = express(); |
| 2 | +var bodyParser = require('body-parser'); |
3 | 3 |
|
4 | 4 | var BordeauxWines = require('./data/bordeaux-wines.json');
|
5 | 5 | var BurgundyWines = require('./data/burgundy-wines.json');
|
@@ -35,9 +35,11 @@ var Comments = {
|
35 | 35 | }]
|
36 | 36 | };
|
37 | 37 |
|
| 38 | +// Create Server |
| 39 | +var app = express(); |
| 40 | +app.use(bodyParser.json()); |
38 | 41 | // Serve API documentation
|
39 | 42 | app.use(express.static('doc'));
|
40 |
| - |
41 | 43 | // Configure CORS
|
42 | 44 | app.use(function(req, res, next) {
|
43 | 45 | res.header("Access-Control-Allow-Origin", "*");
|
@@ -215,6 +217,41 @@ app.get('/api/wines/:id/comments', function (req, res) {
|
215 | 217 | }
|
216 | 218 | });
|
217 | 219 |
|
| 220 | +/** |
| 221 | + * @api {post} /wines/:id/comments Comment |
| 222 | + * @apiName Comment |
| 223 | + * @apiGroup Wines |
| 224 | + * |
| 225 | + * @apiParam {String} id the id of the wine |
| 226 | + * @apiParam {String} title title of the comment. |
| 227 | + * @apiParam {String} content content of the comment. |
| 228 | + * |
| 229 | + * @apiSuccessExample {json} Success-Response: |
| 230 | + * HTTP/1.1 201 Created |
| 231 | + * |
| 232 | + * @apiError {String} 404 Not found - No wine corresponding to given 'id' |
| 233 | + * @apiError {String} 400 Bad request - missing 'title' or 'content' attribute in body |
| 234 | + */ |
| 235 | +app.post('/api/wines/:id/comments', function (req, res) { |
| 236 | + var id = req.params.id; |
| 237 | + if (!WinesById[id]) { |
| 238 | + res.sendStatus(404); |
| 239 | + } else { |
| 240 | + var body = req.body; |
| 241 | + if (!body.title || !body.content) { |
| 242 | + res.sendStatus(400); |
| 243 | + } else { |
| 244 | + var newComment = { |
| 245 | + date: new Date(), |
| 246 | + title: body.title, |
| 247 | + content: body.content |
| 248 | + }; |
| 249 | + Comments[id] = (Comments[id] || []).concat(newComment); |
| 250 | + res.sendStatus(201); |
| 251 | + } |
| 252 | + } |
| 253 | +}); |
| 254 | + |
218 | 255 | /**
|
219 | 256 | * @api {get} /regions All
|
220 | 257 | * @apiName All
|
|
0 commit comments