Select Git revision
partService.js
-
Eunhak Lee authoredEunhak Lee authored
TripController.js 5.13 KiB
const Trip = require('../models/trips');
const User = require('../models/user');
// 전체 여행 목록 조회
const getAllTrips = async (req, res) => {
try {
const trips = await Trip.find().populate('create_by collaborators');
res.status(200).json(trips);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
// 특정 여행 계획 조회
const getTripById = async (req, res) => {
try {
const trip = await Trip.findById(req.params.id).populate('create_by collaborators');
if (!trip) return res.status(404).json({ message: 'Trip not found' });
res.status(200).json(trip);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
// 새로운 여행 계획 생성
const createTrip = async (req, res) => {
try {
const { name, start_date, end_date, create_by, location, collaborators } = req.body;
const trip = new Trip({ name, start_date, end_date, create_by, location, collaborators });
const savedTrip = await trip.save();
res.status(201).json(savedTrip);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
// 특정 여행 계획 수정
const updateTrip = async (req, res) => {
try {
const { name, start_date, end_date, collaborators } = req.body;
const updatedTrip = await Trip.findByIdAndUpdate(
req.params.id,
{ name, start_date, end_date, collaborators },
{ new: true }
);
if (!updatedTrip) return res.status(404).json({ message: 'Trip not found' });
res.status(200).json(updatedTrip);
} catch (err) {
res.status(500).json({ error: err.message });
}
};
// 특정 여행 계획 삭제
const deleteTrip = async (req, res) => {
try {
const deletedTrip = await Trip.findByIdAndDelete(req.params.id);
if (!deletedTrip) return res.status(404).json({ message: 'Trip not found' });
res.status(200).json({ message: 'Trip deleted successfully' });
} catch (err) {
res.status(500).json({ error: err.message });
}
};
// 공동 작업자 추가
const addCollaborator = async (req, res) => {
try {
const { collaboratorEmail } = req.body;
const collaborator = await User.findOne({ email: collaboratorEmail });
if (!collaborator) {