35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
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;
|