mirror of
https://gitlab.com/little-lines/backend.git
synced 2024-11-23 13:36:50 +00:00
get by id
This commit is contained in:
parent
0feed90cfc
commit
b28f6d5430
5 changed files with 80 additions and 41 deletions
58
controllers/favController.js
Normal file
58
controllers/favController.js
Normal 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 };
|
|
@ -1,6 +1,6 @@
|
||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
const user = require('../models/user');
|
|
||||||
const sendToken = require('../utils/jwtToken');
|
const sendToken = require('../utils/jwtToken');
|
||||||
|
const user = require('../models/user');
|
||||||
|
|
||||||
// Get all users => GET api/users
|
// Get all users => GET api/users
|
||||||
const getAllUser = asyncHandler(async (req, res) => {
|
const getAllUser = asyncHandler(async (req, res) => {
|
||||||
|
|
|
@ -1,27 +1,31 @@
|
||||||
const { default: mongoose } = require("mongoose")
|
const { default: mongoose } = require("mongoose")
|
||||||
|
|
||||||
const favSchema = new mongoose.Schema({
|
const favoriteSchema = new mongoose.Schema({
|
||||||
user_id : {type:String,required: true},
|
user_id: {
|
||||||
place_name : String,
|
type: String,
|
||||||
|
required: true
|
||||||
|
},
|
||||||
|
place_name: String,
|
||||||
location: {
|
location: {
|
||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
enum: ['Point'],
|
enum: ['Point'],
|
||||||
default: 'Point'
|
required: true
|
||||||
},
|
},
|
||||||
coordinates: {
|
coordinates: {
|
||||||
type: [Number],
|
type: [Number],
|
||||||
required: true
|
required: true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
wheelchair_access : String,
|
wheelchair_access: String,
|
||||||
highway_type : String,
|
highway_type: String,
|
||||||
updated_at : {type:Date, default:Date.now}
|
updated_at: { type: Date, default: Date.now }
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = mongoose.model('favorites',favSchema)
|
|
||||||
|
module.exports = mongoose.model('favorites',favoriteSchema)
|
||||||
|
|
||||||
//ตัวอย่างข้อมูล
|
//ตัวอย่างข้อมูล
|
||||||
// {
|
// {
|
||||||
|
|
|
@ -1,34 +1,11 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const fav = require('../models/favorites.js');
|
const { getAllFavorites,
|
||||||
|
createFavorite,
|
||||||
router.get('/', async (req, res, next) => {
|
getFavoritesByUserId } = require('../controllers/favController');
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
router.route('/').get(getAllFavorites);
|
||||||
|
router.route('/:userId').get(getFavoritesByUserId);
|
||||||
|
router.route('/create').post(createFavorite);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
|
@ -3,7 +3,7 @@ const app = express();
|
||||||
const cors = require('cors');
|
const cors = require('cors');
|
||||||
const dotenv = require("dotenv").config();
|
const dotenv = require("dotenv").config();
|
||||||
const connectDb = require('./config/dbConnection');
|
const connectDb = require('./config/dbConnection');
|
||||||
const fav = require('./routes/favRoute');
|
// const fav = require('./routes/favRoute');
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(cors());
|
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, () => {
|
app.listen(process.env.PORT, () => {
|
||||||
console.log(`server's running on port ${process.env.PORT}`);
|
console.log(`server's running on port ${process.env.PORT}`);
|
||||||
|
|
Loading…
Reference in a new issue