swa07016

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

...@@ -26,7 +26,6 @@ const MealCard = (props) => { ...@@ -26,7 +26,6 @@ const MealCard = (props) => {
26 .then(result => { 26 .then(result => {
27 if(result.message === 'valid token') { 27 if(result.message === 'valid token') {
28 28
29 -
30 return fetch('/api/pick', { 29 return fetch('/api/pick', {
31 method: 'POST', 30 method: 'POST',
32 headers: { 31 headers: {
...@@ -39,9 +38,9 @@ const MealCard = (props) => { ...@@ -39,9 +38,9 @@ const MealCard = (props) => {
39 }).then(response => response.json()) 38 }).then(response => response.json())
40 .then(result => { 39 .then(result => {
41 if(result.message === 'insertion success') { 40 if(result.message === 'insertion success') {
42 - alert('pick success'); 41 + alert('MyPick에 담겼습니다.');
43 } else if(result.message === 'card exist') { 42 } else if(result.message === 'card exist') {
44 - alert('이미 mypick에 존재합니다.'); 43 + alert('이미 MyPick에 존재합니다.');
45 } else { 44 } else {
46 alert('error'); 45 alert('error');
47 } 46 }
......
...@@ -5,81 +5,35 @@ import axios from 'axios'; ...@@ -5,81 +5,35 @@ import axios from 'axios';
5 import PickedCard from '../components/PickedCard'; 5 import PickedCard from '../components/PickedCard';
6 6
7 const UserCards = (props) => { 7 const UserCards = (props) => {
8 - const [datas, setDatas] = useState([]);
9 const [username, setUsername] = useState('User'); 8 const [username, setUsername] = useState('User');
10 - const [picks, setPicks] = useState([{ 9 + const [picks, setPicks] = useState([]);
11 - "id": "1",
12 - "name": "#신슨즈(#Shinsons)",
13 - "address": "경기도 용인시 기흥구 서그내로15번길 34 (서천동)",
14 - "latitude": "37.2464876",
15 - "longitude": "127.0768072",
16 - "type": "호프",
17 - "menu": "칵테일, 술",
18 - "img": "/images/1_img.jpg",
19 - "img_source": "https://www.picuki.com/profile/shinsons"
20 - },
21 - {
22 - "id": "2",
23 - "name": "감쟈",
24 - "address": "경기도 용인시 기흥구 서그내로15번길 29, 102호 (서천동)",
25 - "latitude": "37.2464608",
26 - "longitude": "127.0764465",
27 - "type": "술집",
28 - "menu": "안주, 술",
29 - "img": "/images/2_img.jpg",
30 - "img_source": "https://www.facebook.com/gamjua/posts/1408798555882739/"
31 - },
32 - {
33 - "id": "3",
34 - "name": "깜냥",
35 - "address": "경기도 용인시 기흥구 서그내로15번길 29 (서천동,1층)",
36 - "latitude": "37.2464608",
37 - "longitude": "127.0764465",
38 - "type": "술집",
39 - "menu": "안주, 술",
40 - "img": "/images/3_img.jpg",
41 - "img_source": "https://www.facebook.com/ggamnyang316/"
42 - },
43 - {
44 - "id": "4",
45 - "name": "꼬꼬리아통닭",
46 - "address": "경기도 용인시 기흥구 서그내로15번길 39 (서천동)",
47 - "latitude": "37.2465772",
48 - "longitude": "127.0775286",
49 - "type": "호프",
50 - "menu": "치킨, 술",
51 - "img": "/images/4_img.jpg",
52 - "img_source": "https://bigsta.net/tag/%EA%BC%AC%EA%BC%AC%EB%A6%AC%EC%95%84/"
53 - },
54 - {
55 - "id": "5",
56 - "name": "도스마스수원경희대점",
57 - "address": "경기도 용인시 기흥구 서그내로15번길 33 (서천동, 서윤빌딩1층)",
58 - "latitude": "37.2467668",
59 - "longitude": "127.0768863",
60 - "type": "기타",
61 - "menu": "부리또, 타코",
62 - "img": "/images/5_img.jpg",
63 - "img_source": "https://blog.naver.com/alttium/221443978130"
64 - }]);
65 10
11 + const authApi = () => {
12 + const user = JSON.parse(localStorage.getItem('user'));
13 + return fetch('/api/mypicks', {
14 + method: 'GET',
15 + headers: {
16 + 'Content-Type': 'application/json',
17 + 'authorization': user
18 + }
19 + }).then(response => response.json())
20 + .then( result => {
21 + setPicks(result.datas);
22 + console.log(result.datas);
23 + }
24 + );
25 + }
26 +
66 useEffect(() => { 27 useEffect(() => {
67 - const fetchData = async () => { 28 + setUsername(props.username);
68 - const result = await axios( 29 + authApi();
69 - 'http://localhost:5000/api/datas', 30 + }, [username]);
70 - // localhost로 바꾸기 31 +
71 - ); 32 +
72 - setDatas(result.data);
73 - };
74 - fetchData();
75 - setUsername(props.username);
76 - // setUsername(cookie.load('username'));
77 - }, [username]);
78 33
79 const LogoutHandler = (e) => { 34 const LogoutHandler = (e) => {
80 e.preventDefault(); 35 e.preventDefault();
81 localStorage.removeItem('user'); 36 localStorage.removeItem('user');
82 - // cookie.remove('username');
83 props.isLogin(false); 37 props.isLogin(false);
84 return ; 38 return ;
85 } 39 }
......
...@@ -27,7 +27,7 @@ const LandingPage = (props) => { ...@@ -27,7 +27,7 @@ const LandingPage = (props) => {
27 useEffect(() => { 27 useEffect(() => {
28 const fetchData = async () => { 28 const fetchData = async () => {
29 const result = await axios( 29 const result = await axios(
30 - 'http://localhost:5000/api/datas', 30 + '/api/datas',
31 // localhost로 바꾸기 31 // localhost로 바꾸기
32 ); 32 );
33 setDatas(result.data); 33 setDatas(result.data);
......
...@@ -27,7 +27,7 @@ const MenuPage = (props) => { ...@@ -27,7 +27,7 @@ const MenuPage = (props) => {
27 useEffect(() => { 27 useEffect(() => {
28 const fetchData = async () => { 28 const fetchData = async () => {
29 const result = await axios( 29 const result = await axios(
30 - 'http://localhost:5000/api/datas', 30 + '/api/datas',
31 // localhost로 바꾸기 31 // localhost로 바꾸기
32 ); 32 );
33 setDatas(result.data); 33 setDatas(result.data);
......
...@@ -46,7 +46,6 @@ app.post("/api/signup", (req, res) => { ...@@ -46,7 +46,6 @@ app.post("/api/signup", (req, res) => {
46 46
47 let sql_usercheck = `SELECT * FROM USER WHERE name='${req.body.username}';`; 47 let sql_usercheck = `SELECT * FROM USER WHERE name='${req.body.username}';`;
48 connection.query(sql_usercheck, (err, rows, fields) => { 48 connection.query(sql_usercheck, (err, rows, fields) => {
49 - console.log(rows);
50 if(rows.length!==0) { 49 if(rows.length!==0) {
51 return res.json({ 50 return res.json({
52 code: 400, 51 code: 400,
...@@ -174,6 +173,26 @@ app.get('/api/auth', (req, res) => { ...@@ -174,6 +173,26 @@ app.get('/api/auth', (req, res) => {
174 } 173 }
175 }); 174 });
176 175
176 +app.get("/api/mypicks", (req, res) => {
177 + let result = [];
178 + const user = jwt_decode(req.headers.authorization);
179 + const username = user.name;
180 + let temp = iconv.decode(dataBuffer, "EUC-KR");
181 +
182 + connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds) => {
183 + let user_picks = rows[0].pick.split(',');
184 + temp = JSON.parse(temp);
185 + user_picks.pop();
186 + for(let i=0; i<user_picks.length; i++) {
187 + user_picks[i] = parseInt(user_picks[i]);
188 + }
189 + for(let i=0; i<user_picks.length; i++) {
190 + result.push(temp[user_picks[i]-1]);
191 + }
192 +
193 + res.send({'datas':result});
194 + })
195 +});
177 196
178 app.post('/api/pick', (req, res) => { 197 app.post('/api/pick', (req, res) => {
179 198
...@@ -182,7 +201,6 @@ app.post('/api/pick', (req, res) => { ...@@ -182,7 +201,6 @@ app.post('/api/pick', (req, res) => {
182 const cardid = req.body.cardid; 201 const cardid = req.body.cardid;
183 202
184 connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> { 203 connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> {
185 - console.log(rows[0]);
186 if(rows.length === 0) { 204 if(rows.length === 0) {
187 // ?? 205 // ??
188 } else { 206 } else {
......