Skip to content

Commit 5fa605d

Browse files
ianfdksomus
authored andcommitted
Make app's API more restful (#160)
1 parent 6e1fe48 commit 5fa605d

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

client/modules/Post/PostActions.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function addPost(post) {
2626

2727
export function addPostRequest(post) {
2828
return (dispatch) => {
29-
fetch(`${baseURL}/api/addPost`, {
29+
fetch(`${baseURL}/api/posts`, {
3030
method: 'post',
3131
body: JSON.stringify({
3232
post: {
@@ -51,7 +51,7 @@ export function addSelectedPost(post) {
5151

5252
export function getPostRequest(post) {
5353
return (dispatch) => {
54-
return fetch(`${baseURL}/api/getPost?slug=${post}`, {
54+
return fetch(`${baseURL}/api/posts/${post}`, {
5555
method: 'get',
5656
headers: new Headers({
5757
'Content-Type': 'application/json',
@@ -76,18 +76,18 @@ export function addPosts(posts) {
7676

7777
export function fetchPosts() {
7878
return (dispatch) => {
79-
return fetch(`${baseURL}/api/getPosts`).
79+
return fetch(`${baseURL}/api/posts`).
8080
then((response) => response.json()).
8181
then((response) => dispatch(addPosts(response.posts)));
8282
};
8383
}
8484

8585
export function deletePostRequest(post) {
8686
return (dispatch) => {
87-
fetch(`${baseURL}/api/deletePost`, {
88-
method: 'post',
87+
fetch(`${baseURL}/api/posts`, {
88+
method: 'delete',
8989
body: JSON.stringify({
90-
postId: post._id,
90+
id: post._id,
9191
}),
9292
headers: new Headers({
9393
'Content-Type': 'application/json',

client/modules/Post/components/PostListItem/PostListItem.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function PostListItem(props) {
88
return (
99
<div className={styles['single-post']}>
1010
<h3 className={styles['post-title']}>
11-
<Link to={`/post/${props.post.slug}-${props.post.cuid}`} onClick={props.onClick}>
11+
<Link to={`/posts/${props.post.slug}-${props.post.cuid}`} onClick={props.onClick}>
1212
{props.post.title}
1313
</Link>
1414
</h3>

client/routes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default (
3232
}}
3333
/>
3434
<Route
35-
path="/post/:slug"
35+
path="/posts/:slug"
3636
getComponent={(nextState, cb) => {
3737
require.ensure([], require => {
3838
cb(null, require('./modules/Post/pages/PostDetailPage/PostDetailPage').default);

server/controllers/post.controller.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function addPost(req, res) { // eslint-disable-line consistent-return
5151
* @param res
5252
*/
5353
export function getPost(req, res) {
54-
const newSlug = req.query.slug.split('-');
54+
const newSlug = req.params.slug.split('-');
5555
const newCuid = newSlug[newSlug.length - 1];
5656
Post.findOne({ cuid: newCuid }).exec((err, post) => {
5757
if (err) {
@@ -67,7 +67,7 @@ export function getPost(req, res) {
6767
* @param res
6868
*/
6969
export function deletePost(req, res) {
70-
const postId = req.body.postId;
70+
const postId = req.body.id;
7171
Post.findById(postId).exec((err, post) => { // eslint-disable-line consistent-return
7272
if (err) {
7373
return res.status(500).send(err);

server/routes/post.routes.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import * as PostController from '../controllers/post.controller';
33
const router = new Router();
44

55
// Get all Posts
6-
router.route('/getPosts').get(PostController.getPosts);
6+
router.route('/posts').get(PostController.getPosts);
77

88
// Get one post by title
9-
router.route('/getPost').get(PostController.getPost);
9+
router.route('/posts/:slug').get(PostController.getPost);
1010

1111
// Add a new Post
12-
router.route('/addPost').post(PostController.addPost);
12+
router.route('/posts').post(PostController.addPost);
1313

1414
// Delete a Post
15-
router.route('/deletePost').post(PostController.deletePost);
15+
router.route('/posts').delete(PostController.deletePost);
1616

1717
export default router;

server/tests/post.spec.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function dropDB(done) {
2828
});
2929
}
3030

31-
describe('GET /api/getPosts', function () {
31+
describe('GET /api/posts', function () {
3232

3333
beforeEach('connect and add two post entries', function (done) {
3434

@@ -49,7 +49,7 @@ describe('GET /api/getPosts', function () {
4949
it('Should correctly give number of Posts', function (done) {
5050

5151
request(app)
52-
.get('/api/getPosts')
52+
.get('/api/posts')
5353
.set('Accept', 'application/json')
5454
.end(function (err, res) {
5555
Post.find().exec(function (err, posts) {
@@ -60,7 +60,7 @@ describe('GET /api/getPosts', function () {
6060
});
6161
});
6262

63-
describe('GET /api/getPost', function () {
63+
describe('GET /api/posts/:slug', function () {
6464

6565
beforeEach('connect and add one Post entry', function(done){
6666

@@ -80,7 +80,7 @@ describe('GET /api/getPost', function () {
8080
it('Should send correct data when queried against a title', function (done) {
8181

8282
request(app)
83-
.get('/api/getPost?slug=bar-f34gb2bh24b24b2')
83+
.get('/api/posts/bar-f34gb2bh24b24b2')
8484
.set('Accept', 'application/json')
8585
.end(function (err, res) {
8686
Post.findOne({ cuid: 'f34gb2bh24b24b2' }).exec(function (err, post) {
@@ -92,7 +92,7 @@ describe('GET /api/getPost', function () {
9292

9393
});
9494

95-
describe('POST /api/addPost', function () {
95+
describe('POST /api/posts', function () {
9696

9797
beforeEach('connect and add a post', function (done) {
9898

@@ -108,7 +108,7 @@ describe('POST /api/addPost', function () {
108108
it('Should send correctly add a post', function (done) {
109109

110110
request(app)
111-
.post('/api/addPost')
111+
.post('/api/posts')
112112
.send({ post: { name: 'Foo', title: 'bar', content: 'Hello Mern says Foo' } })
113113
.set('Accept', 'application/json')
114114
.end(function (err, res) {
@@ -121,7 +121,7 @@ describe('POST /api/addPost', function () {
121121

122122
});
123123

124-
describe('POST /api/deletePost', function () {
124+
describe('POST /api/posts', function () {
125125
var postId;
126126

127127
beforeEach('connect and add one Post entry', function(done){
@@ -148,8 +148,8 @@ describe('POST /api/deletePost', function () {
148148
});
149149

150150
request(app)
151-
.post('/api/deletePost')
152-
.send({ postId: postId})
151+
.delete('/api/posts')
152+
.send({ id: postId})
153153
.set('Accept', 'application/json')
154154
.end(function () {
155155

0 commit comments

Comments
 (0)