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

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;