Finished contact CRUD and frontend with websockets

This commit is contained in:
canove
2020-07-24 10:57:16 -03:00
parent 6d4e2a60a9
commit e266765caa
11 changed files with 343 additions and 136 deletions

View File

@@ -61,14 +61,18 @@ exports.store = async (req, res) => {
exports.show = async (req, res) => {
const { contactId } = req.params;
const { id, name, number, extraInfo } = await Contact.findByPk(contactId, {
include: [{ model: ContactCustomField, as: "extraInfo" }],
});
const { id, name, number, email, extraInfo } = await Contact.findByPk(
contactId,
{
include: [{ model: ContactCustomField, as: "extraInfo" }],
}
);
res.status(200).json({
id,
name,
number,
email,
extraInfo,
});
};
@@ -93,3 +97,23 @@ exports.update = async (req, res) => {
res.status(200).json(contact);
};
exports.delete = async (req, res) => {
const io = getIO();
const { contactId } = req.params;
const contact = await Contact.findByPk(contactId);
if (!contact) {
return res.status(400).json({ error: "No contact found with this ID" });
}
await contact.destroy();
io.emit("contact", {
action: "delete",
contactId: contactId,
});
res.status(200).json({ message: "Contact deleted" });
};