get by id

This commit is contained in:
p11037 2023-11-22 16:45:06 +07:00
parent 0feed90cfc
commit b28f6d5430
5 changed files with 80 additions and 41 deletions

View file

@ -0,0 +1,58 @@
const fav = require('../models/favorites.js');
const user = require('../models/user.js');
const mongoose = require('mongoose');
const asyncHandler = require('express-async-handler');
// Get all favorites => GET api/favorites
const getAllFavorites = asyncHandler(async (req, res, next) => {
try {
const favorites = await fav.find({});
res.json(favorites);
} catch (err) {
return next(err);
}
});
// Create new favorite => POST api/favorites
const createFavorite = asyncHandler(async (req, res, next) => {
try {
const { user_id, place_name, location, wheelchair_access, highway_type } = req.body;
const newFavorite = await fav.create({
user_id: user_id, // กำหนด user ID ให้กับ favorite
place_name,
location,
wheelchair_access,
highway_type
});
res.status(201).json(newFavorite);
} catch (err) {
return next(err);
}
});
const getFavoritesByUserId = asyncHandler(async (req, res, next) => {
try {
const userId = req.params.userId;
//console.log(userId)
const favorites = await fav.find({ "user_id": userId });
//console.log(favorites)
if (!favorites || favorites.length === 0) {
return res.status(404).json({ message: 'No favorites found for this user ID' });
}
res.json(favorites);
} catch (err) {
return next(err);
}
});
module.exports = { getAllFavorites, createFavorite, getFavoritesByUserId };

View file

@ -1,6 +1,6 @@
const asyncHandler = require('express-async-handler');
const user = require('../models/user');
const sendToken = require('../utils/jwtToken');
const user = require('../models/user');
// Get all users => GET api/users
const getAllUser = asyncHandler(async (req, res) => {

View file

@ -1,27 +1,31 @@
const { default: mongoose } = require("mongoose")
const favSchema = new mongoose.Schema({
user_id : {type:String,required: true},
place_name : String,
const favoriteSchema = new mongoose.Schema({
user_id: {
type: String,
required: true
},
place_name: String,
location: {
type: {
type: String,
enum: ['Point'],
default: 'Point'
required: true
},
coordinates: {
type: [Number],
required: true
}
},
wheelchair_access : String,
highway_type : String,
updated_at : {type:Date, default:Date.now}
})
wheelchair_access: String,
highway_type: String,
updated_at: { type: Date, default: Date.now }
});
module.exports = mongoose.model('favorites',favSchema)
module.exports = mongoose.model('favorites',favoriteSchema)
//ตัวอย่างข้อมูล
// {

View file

@ -1,34 +1,11 @@
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
}
});
const { getAllFavorites,
createFavorite,
getFavoritesByUserId } = require('../controllers/favController');
router.route('/').get(getAllFavorites);
router.route('/:userId').get(getFavoritesByUserId);
router.route('/create').post(createFavorite);
module.exports = router;

View file

@ -3,7 +3,7 @@ const app = express();
const cors = require('cors');
const dotenv = require("dotenv").config();
const connectDb = require('./config/dbConnection');
const fav = require('./routes/favRoute');
// const fav = require('./routes/favRoute');
app.use(express.json());
app.use(cors());
@ -15,7 +15,7 @@ app.get('/', function(req, res) {
});
});
app.use('/favorites',fav);
app.use('/api/favorites', require('./routes/favRoute'));
app.listen(process.env.PORT, () => {
console.log(`server's running on port ${process.env.PORT}`);