mirror of
https://github.com/Alvin-Zilverstand/challenge-11.git
synced 2026-03-07 05:47:23 +01:00
Add create-admin script to package.json and set up /api/users route in server.js for user management functionality.
This commit is contained in:
65
routes/contacts.js
Normal file
65
routes/contacts.js
Normal file
@@ -0,0 +1,65 @@
|
||||
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;
|
||||
Reference in New Issue
Block a user