Skip to content

Commit

Permalink
REST changes on API
Browse files Browse the repository at this point in the history
  • Loading branch information
FLiotta committed Feb 24, 2020
1 parent 7894e6d commit 44d9224
Show file tree
Hide file tree
Showing 14 changed files with 314 additions and 249 deletions.
10 changes: 5 additions & 5 deletions client/src/actions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ export const signIn = ({username, password}) => {
position: 'bottom-right'
});
dispatch({
type: SIGN_IN,
payload: {
...res.response
}
});
type: SIGN_IN,
payload: {
...res.response
}
});
}
})
.catch(e => console.log(e))
Expand Down
5 changes: 2 additions & 3 deletions client/src/actions/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,9 @@ export const unlikePost = (postId) => {
}

export const deletePost = (data) => {
return (dispatch, getState) => {
const state = getState();
return dispatch => {
const { postId } = data;
API.get(`post/${postId}/delete`)
API.delete(`post/${postId}`)
.then(res => {
cogoToast.warn(`Post deleted`, {
position: 'bottom-right'
Expand Down
13 changes: 4 additions & 9 deletions client/src/actions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ export const CHANGE_IMAGE = 'CHANGE_IMAGE',


export const changeImage = (binary, crop) => {
return (dispatch, getState) => {
const state = getState();
const { username } = state.app.logged;
return dispatch => {
const payload = new FormData();
payload.append('crop', JSON.stringify(crop));
payload.append('newImage', binary);


API.post(`user/${username}/edit/info/profilePicture`,payload, {
API.patch(`user/settings/profilePicture`,payload, {
headers: {
'accept': 'application/json',
'Accept-Language': 'en-US,en;q=0.8',
Expand All @@ -38,12 +36,9 @@ export const changeImage = (binary, crop) => {
}

export const changeDescription = description => {
return (dispatch, getState) => {
const state = getState();
return dispatch => {

const username = state.app.logged.username;

API.post(`user/${username}/edit/info/description`, { description })
API.patch(`user/settings/description`, { description })
.then(res => {
if(res.code == 200)
dispatch(setDescription(res.response.newDescription));
Expand Down
43 changes: 38 additions & 5 deletions client/src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logout } from '../actions/app';

class Api {
constructor() {
this.baseUrl = 'http://localhost:3000';
this.baseUrl = 'http://localhost:3000/api';
}

get(url) {
Expand Down Expand Up @@ -40,14 +40,14 @@ class Api {

post(url, params) {
const state = store.getState();

const config = {
headers: { }
headers: {}
}

if(state.app.logged.token)
if(state.app.logged.token) {
config.headers['authToken'] = state.app.logged.token;

}

return new Promise((res,rej) => {
axios.post(`${this.baseUrl}/${url}`, params, config)
Expand Down Expand Up @@ -100,6 +100,39 @@ class Api {
});
})
}

delete(url, params) {
const state = store.getState();

if(!state.app.logged.token)
return;

const config = {
headers: {
authToken: state.app.logged.token
},
data: params
}

return new Promise((res,rej) => {
axios.delete(`${this.baseUrl}/${url}`, config)
.then(response => res(response.data))
.catch(e => {
const { status, data } = e.response;

switch(status){
case 401:
store.dispatch(logout());
break;
}

cogoToast.error(`${status}: ${data.message}`, {
position: 'bottom-right'
});
rej(e);
});
})
}
}

export default Api;
9 changes: 1 addition & 8 deletions server/middlewares/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,4 @@ const isAuth = (req,res,next) => {
next();
}

const checkOwnsProfile = (req,res,next) => {
if(req.user.username != req.params.username)
return res.status(403).json({ code: 403, message: "Unauthorized request"});

next();
}

module.exports = {isAuth, checkOwnsProfile};
module.exports = {isAuth};
8 changes: 8 additions & 0 deletions server/routes/Api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const apiRouter = require('express').Router();

apiRouter.use('/auth', require('./Auth'));
apiRouter.use('/user', require('./user'));
apiRouter.use('/discover', require('./Discover'));
apiRouter.use('/post', require('./Post'));

module.exports = apiRouter;
6 changes: 3 additions & 3 deletions server/routes/Discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ router.get('/users', (req,res) => {
.exec((err, count) => {
if(err)
return res.status(500).send('There were an error counting the users');
const random = Math.floor(Math.random() * (count - 20))
const random = Math.floor(Math.random() * (count - 10))
User.find({})
.limit(20)
.limit(10)
.skip(random)
.exec((err, result) => {
if(err)
Expand All @@ -36,7 +36,7 @@ router.get('/posts', (req,res) => {
const random = Math.floor(Math.random() * (count - 10));
Post.find({})
.limit(10)
//.skip(random)
.skip(random)
.populate('author')
.exec((err, result) => {
if(err)
Expand Down
50 changes: 25 additions & 25 deletions server/routes/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@ const router = express.Router();
const Post = require('../models/Post');
const {isAuth} = require('../middlewares/auth');

router.get('/:id', (req,res) => {
const { id } = req.params;

Post.findById(id)
.then(post =>
post
? res.status(200).json({code: 200, response: post})
: res.status(404).json({code: 404, message: 'Post not found'}))
.catch(e => res.send(500).json({error: 'Unexpected error.'}));
});

router.delete('/:id', isAuth, (req,res) => {
const { id } = req.params;

Post.findOneAndDelete({
_id: id,
$or: [{author: req.user._id}, {profile: req.user.username}]
})
.exec((err, post) => {
if(err)
return res.status(500).json({code: 500, message: 'There was an error deleting the post', error: err})
res.status(200).json({code: 200, message: 'Post deleted', deletedPost: post})
});
})

router.post('/:id/like', isAuth, (req,res) => {
const { id } = req.params;

Expand Down Expand Up @@ -65,29 +90,4 @@ router.post('/:id/unlike', isAuth, (req,res) => {
.catch(e => res.status(500).send("There were an error"));
})

router.get('/:id/delete', isAuth, (req,res) => {
const { id } = req.params;

if(!req.user)
res.status(403).json({code: 403, response: "Unauthorized request"});

Post.findOneAndDelete({_id: id, $or: [{author: req.user._id}, {profile: req.user.username}]})
.exec((err, post) => {
if(err)
return res.status(500).json({code: 500, message: 'There was an error deleting the post', error: err})
res.status(200).json({code: 200, message: 'Post deleted', deletedPost: post})
});
})

router.get('/:id', (req,res) => {
const { id } = req.params;

Post.findById(id)
.then(post =>
post
? res.status(200).json({code: 200, response: post})
: res.status(404).json({code: 404, response: 'Post not found'}))
.catch(e => res.send(500).json({error: 'There were an error.'}));
});

module.exports = router;
Loading

0 comments on commit 44d9224

Please sign in to comment.