Skip to content

Commit

Permalink
refactoring and routing
Browse files Browse the repository at this point in the history
  • Loading branch information
jutt committed Jun 4, 2020
1 parent 74dc9b7 commit deffc3e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
27 changes: 13 additions & 14 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ app.get('/pokemon/image/:id', async(req, res)=>{
const images = mongoose.model('images');
const singlePokeImage = await images.find({idNumber: req.params.id}).exec();
if(singlePokeImage.length > 0){
console.log('trying to send')
return res.json(singlePokeImage[0]);
}
else{
Expand Down Expand Up @@ -173,19 +172,8 @@ app.get('/friends/all', async(req, res)=>{
res.status(500).json(null);
}
});
app.get('/friends/:name', async(req, res)=>{
const friends = mongoose.model('friends');
const singleFriend = await friends.find({name: req.params.name}).exec();
if(singleFriend.length > 0){
res.json(singleFriend[0]);
}
else{
return res.status(500).json(null);
}
})

app.post('/friends/addNew', async(req, res)=>{
console.log(req.body);
app.post('/friends/create', async(req, res)=>{
const friends = mongoose.model('friends');

//find a friend by a name, if not found upsert
Expand Down Expand Up @@ -219,14 +207,25 @@ app.post('/friends/update', async(req,res)=>{
app.post('/friends/delete', async(req,res)=>{
const friends = mongoose.model('friends');
const deleteFriend = await friends.deleteOne({name: req.body.name});
console.log(deleteFriend);
if(deleteFriend.ok === 1){
res.status(204).send('success');
}
else{
res.status(500).send('failure');
}
});
app.get('/friends/single/:name', async(req, res)=>{
const friends = mongoose.model('friends');
const singleFriend = await friends.find({name: req.params.name}).exec();
if(singleFriend.length > 0){
res.json(singleFriend[0]);
}
else{
return res.status(500).json(null);
}

})


app.get('*', middleware.checkToken, handlers.index);

Expand Down
6 changes: 3 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class App extends React.Component
<Pokemon />
</div>
</Route>

<Route path="/friends/add">
<div className="row justify-content-center">
<FriendsAdd />
Expand All @@ -52,12 +53,11 @@ class App extends React.Component
<FriendsEdit />
</div>
</Route>

<Route path="/friends">
<FriendsTable />
</Route>
</Route>



<Route path="/">
<PokemonTable />
Expand Down
5 changes: 3 additions & 2 deletions src/components/FriendsAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ class FriendsAdd extends React.Component{
}
handleSubmit(event){
$.ajax({
url: '/friends/addNew',
url: '/friends/create',
method: 'POST',
data:{
name: this.state.friend.name,
status: this.state.friend.status,
daysNextStatus: this.state.friend.daysNextStatus
},
success: ()=>{
window.location.reload(false);

window.location.href=`/friends/edit/${this.state.friend.name}`;
},
error: (err)=>{
console.log(err)
Expand Down
3 changes: 1 addition & 2 deletions src/components/FriendsEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class FriendsEdit extends React.Component{
name: this.state.friend.name
},
success: ()=>{
console.log('success');
window.location.href="/friends/";
},
error: (err)=>{
Expand Down Expand Up @@ -56,7 +55,7 @@ class FriendsEdit extends React.Component{
}
async componentDidMount(){
const name = this.props.match.params.name;
var friend = await fetch(`/friends/${name}`).then(res => res.json());
var friend = await fetch(`/friends/single/${name}`).then(res => res.json());
friend.oldName = friend.name;
this.setState({
friend: friend
Expand Down

0 comments on commit deffc3e

Please sign in to comment.