Skip to content

Commit d0770bb

Browse files
committed
Add route to post comments
1 parent d3637f2 commit d0770bb

File tree

6 files changed

+175
-5
lines changed

6 files changed

+175
-5
lines changed

api/doc/api_data.js

+66
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,72 @@ define({ "api": [
247247
"filename": "src/server.js",
248248
"groupTitle": "Wines"
249249
},
250+
{
251+
"type": "post",
252+
"url": "/wines/:id/comments",
253+
"title": "Comment",
254+
"name": "Comment",
255+
"group": "Wines",
256+
"parameter": {
257+
"fields": {
258+
"Parameter": [
259+
{
260+
"group": "Parameter",
261+
"type": "String",
262+
"optional": false,
263+
"field": "id",
264+
"description": "<p>the id of the wine</p>"
265+
},
266+
{
267+
"group": "Parameter",
268+
"type": "String",
269+
"optional": false,
270+
"field": "title",
271+
"description": "<p>title of the comment.</p>"
272+
},
273+
{
274+
"group": "Parameter",
275+
"type": "String",
276+
"optional": false,
277+
"field": "content",
278+
"description": "<p>content of the comment.</p>"
279+
}
280+
]
281+
}
282+
},
283+
"success": {
284+
"examples": [
285+
{
286+
"title": "Success-Response:",
287+
"content": "HTTP/1.1 201 Created",
288+
"type": "json"
289+
}
290+
]
291+
},
292+
"error": {
293+
"fields": {
294+
"Error 4xx": [
295+
{
296+
"group": "Error 4xx",
297+
"type": "String",
298+
"optional": false,
299+
"field": "404",
300+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
301+
},
302+
{
303+
"group": "Error 4xx",
304+
"type": "String",
305+
"optional": false,
306+
"field": "400",
307+
"description": "<p>Bad request - missing 'title' or 'content' attribute in body</p>"
308+
}
309+
]
310+
}
311+
},
312+
"version": "0.0.0",
313+
"filename": "src/server.js",
314+
"groupTitle": "Wines"
315+
},
250316
{
251317
"type": "get",
252318
"url": "/wines/:id/comments",

api/doc/api_data.json

+66
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,72 @@
247247
"filename": "src/server.js",
248248
"groupTitle": "Wines"
249249
},
250+
{
251+
"type": "post",
252+
"url": "/wines/:id/comments",
253+
"title": "Comment",
254+
"name": "Comment",
255+
"group": "Wines",
256+
"parameter": {
257+
"fields": {
258+
"Parameter": [
259+
{
260+
"group": "Parameter",
261+
"type": "String",
262+
"optional": false,
263+
"field": "id",
264+
"description": "<p>the id of the wine</p>"
265+
},
266+
{
267+
"group": "Parameter",
268+
"type": "String",
269+
"optional": false,
270+
"field": "title",
271+
"description": "<p>title of the comment.</p>"
272+
},
273+
{
274+
"group": "Parameter",
275+
"type": "String",
276+
"optional": false,
277+
"field": "content",
278+
"description": "<p>content of the comment.</p>"
279+
}
280+
]
281+
}
282+
},
283+
"success": {
284+
"examples": [
285+
{
286+
"title": "Success-Response:",
287+
"content": "HTTP/1.1 201 Created",
288+
"type": "json"
289+
}
290+
]
291+
},
292+
"error": {
293+
"fields": {
294+
"Error 4xx": [
295+
{
296+
"group": "Error 4xx",
297+
"type": "String",
298+
"optional": false,
299+
"field": "404",
300+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
301+
},
302+
{
303+
"group": "Error 4xx",
304+
"type": "String",
305+
"optional": false,
306+
"field": "400",
307+
"description": "<p>Bad request - missing 'title' or 'content' attribute in body</p>"
308+
}
309+
]
310+
}
311+
},
312+
"version": "0.0.0",
313+
"filename": "src/server.js",
314+
"groupTitle": "Wines"
315+
},
250316
{
251317
"type": "get",
252318
"url": "/wines/:id/comments",

api/doc/api_project.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ define({
88
"apidoc": "0.2.0",
99
"generator": {
1010
"name": "apidoc",
11-
"time": "2016-03-13T21:01:05.404Z",
11+
"time": "2016-03-14T13:20:36.863Z",
1212
"url": "http://apidocjs.com",
1313
"version": "0.15.1"
1414
}

api/doc/api_project.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"apidoc": "0.2.0",
99
"generator": {
1010
"name": "apidoc",
11-
"time": "2016-03-13T21:01:05.404Z",
11+
"time": "2016-03-14T13:20:36.863Z",
1212
"url": "http://apidocjs.com",
1313
"version": "0.15.1"
1414
}

api/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"description": "React Workshop API",
44
"version": "0.0.0",
55
"dependencies": {
6-
"express": "4.13.4"
6+
"express": "4.13.4",
7+
"body-parser": "1.15.0"
78
},
89
"devDependencies": {
910
"apidoc": "0.15.1"

api/src/server.js

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var express = require('express');
2-
var app = express();
2+
var bodyParser = require('body-parser');
33

44
var BordeauxWines = require('./data/bordeaux-wines.json');
55
var BurgundyWines = require('./data/burgundy-wines.json');
@@ -35,9 +35,11 @@ var Comments = {
3535
}]
3636
};
3737

38+
// Create Server
39+
var app = express();
40+
app.use(bodyParser.json());
3841
// Serve API documentation
3942
app.use(express.static('doc'));
40-
4143
// Configure CORS
4244
app.use(function(req, res, next) {
4345
res.header("Access-Control-Allow-Origin", "*");
@@ -215,6 +217,41 @@ app.get('/api/wines/:id/comments', function (req, res) {
215217
}
216218
});
217219

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+
218255
/**
219256
* @api {get} /regions All
220257
* @apiName All

0 commit comments

Comments
 (0)