From 0d364fa4cc8667442d02168e7ad4f33f56cd2d50 Mon Sep 17 00:00:00 2001 From: p11037 Date: Sat, 18 Nov 2023 23:59:43 +0700 Subject: [PATCH] favApi --- models/favorites.js | 36 +++++++++++++++++++++++++ package-lock.json | 66 +++++++++++++++++++++++++++------------------ package.json | 6 ++--- routes/favRoute.js | 34 +++++++++++++++++++++++ server.js | 3 +++ 5 files changed, 116 insertions(+), 29 deletions(-) create mode 100644 models/favorites.js create mode 100644 routes/favRoute.js diff --git a/models/favorites.js b/models/favorites.js new file mode 100644 index 0000000..58b6ac8 --- /dev/null +++ b/models/favorites.js @@ -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" +// } diff --git a/package-lock.json b/package-lock.json index 218ed60..2cd5a19 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,29 +18,33 @@ "express-async-handler": "^1.2.0", "express-session": "^1.17.3", "jsonwebtoken": "^9.0.2", - "mongoose": "^7.5.1", + "mongoose": "^7.6.5", "nodemon": "^3.0.1", - "validator": "^13.11.0" + "validator": "^13.11.0", + "y": "^0.3.2" } }, "node_modules/@mongodb-js/saslprep": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.0.tgz", - "integrity": "sha512-Xfijy7HvfzzqiOAhAepF4SGN5e9leLkMvg/OPOF97XemjfVCYN/oWa75wnkc6mltMSTwY+XlbhWgUOJmkFspSw==", + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.1.tgz", + "integrity": "sha512-t7c5K033joZZMspnHg/gWPE4kandgc2OxE74aYOtGKfgB9VPuVJPix0H6fhmm2erj5PBJ21mqcx34lpIGtUCsQ==", "optional": true, "dependencies": { "sparse-bitfield": "^3.0.3" } }, "node_modules/@types/node": { - "version": "20.6.0", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.0.tgz", - "integrity": "sha512-najjVq5KN2vsH2U/xyh2opaSEz6cZMR2SetLIlxlj08nOcmPOemJmUK2o4kUzfLqfrWE0PIrNeE16XhYDd3nqg==" + "version": "20.9.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.9.1.tgz", + "integrity": "sha512-HhmzZh5LSJNS5O8jQKpJ/3ZcrrlG6L70hpGqMIAoM9YVD0YBRNWYsfwcXq8VnSjlNpCpgLzMXdiPo+dxcvSmiA==", + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/@types/webidl-conversions": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.0.tgz", - "integrity": "sha512-xTE1E+YF4aWPJJeUzaZI5DRntlkY3+BCVJi0axFptnjGmAoWxkyREIh/XMrfxVLejwQxMCfDXdICo0VLxThrog==" + "version": "7.0.3", + "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz", + "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA==" }, "node_modules/@types/whatwg-url": { "version": "8.2.2", @@ -147,9 +151,9 @@ } }, "node_modules/bson": { - "version": "5.4.0", - "resolved": "https://registry.npmjs.org/bson/-/bson-5.4.0.tgz", - "integrity": "sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==", + "version": "5.5.1", + "resolved": "https://registry.npmjs.org/bson/-/bson-5.5.1.tgz", + "integrity": "sha512-ix0EwukN2EpC0SRWIj/7B5+A6uQMQy6KMREI9qQqvgpkV2frH63T0UDVd1SYedL6dNCmDBYB3QtXi4ISk9YT+g==", "engines": { "node": ">=14.20.1" } @@ -866,11 +870,11 @@ } }, "node_modules/mongodb": { - "version": "5.8.1", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.8.1.tgz", - "integrity": "sha512-wKyh4kZvm6NrCPH8AxyzXm3JBoEf4Xulo0aUWh3hCgwgYJxyQ1KLST86ZZaSWdj6/kxYUA3+YZuyADCE61CMSg==", + "version": "5.9.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-5.9.0.tgz", + "integrity": "sha512-g+GCMHN1CoRUA+wb1Agv0TI4YTSiWr42B5ulkiAfLLHitGK1R+PkSAf3Lr5rPZwi/3F04LiaZEW0Kxro9Fi2TA==", "dependencies": { - "bson": "^5.4.0", + "bson": "^5.5.0", "mongodb-connection-string-url": "^2.6.0", "socks": "^2.7.1" }, @@ -915,13 +919,13 @@ } }, "node_modules/mongoose": { - "version": "7.5.1", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-7.5.1.tgz", - "integrity": "sha512-gJCkUtW3KgAu7Uf8HTVv+S3wf9+xldR5LFYM6mABJJQHQB0aWUGyC3bWsl2X/6dVHZSoQq3wh5UV7rWC4FrewA==", + "version": "7.6.5", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-7.6.5.tgz", + "integrity": "sha512-ElHgGWVKQUawKBn0DXuHmSd3W5w5Kb8JUbDNQH30odhYCDKq9GCh+E1/SuN8jZGxrHgFyLrvYxLSpC36BpqS+w==", "dependencies": { - "bson": "^5.4.0", + "bson": "^5.5.0", "kareem": "2.5.1", - "mongodb": "5.8.1", + "mongodb": "5.9.0", "mpath": "0.9.0", "mquery": "5.0.0", "ms": "2.1.3", @@ -1132,9 +1136,9 @@ "integrity": "sha512-77DZwxQmxKnu3aR542U+X8FypNzbfJ+C5XQDk3uWjWxn6151aIMGthWYRXTqT1E5oJvg+ljaa2OJi+VfvCOQ8w==" }, "node_modules/punycode": { - "version": "2.3.0", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz", - "integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", "engines": { "node": ">=6" } @@ -1435,6 +1439,11 @@ "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.5.tgz", "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": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", @@ -1487,6 +1496,11 @@ "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": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", diff --git a/package.json b/package.json index 67c8aa3..64464f6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "nodemon server.js" - }, "keywords": [], "author": "", @@ -21,8 +20,9 @@ "express-async-handler": "^1.2.0", "express-session": "^1.17.3", "jsonwebtoken": "^9.0.2", - "mongoose": "^7.5.1", + "mongoose": "^7.6.5", "nodemon": "^3.0.1", - "validator": "^13.11.0" + "validator": "^13.11.0", + "y": "^0.3.2" } } diff --git a/routes/favRoute.js b/routes/favRoute.js new file mode 100644 index 0000000..0180ff7 --- /dev/null +++ b/routes/favRoute.js @@ -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; diff --git a/server.js b/server.js index c7c3071..9d1a20a 100644 --- a/server.js +++ b/server.js @@ -3,6 +3,7 @@ const app = express(); const cors = require('cors'); const dotenv = require("dotenv").config(); const connectDb = require('./config/dbConnection'); +const fav = require('./routes/favRoute'); app.use(express.json()); app.use(cors()); @@ -14,6 +15,8 @@ app.get('/', function(req, res) { }); }); +app.use('/favorites',fav); + app.listen(process.env.PORT, () => { console.log(`server's running on port ${process.env.PORT}`); })