Merge branch 'main' of gitlab.com:openKMITL/little-lines/backend

This commit is contained in:
NekoVari 2023-11-19 03:48:29 +07:00
commit 78e10c415c
5 changed files with 116 additions and 28 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;