mirror of
https://gitlab.com/little-lines/backend.git
synced 2025-07-07 14:31:04 +00:00
favApi
This commit is contained in:
parent
05c8212b75
commit
0d364fa4cc
5 changed files with 116 additions and 29 deletions
34
routes/favRoute.js
Normal file
34
routes/favRoute.js
Normal 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;
|
Loading…
Add table
Add a link
Reference in a new issue