This commit is contained in:
p11037 2023-11-18 23:59:43 +07:00
parent 05c8212b75
commit 0d364fa4cc
5 changed files with 116 additions and 29 deletions

36
models/favorites.js Normal file
View file

@ -0,0 +1,36 @@
const { default: mongoose } = require("mongoose")
const favSchema = new mongoose.Schema({
user_id : {type:String,required: true},
place_name : String,
location: {
type: {
type: String,
enum: ['Point'],
default: 'Point'
},
coordinates: {
type: [Number],
required: true
}
},
wheelchair_access : String,
highway_type : String,
updated_at : {type:Date, default:Date.now}
})
module.exports = mongoose.model('favorites',favSchema)
//ตัวอย่างข้อมูล
// {
// "user_id": "651310bdb59abf1a593e2368",
// "place_name": "Nonthaburi",
// "location": {
// "type": "Point",
// "coordinates": [100.5212, 13.8593] // ลำดับตำแหน่งแรกเป็นลองจิจูดและตำแหน่งที่สองเป็นละติจูด
// },
// "wheelchair_access": "accessible",
// "highway_type": "expressway"
// }

66
package-lock.json generated
View file

@ -18,29 +18,33 @@
"express-async-handler": "^1.2.0", "express-async-handler": "^1.2.0",
"express-session": "^1.17.3", "express-session": "^1.17.3",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"mongoose": "^7.5.1", "mongoose": "^7.6.5",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"validator": "^13.11.0" "validator": "^13.11.0",
"y": "^0.3.2"
} }
}, },
"node_modules/@mongodb-js/saslprep": { "node_modules/@mongodb-js/saslprep": {
"version": "1.1.0", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.0.tgz", "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.1.tgz",
"integrity": "sha512-Xfijy7HvfzzqiOAhAepF4SGN5e9leLkMvg/OPOF97XemjfVCYN/oWa75wnkc6mltMSTwY+XlbhWgUOJmkFspSw==", "integrity": "sha512-t7c5K033joZZMspnHg/gWPE4kandgc2OxE74aYOtGKfgB9VPuVJPix0H6fhmm2erj5PBJ21mqcx34lpIGtUCsQ==",
"optional": true, "optional": true,
"dependencies": { "dependencies": {
"sparse-bitfield": "^3.0.3" "sparse-bitfield": "^3.0.3"
} }
}, },
"node_modules/@types/node": { "node_modules/@types/node": {
"version": "20.6.0", "version": "20.9.1",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.0.tgz", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.9.1.tgz",
"integrity": "sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==" "integrity": "sha512-HhmzZh5LSJNS5O8jQKpJ/3ZcrrlG6L70hpGqMIAoM9YVD0YBRNWYsfwcXq8VnSjlNpCpgLzMXdiPo+dxcvSmiA==",
"dependencies": {
"undici-types": "~5.26.4"
}
}, },
"node_modules/@types/webidl-conversions": { "node_modules/@types/webidl-conversions": {
"version": "7.0.0", "version": "7.0.3",
"resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz", "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz",
"integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==" "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA=="
}, },
"node_modules/@types/whatwg-url": { "node_modules/@types/whatwg-url": {
"version": "8.2.2", "version": "8.2.2",
@ -147,9 +151,9 @@
} }
}, },
"node_modules/bson": { "node_modules/bson": {
"version": "5.4.0", "version": "5.5.1",
"resolved": "https://registry.npmjs.org/bson/-/bson-5.4.0.tgz", "resolved": "https://registry.npmjs.org/bson/-/bson-5.5.1.tgz",
"integrity": "sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==", "integrity": "sha512-ix0EwukN2EpC0SRWIj/7B5+A6uQMQy6KMREI9qQqvgpkV2frH63T0UDVd1SYedL6dNCmDBYB3QtXi4ISk9YT+g==",
"engines": { "engines": {
"node": ">=14.20.1" "node": ">=14.20.1"
} }
@ -866,11 +870,11 @@
} }
}, },
"node_modules/mongodb": { "node_modules/mongodb": {
"version": "5.8.1", "version": "5.9.0",
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.8.1.tgz", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.9.0.tgz",
"integrity": "sha512-wKyh4kZvm6NrCPH8AxyzXm3JBoEf4Xulo0aUWh3hCgwgYJxyQ1KLST86ZZaSWdj6/kxYUA3+YZuyADCE61CMSg==", "integrity": "sha512-g+GCMHN1CoRUA+wb1Agv0TI4YTSiWr42B5ulkiAfLLHitGK1R+PkSAf3Lr5rPZwi/3F04LiaZEW0Kxro9Fi2TA==",
"dependencies": { "dependencies": {
"bson": "^5.4.0", "bson": "^5.5.0",
"mongodb-connection-string-url": "^2.6.0", "mongodb-connection-string-url": "^2.6.0",
"socks": "^2.7.1" "socks": "^2.7.1"
}, },
@ -915,13 +919,13 @@
} }
}, },
"node_modules/mongoose": { "node_modules/mongoose": {
"version": "7.5.1", "version": "7.6.5",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-7.5.1.tgz", "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-7.6.5.tgz",
"integrity": "sha512-gJCkUtW3KgAu7Uf8HTVv+S3wf9+xldR5LFYM6mABJJQHQB0aWUGyC3bWsl2X/6dVHZSoQq3wh5UV7rWC4FrewA==", "integrity": "sha512-ElHgGWVKQUawKBn0DXuHmSd3W5w5Kb8JUbDNQH30odhYCDKq9GCh+E1/SuN8jZGxrHgFyLrvYxLSpC36BpqS+w==",
"dependencies": { "dependencies": {
"bson": "^5.4.0", "bson": "^5.5.0",
"kareem": "2.5.1", "kareem": "2.5.1",
"mongodb": "5.8.1", "mongodb": "5.9.0",
"mpath": "0.9.0", "mpath": "0.9.0",
"mquery": "5.0.0", "mquery": "5.0.0",
"ms": "2.1.3", "ms": "2.1.3",
@ -1132,9 +1136,9 @@
"integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==" "integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w=="
}, },
"node_modules/punycode": { "node_modules/punycode": {
"version": "2.3.0", "version": "2.3.1",
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz",
"integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==", "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==",
"engines": { "engines": {
"node": ">=6" "node": ">=6"
} }
@ -1435,6 +1439,11 @@
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz",
"integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA==" "integrity": "sha512-WxONCrssBM8TSPRqN5EmsjVrsv4A8X12J4ArBiiayv3DyyG3ZlIg6yysuuSYdZsVz3TKcTg2fd//Ujd4CHV1iA=="
}, },
"node_modules/undici-types": {
"version": "5.26.5",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="
},
"node_modules/unpipe": { "node_modules/unpipe": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
@ -1487,6 +1496,11 @@
"node": ">=12" "node": ">=12"
} }
}, },
"node_modules/y": {
"version": "0.3.2",
"resolved": "https://registry.npmjs.org/y/-/y-0.3.2.tgz",
"integrity": "sha512-OJQTQuD3iqJqXzYKs2AgZqQrfPuTOjIFm3jvIc+2SxP6QSB5Ab9Ja9A5mWEDngjaJiEgsHbp2Q2RR/U2pTpTEw=="
},
"node_modules/yallist": { "node_modules/yallist": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",

View file

@ -6,7 +6,6 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server.js" "dev": "nodemon server.js"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
@ -21,8 +20,9 @@
"express-async-handler": "^1.2.0", "express-async-handler": "^1.2.0",
"express-session": "^1.17.3", "express-session": "^1.17.3",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
"mongoose": "^7.5.1", "mongoose": "^7.6.5",
"nodemon": "^3.0.1", "nodemon": "^3.0.1",
"validator": "^13.11.0" "validator": "^13.11.0",
"y": "^0.3.2"
} }
} }

34
routes/favRoute.js Normal file
View file

@ -0,0 +1,34 @@
const express = require('express');
const router = express.Router();
const fav = require('../models/favorites.js');
router.get('/', async (req, res, next) => {
try {
const favorites = await fav.find({});
res.json(favorites);
} catch (err) {
return next(err);
}
});
router.post('/', async (req, res, next) => {
try {
// ดึงข้อมูลที่ส่งมาจาก body ของ request
const { user_id, place_name, location, wheelchair_access, highway_type } = req.body;
const newFavorite = await fav.create({
user_id: user_id,
place_name: place_name,
location: location,
wheelchair_access: wheelchair_access,
highway_type: highway_type
});
res.status(201).json(newFavorite); // ส่งข้อมูลที่เพิ่มเข้าสู่ฐานข้อมูลกลับไปยัง client
} catch (err) {
return next(err); // หากเกิดข้อผิดพลาดในการสร้างข้อมูลใหม่ ส่งไปยัง error handling middleware
}
});
module.exports = router;

View file

@ -3,6 +3,7 @@ const app = express();
const cors = require('cors'); const cors = require('cors');
const dotenv = require("dotenv").config(); const dotenv = require("dotenv").config();
const connectDb = require('./config/dbConnection'); const connectDb = require('./config/dbConnection');
const fav = require('./routes/favRoute');
app.use(express.json()); app.use(express.json());
app.use(cors()); app.use(cors());
@ -14,6 +15,8 @@ app.get('/', function(req, res) {
}); });
}); });
app.use('/favorites',fav);
app.listen(process.env.PORT, () => { app.listen(process.env.PORT, () => {
console.log(`server's running on port ${process.env.PORT}`); console.log(`server's running on port ${process.env.PORT}`);
}) })