공태현

Implement API Updating user squart count

Showing 1 changed file with 38 additions and 5 deletions
...@@ -53,9 +53,16 @@ app.get('/main.js', (req,res) => { ...@@ -53,9 +53,16 @@ app.get('/main.js', (req,res) => {
53 }) 53 })
54 54
55 app.get('/squart', (req,res) => { 55 app.get('/squart', (req,res) => {
56 - 56 + if (req.session.user)
57 + {
57 app.set('views', __dirname + '/views/squartPage') 58 app.set('views', __dirname + '/views/squartPage')
58 res.render('squart.html') 59 res.render('squart.html')
60 + }
61 + else
62 + { // 로그인 안되어 있으면, 스쿼트 페이지 진입 불가.
63 + app.set('views', __dirname + '/views/mainPage')
64 + res.render('main.html')
65 + }
59 }) 66 })
60 67
61 app.listen(port, () => { 68 app.listen(port, () => {
...@@ -72,12 +79,19 @@ app.listen(port, () => { ...@@ -72,12 +79,19 @@ app.listen(port, () => {
72 // 등록 . 79 // 등록 .
73 app.use(express.json()) 80 app.use(express.json())
74 app.post('/api/users/register', (req,res) => { 81 app.post('/api/users/register', (req,res) => {
82 + console.log(req.body)
75 const new_user = new User(req.body); 83 const new_user = new User(req.body);
76 new_user.save((err, userInfo) => { 84 new_user.save((err, userInfo) => {
77 - if (err) return res.json({ successs : false, err}) 85 + if (err)
78 - return res.status(200).json({ 86 + {
79 - success : true 87 + var result = res.json({success : false, err})
80 - }) 88 + return result
89 + }
90 + else
91 + {
92 + var result = res.status(200).json({success : true})
93 + return result
94 + }
81 }) 95 })
82 }) 96 })
83 97
...@@ -145,8 +159,27 @@ app.get('/api/users/logout', (req,res) => { ...@@ -145,8 +159,27 @@ app.get('/api/users/logout', (req,res) => {
145 }) 159 })
146 160
147 161
162 +app.post('/api/users/countupdate', (req,res) => {
163 + var userName = req.body.name
164 + var userCount = req.body.count
165 + User.findOne({name : userName}, (err,userInfo) => {
166 +
167 + if (err) res.json({success : false, err})
168 +
169 + userInfo.today_squart = Number(userInfo.today_squart) + Number(userCount)
170 + userInfo.total_squart = Number(userInfo.total_squart) + Number(userCount)
171 + userInfo.save()
148 172
149 173
174 + return res.json({
175 + success : true,
176 + today_squart : userInfo.today_squart,
177 + total_squart : userInfo.total_squart
178 + })
179 +
180 + })
181 +})
182 +
150 183
151 // 세션 저장 확인 184 // 세션 저장 확인
152 app.get('/api/session', (req,res) => { 185 app.get('/api/session', (req,res) => {
......