mirror of
https://github.com/Alvin-Zilverstand/challenge-11.git
synced 2026-03-06 02:56:27 +01:00
65 lines
1.8 KiB
JavaScript
65 lines
1.8 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const Contact = require('../models/Contact');
|
|
|
|
// Get all contacts for a customer
|
|
router.get('/customer/:customerId', async (req, res) => {
|
|
try {
|
|
const contacts = await Contact.find({ customer: req.params.customerId })
|
|
.sort({ createdAt: -1 })
|
|
.populate('user', 'username');
|
|
res.json(contacts);
|
|
} catch (error) {
|
|
console.error('Error fetching contacts:', error);
|
|
res.status(500).json({ message: 'Server error' });
|
|
}
|
|
});
|
|
|
|
// Create new contact
|
|
router.post('/', async (req, res) => {
|
|
try {
|
|
const contact = new Contact({
|
|
...req.body,
|
|
user: req.user.id, // This will be set by the auth middleware
|
|
});
|
|
await contact.save();
|
|
res.status(201).json(contact);
|
|
} catch (error) {
|
|
console.error('Error creating contact:', error);
|
|
res.status(500).json({ message: 'Server error' });
|
|
}
|
|
});
|
|
|
|
// Update contact
|
|
router.put('/:id', async (req, res) => {
|
|
try {
|
|
const contact = await Contact.findByIdAndUpdate(
|
|
req.params.id,
|
|
req.body,
|
|
{ new: true }
|
|
);
|
|
if (!contact) {
|
|
return res.status(404).json({ message: 'Contact not found' });
|
|
}
|
|
res.json(contact);
|
|
} catch (error) {
|
|
console.error('Error updating contact:', error);
|
|
res.status(500).json({ message: 'Server error' });
|
|
}
|
|
});
|
|
|
|
// Delete contact
|
|
router.delete('/:id', async (req, res) => {
|
|
try {
|
|
const contact = await Contact.findByIdAndDelete(req.params.id);
|
|
if (!contact) {
|
|
return res.status(404).json({ message: 'Contact not found' });
|
|
}
|
|
res.json({ message: 'Contact deleted successfully' });
|
|
} catch (error) {
|
|
console.error('Error deleting contact:', error);
|
|
res.status(500).json({ message: 'Server error' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|