swa07016

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

......@@ -25,8 +25,28 @@ const MealCard = (props) => {
}).then(response => response.json())
.then(result => {
if(result.message === 'valid token') {
return fetch('/api/pick', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'authorization': user
},
body: JSON.stringify({
"cardid":props.id
})
}).then(response => response.json())
.then(result => {
if(result.message === 'insertion success') {
alert('pick success');
} else if(result.message === 'card exist') {
alert('이미 mypick에 존재합니다.');
} else {
alert('error');
}
});
setIsLogin(true);
// pick 로직 수행
} else {
alert('로그인이 필요합니다.');
window.location.href = "/mypick";
......@@ -34,6 +54,7 @@ const MealCard = (props) => {
});
}
const pickHandler = (e) => {
e.preventDefault();
authApi();
......
......@@ -89,11 +89,14 @@ const LandingPage = (props) => {
return ;
}
else {
console.log(filteredDatas);
setIsRandom(1);
setTimeout(()=>{
let x = getRandomInt(0, filteredDatas.length);
let y = getRandomInt(0, filteredDatas.length);
let y = -1;
while(1) {
y = getRandomInt(0, filteredDatas.length);
if(x!=y) break;
}
setRandomCards([filteredDatas[x], filteredDatas[y]]);
setIsRandom(2);
}, 2500);
......@@ -123,7 +126,7 @@ const LandingPage = (props) => {
'boxShadow': '0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)'
}}
>
<Row xs="1" sm="2" md="2">
<Row xs="1" sm="1" md="2">
<Col style={{
}}>
......@@ -240,7 +243,7 @@ const LandingPage = (props) => {
</Row>
</Container>
</Container>
)
......
......@@ -3,9 +3,6 @@ import NavBar from '../components/NavBar';
import LoginLink from '../components/LoginLink';
import UserCards from '../components/UserCards';
import { Container } from 'reactstrap';
// auth로 로그인한 사용자일 때와 아닐때 판단해서 화면을 다르게
// 렌더링
// useEffect로 초기 인증
const MypickPage = () => {
const [isLogin, setIsLogin] = useState(false);
......
......@@ -85,8 +85,6 @@ app.post("/api/signin", (req, res) => {
connection.query(sql_usercheck, (err, rows, fields) => {
if(rows.length === 0) {
flag = false;
// console.log(flag);
return res.send({
code: 400,
message: "user does not exist",
......@@ -148,9 +146,8 @@ app.get('/api/auth', (req, res) => {
const user = jwt_decode(req.headers.authorization);
console.log(user.name);
try {
// ?? ??? ??? ??(req.headers.authorization)? ???? ???? ?? ??
req.decoded = jwt.verify(req.headers.authorization, jwt_secret_key.value);
return res.status(200).json({
code: 200,
......@@ -159,9 +156,9 @@ app.get('/api/auth', (req, res) => {
});
}
// ?? ??
catch (error) {
// ????? ??? ??
if (error.name === 'TokenExpiredError') {
return res.status(419).json({
code: 419,
......@@ -169,7 +166,7 @@ app.get('/api/auth', (req, res) => {
});
}
// ??? ???? ???? ?? ??
return res.status(401).json({
code: 401,
message: 'invalid token'
......@@ -178,4 +175,44 @@ app.get('/api/auth', (req, res) => {
});
app.post('/api/pick', (req, res) => {
const user = jwt_decode(req.headers.authorization);
const username = user.name;
const cardid = req.body.cardid;
connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> {
console.log(rows[0]);
if(rows.length === 0) {
// ??
} else {
// ???? ??
let flag = true;
let user_picks = rows[0].pick.split(',');
user_picks.pop();
for(let i=0; i<user_picks.length; i++) {
user_picks[i] = parseInt(user_picks[i]);
if(user_picks[i] == cardid) {
flag = false;
}
}
if(flag) {
const newPick = rows[0].pick + cardid.toString() + ',';
connection.query(`UPDATE USER SET pick='${newPick}' WHERE NAME='${username}';`, (err, rows, fields) => {
return res.status(200).json({
code: 200,
message: 'insertion success',
});
})
} else {
return res.status(401).json({
code: 401,
message: 'card exist'
});
}
}
})
});
app.listen(port, () => console.log(`Listening on port ${port}`));
\ No newline at end of file
......