Donghoon Kim

MySQL DB 연동

File mode changed
var map;
var pos;
var infowindow;
var service;
var result_food = [];
var result_entertainment = [];
var result_room = [];
export { pos as posiiton };
export { result_food as foodList };
export { result_entertainment as entList };
export { result_room as roomList };
var result_entertainment = []
var result_food = []
var result_room = []
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 17
});
infowindow = new google.maps.InfoWindow();
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
......@@ -27,22 +15,22 @@ function initMap() {
lng: position.coords.longitude
};
var map = new google.maps.Map(document.getElementById('map'), {
center: pos,
zoom: 17
});
infowindow = new google.maps.InfoWindow();
map.setCenter(pos);
service = new google.maps.places.PlacesService(map);
service.nearbySearch({
location: pos,
radius: 500,
type: ['bakery']
}, callback_foods);
searchPlace('bar','food');
searchPlace('cafe','food');
searchPlace('meal_delivery','food');
searchPlace('meal_takeaway','food');
searchPlace('restaurant','food');
searchPlace('bakery','food');
searchPlace('department_store','entertainment');
searchPlace('movie_theater','entertainment');
......@@ -52,6 +40,17 @@ function initMap() {
searchPlace('zoo','entertainment');
searchPlace('lodging','room');
// put data to db
for (var i = 0; i < results_food.length; i++) {
putDataToDB(results_food[i], 'food')
}
for (var i = 0; i < results_entertainment.length; i++) {
putDataToDB(results_entertainment[i], 'entertainment')
}
for (var i = 0; i < results_room.length; i++) {
putDataToDB(results_room[i], 'room')
}
});
}
}
......@@ -62,21 +61,21 @@ function searchPlace(str, placeType) {
service.nearbySearch({
location: pos,
radius: 500,
type: ['meal_takeaway']
type: [str]
}, callback_foods);
break;
case 'entertainment':
service.nearbySearch({
location: pos,
radius: 500,
type: [str.toString()]
type: [str]
}, callback_entertainment);
break;
case 'room':
service.nearbySearch({
location: pos,
radius: 500,
type: ['lodging']
type: [str]
}, callback_rooms);
break;
default:
......@@ -112,8 +111,6 @@ function callback_rooms(results, status) {
}
}
function createMarker_foods(place) {
var marker = new google.maps.Marker({
map: map,
......@@ -155,3 +152,78 @@ function createMarker_rooms(place) {
infowindow.open(map, this);
});
}
function putDataToDB(result, category1) {
const id = result;
const place_id =result;
const name = result;
const address = result;
let category_big = category1
const category_small = result;
const image = result;
const rating = result;
const lng = result;
const lat =result;
const QueryCheck = () => {
if (!id || !place_id || !name || !address || !category_big || !category_small || !image || !rating || !lng || !lat) {
return Promise.reject({
message: 'Query Error'
})
}
return Promise.resolve()
}
// 2. SQL Start
const SQLStart = async (pool) => {
try {
let data = await pool.query('INSERT INTO PLACE(ID, PLACE_ID, NAME, ADDRESS, CATEGORY_BIG, CATEGORY_SMALL, IMAGE, RATING, LNG, LAT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);', [id, place_id, name, address, category_big, category_small, image, rating, lng, lat])
return Promise.resolve(pool)
} catch(err) {
return Promise.reject(err)
}
}
// 3. Response
const Response = (rows) => {
return res.status(200).json(rows)
}
// 1. Query Check
const FindQueryCheck = () => {
if (!keyword) {
return Promise.reject({
message: 'Query Error'
})
}
else return pool
}
// 2. SQL Start
const FindSQLStart = (pool) => {
return pool.query(`SELECT * FROM PLACE WHERE ID LIKE '%${id}%')
}
// 3. Response
const FindResponse = (rows) => {
return res.status(200).json(rows)
}
if([] == FindQueryCheck()
.then(FindSQLStart)
.then(FindResponse)
.catch(err => {
if (err) {
return res.status(500).json(err.message || err)
}
}))
{
QueryCheck()
.then(SQLStart)
.then(Response)
.catch(err => {
if (err) {
return res.status(500).json(err.message || err)
}
})
}
}
......
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var router = require('./routes');
var app = express();
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/', router);
var server = app.listen(3000);
......
console.log("Hello World!");
\ No newline at end of file
......@@ -95,7 +95,7 @@
</ul>
<div id="content-wrapper">
<script type="text/javascript" src="js/getPlaceList.js"></script>
<script type="text/javascript" src="/../api/getPlaceList.js"></script>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDg4CkUEEt_96n1ork1nL5t4E_SpdKrgMI&libraries=places&callback=initMap" async defer></script>
<!-- Sticky Footer -->
......
import entList from 'getPlaceList'
//import position from 'getPlaceList'
import foodList from 'getPlaceList'
import roomList from 'getPlaceList'
......@@ -6,8 +6,4 @@ router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/Foods', function(req, res, next) {
res.render('Foods', { title: 'Express' });
});
module.exports = router;
......
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;