swa07016

'/api/mypicks' 유저 picks get api 구현

......@@ -26,7 +26,6 @@ const MealCard = (props) => {
.then(result => {
if(result.message === 'valid token') {
return fetch('/api/pick', {
method: 'POST',
headers: {
......@@ -39,9 +38,9 @@ const MealCard = (props) => {
}).then(response => response.json())
.then(result => {
if(result.message === 'insertion success') {
alert('pick success');
alert('MyPick에 담겼습니다.');
} else if(result.message === 'card exist') {
alert('이미 mypick에 존재합니다.');
alert('이미 MyPick에 존재합니다.');
} else {
alert('error');
}
......
......@@ -5,81 +5,35 @@ import axios from 'axios';
import PickedCard from '../components/PickedCard';
const UserCards = (props) => {
const [datas, setDatas] = useState([]);
const [username, setUsername] = useState('User');
const [picks, setPicks] = useState([{
"id": "1",
"name": "#신슨즈(#Shinsons)",
"address": "경기도 용인시 기흥구 서그내로15번길 34 (서천동)",
"latitude": "37.2464876",
"longitude": "127.0768072",
"type": "호프",
"menu": "칵테일, 술",
"img": "/images/1_img.jpg",
"img_source": "https://www.picuki.com/profile/shinsons"
},
{
"id": "2",
"name": "감쟈",
"address": "경기도 용인시 기흥구 서그내로15번길 29, 102호 (서천동)",
"latitude": "37.2464608",
"longitude": "127.0764465",
"type": "술집",
"menu": "안주, 술",
"img": "/images/2_img.jpg",
"img_source": "https://www.facebook.com/gamjua/posts/1408798555882739/"
},
{
"id": "3",
"name": "깜냥",
"address": "경기도 용인시 기흥구 서그내로15번길 29 (서천동,1층)",
"latitude": "37.2464608",
"longitude": "127.0764465",
"type": "술집",
"menu": "안주, 술",
"img": "/images/3_img.jpg",
"img_source": "https://www.facebook.com/ggamnyang316/"
},
{
"id": "4",
"name": "꼬꼬리아통닭",
"address": "경기도 용인시 기흥구 서그내로15번길 39 (서천동)",
"latitude": "37.2465772",
"longitude": "127.0775286",
"type": "호프",
"menu": "치킨, 술",
"img": "/images/4_img.jpg",
"img_source": "https://bigsta.net/tag/%EA%BC%AC%EA%BC%AC%EB%A6%AC%EC%95%84/"
},
{
"id": "5",
"name": "도스마스수원경희대점",
"address": "경기도 용인시 기흥구 서그내로15번길 33 (서천동, 서윤빌딩1층)",
"latitude": "37.2467668",
"longitude": "127.0768863",
"type": "기타",
"menu": "부리또, 타코",
"img": "/images/5_img.jpg",
"img_source": "https://blog.naver.com/alttium/221443978130"
}]);
const [picks, setPicks] = useState([]);
const authApi = () => {
const user = JSON.parse(localStorage.getItem('user'));
return fetch('/api/mypicks', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'authorization': user
}
}).then(response => response.json())
.then( result => {
setPicks(result.datas);
console.log(result.datas);
}
);
}
useEffect(() => {
const fetchData = async () => {
const result = await axios(
'http://localhost:5000/api/datas',
// localhost로 바꾸기
);
setDatas(result.data);
};
fetchData();
setUsername(props.username);
// setUsername(cookie.load('username'));
}, [username]);
setUsername(props.username);
authApi();
}, [username]);
const LogoutHandler = (e) => {
e.preventDefault();
localStorage.removeItem('user');
// cookie.remove('username');
props.isLogin(false);
return ;
}
......
......@@ -27,7 +27,7 @@ const LandingPage = (props) => {
useEffect(() => {
const fetchData = async () => {
const result = await axios(
'http://localhost:5000/api/datas',
'/api/datas',
// localhost로 바꾸기
);
setDatas(result.data);
......
......@@ -27,7 +27,7 @@ const MenuPage = (props) => {
useEffect(() => {
const fetchData = async () => {
const result = await axios(
'http://localhost:5000/api/datas',
'/api/datas',
// localhost로 바꾸기
);
setDatas(result.data);
......
......@@ -46,7 +46,6 @@ app.post("/api/signup", (req, res) => {
let sql_usercheck = `SELECT * FROM USER WHERE name='${req.body.username}';`;
connection.query(sql_usercheck, (err, rows, fields) => {
console.log(rows);
if(rows.length!==0) {
return res.json({
code: 400,
......@@ -174,6 +173,26 @@ app.get('/api/auth', (req, res) => {
}
});
app.get("/api/mypicks", (req, res) => {
let result = [];
const user = jwt_decode(req.headers.authorization);
const username = user.name;
let temp = iconv.decode(dataBuffer, "EUC-KR");
connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds) => {
let user_picks = rows[0].pick.split(',');
temp = JSON.parse(temp);
user_picks.pop();
for(let i=0; i<user_picks.length; i++) {
user_picks[i] = parseInt(user_picks[i]);
}
for(let i=0; i<user_picks.length; i++) {
result.push(temp[user_picks[i]-1]);
}
res.send({'datas':result});
})
});
app.post('/api/pick', (req, res) => {
......@@ -182,7 +201,6 @@ app.post('/api/pick', (req, res) => {
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 {
......