-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.js
117 lines (92 loc) · 2.24 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const User = require('./models/user')
const passport = require('passport')
const auth = require('./auth/credentials')
const yelp = require('yelp-fusion')
module.exports = {
getBars:async (req,res,next)=>{
try{
const clientId = auth.yelp.id
const clientSecret = auth.yelp.secret
const result = await yelp.accessToken(clientId, clientSecret)
const token = result.jsonBody.access_token
const client = yelp.client(token);
const rez = await client.search({
limit:10,
categories:'bars',
location: req.body.location
})
const bars = rez.jsonBody.businesses
res.send(bars)
}catch(err){
res.send('no match found')
}
},
getUsers: async (req,res,next)=>{
try{
const users = await User.find({})
res.send(users)
}catch(err){
res.send(err)
}
},
deleteUsers: async (req,res,next)=>{
try{
const users = await User.remove({})
res.send(users)
}catch(err){
res.send(err)
}
},
login :async (req,res,next)=>{
req.session.save()
res.redirect('/login/' + req.user.facebook.token)
},
isAuthenticated: async(req,res,next)=>{
if(req.user){
next()
}else{
console.log('you need authenticate')
}
},
toggle_going: async (req,res,next)=>{
const userid = req.user._id
const barid = req.body.barid
const user = await User.findOne({_id:userid})
console.log(barid)
const havit = user.bars.includes(barid)
console.log(havit)
if(havit){
user.bars = user.bars.filter((bar)=>{
return bar!==barid
})
await user.save()
console.log(user.bars)
res.send(user.bars)
}else{
user.bars.push(barid)
await user.save()
res.send(user.bars)
}
},
getUser: async (req,res,next)=>{
try{
if(req.user){
const user = await User.findOne({_id:req.user._id})
res.send(user)
}else{
res.send(false)
}
}catch(err){
res.send(err)
}
},
logout: async (req,res,next)=>{
try{
req.logout();
console.log(req.user)
res.redirect('/');
}catch(err){
res.send(err)
}
},
}