swa07016

'/api/pick' 카드 pick api 구현

...@@ -25,15 +25,36 @@ const MealCard = (props) => { ...@@ -25,15 +25,36 @@ const MealCard = (props) => {
25 }).then(response => response.json()) 25 }).then(response => response.json())
26 .then(result => { 26 .then(result => {
27 if(result.message === 'valid token') { 27 if(result.message === 'valid token') {
28 +
29 +
30 + return fetch('/api/pick', {
31 + method: 'POST',
32 + headers: {
33 + 'Content-Type': 'application/json',
34 + 'authorization': user
35 + },
36 + body: JSON.stringify({
37 + "cardid":props.id
38 + })
39 + }).then(response => response.json())
40 + .then(result => {
41 + if(result.message === 'insertion success') {
42 + alert('pick success');
43 + } else if(result.message === 'card exist') {
44 + alert('이미 mypick에 존재합니다.');
45 + } else {
46 + alert('error');
47 + }
48 + });
28 setIsLogin(true); 49 setIsLogin(true);
29 - // pick 로직 수행
30 } else { 50 } else {
31 alert('로그인이 필요합니다.'); 51 alert('로그인이 필요합니다.');
32 window.location.href = "/mypick"; 52 window.location.href = "/mypick";
33 } 53 }
34 }); 54 });
35 } 55 }
36 - 56 +
57 +
37 const pickHandler = (e) => { 58 const pickHandler = (e) => {
38 e.preventDefault(); 59 e.preventDefault();
39 authApi(); 60 authApi();
......
...@@ -89,11 +89,14 @@ const LandingPage = (props) => { ...@@ -89,11 +89,14 @@ const LandingPage = (props) => {
89 return ; 89 return ;
90 } 90 }
91 else { 91 else {
92 - console.log(filteredDatas);
93 setIsRandom(1); 92 setIsRandom(1);
94 setTimeout(()=>{ 93 setTimeout(()=>{
95 let x = getRandomInt(0, filteredDatas.length); 94 let x = getRandomInt(0, filteredDatas.length);
96 - let y = getRandomInt(0, filteredDatas.length); 95 + let y = -1;
96 + while(1) {
97 + y = getRandomInt(0, filteredDatas.length);
98 + if(x!=y) break;
99 + }
97 setRandomCards([filteredDatas[x], filteredDatas[y]]); 100 setRandomCards([filteredDatas[x], filteredDatas[y]]);
98 setIsRandom(2); 101 setIsRandom(2);
99 }, 2500); 102 }, 2500);
...@@ -123,7 +126,7 @@ const LandingPage = (props) => { ...@@ -123,7 +126,7 @@ const LandingPage = (props) => {
123 'boxShadow': '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)' 126 'boxShadow': '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)'
124 }} 127 }}
125 > 128 >
126 - <Row xs="1" sm="2" md="2"> 129 + <Row xs="1" sm="1" md="2">
127 <Col style={{ 130 <Col style={{
128 131
129 }}> 132 }}>
...@@ -240,7 +243,7 @@ const LandingPage = (props) => { ...@@ -240,7 +243,7 @@ const LandingPage = (props) => {
240 243
241 244
242 </Row> 245 </Row>
243 -</Container> 246 + </Container>
244 ) 247 )
245 248
246 249
......
...@@ -3,9 +3,6 @@ import NavBar from '../components/NavBar'; ...@@ -3,9 +3,6 @@ import NavBar from '../components/NavBar';
3 import LoginLink from '../components/LoginLink'; 3 import LoginLink from '../components/LoginLink';
4 import UserCards from '../components/UserCards'; 4 import UserCards from '../components/UserCards';
5 import { Container } from 'reactstrap'; 5 import { Container } from 'reactstrap';
6 -// auth로 로그인한 사용자일 때와 아닐때 판단해서 화면을 다르게
7 -// 렌더링
8 -// useEffect로 초기 인증
9 6
10 const MypickPage = () => { 7 const MypickPage = () => {
11 const [isLogin, setIsLogin] = useState(false); 8 const [isLogin, setIsLogin] = useState(false);
......
...@@ -85,8 +85,6 @@ app.post("/api/signin", (req, res) => { ...@@ -85,8 +85,6 @@ app.post("/api/signin", (req, res) => {
85 85
86 connection.query(sql_usercheck, (err, rows, fields) => { 86 connection.query(sql_usercheck, (err, rows, fields) => {
87 if(rows.length === 0) { 87 if(rows.length === 0) {
88 - flag = false;
89 - // console.log(flag);
90 return res.send({ 88 return res.send({
91 code: 400, 89 code: 400,
92 message: "user does not exist", 90 message: "user does not exist",
...@@ -148,9 +146,8 @@ app.get('/api/auth', (req, res) => { ...@@ -148,9 +146,8 @@ app.get('/api/auth', (req, res) => {
148 146
149 147
150 const user = jwt_decode(req.headers.authorization); 148 const user = jwt_decode(req.headers.authorization);
151 - console.log(user.name);
152 try { 149 try {
153 - // ?? ??? ??? ??(req.headers.authorization)? ???? ???? ?? ?? 150 +
154 req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value); 151 req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value);
155 return res.status(200).json({ 152 return res.status(200).json({
156 code: 200, 153 code: 200,
...@@ -159,9 +156,9 @@ app.get('/api/auth', (req, res) => { ...@@ -159,9 +156,9 @@ app.get('/api/auth', (req, res) => {
159 }); 156 });
160 } 157 }
161 158
162 - // ?? ?? 159 +
163 catch (error) { 160 catch (error) {
164 - // ????? ??? ?? 161 +
165 if (error.name === 'TokenExpiredError') { 162 if (error.name === 'TokenExpiredError') {
166 return res.status(419).json({ 163 return res.status(419).json({
167 code: 419, 164 code: 419,
...@@ -169,7 +166,7 @@ app.get('/api/auth', (req, res) => { ...@@ -169,7 +166,7 @@ app.get('/api/auth', (req, res) => {
169 }); 166 });
170 } 167 }
171 168
172 - // ??? ???? ???? ?? ?? 169 +
173 return res.status(401).json({ 170 return res.status(401).json({
174 code: 401, 171 code: 401,
175 message: 'invalid token' 172 message: 'invalid token'
...@@ -178,4 +175,44 @@ app.get('/api/auth', (req, res) => { ...@@ -178,4 +175,44 @@ app.get('/api/auth', (req, res) => {
178 }); 175 });
179 176
180 177
178 +app.post('/api/pick', (req, res) => {
179 +
180 + const user = jwt_decode(req.headers.authorization);
181 + const username = user.name;
182 + const cardid = req.body.cardid;
183 +
184 + connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> {
185 + console.log(rows[0]);
186 + if(rows.length === 0) {
187 + // ??
188 + } else {
189 + // ???? ??
190 + let flag = true;
191 + let user_picks = rows[0].pick.split(',');
192 + user_picks.pop();
193 + for(let i=0; i<user_picks.length; i++) {
194 + user_picks[i] = parseInt(user_picks[i]);
195 + if(user_picks[i] == cardid) {
196 + flag = false;
197 + }
198 + }
199 + if(flag) {
200 + const newPick = rows[0].pick + cardid.toString() + ',';
201 + connection.query(`UPDATE USER SET pick='${newPick}' WHERE NAME='${username}';`, (err, rows, fields) => {
202 + return res.status(200).json({
203 + code: 200,
204 + message: 'insertion success',
205 + });
206 + })
207 + } else {
208 + return res.status(401).json({
209 + code: 401,
210 + message: 'card exist'
211 + });
212 + }
213 +
214 + }
215 + })
216 +});
217 +
181 app.listen(port, () => console.log(`Listening on port ${port}`)); 218 app.listen(port, () => console.log(`Listening on port ${port}`));
...\ No newline at end of file ...\ No newline at end of file
......