const Client = require('../models/client.model').Client;
// Post a Customer
exports.create = (req, res) => {
// Save to MariaDB database
Client.create({
cardcode: req.body.cardcode,
Cardname: req.body.Cardname,
address: req.body.address,
country: req.body.country,
cargo: req.body.cargo,
Tel: req.body.Tel,
FirstName: req.body.FirstName,
LastName: req.body.LastName,
E_MailL: req.body.E_MailL,
clienteNuevo: req.body.clienteNuevo
})
.then(client => {
// Send created customer to client
res.satus(200).json({ message: 'Client Created Successfull', client });
})
.catch(error => res.status(400).send(error))
};
// Fetch all Customers
exports.findAll = (req, res) => {
Customer.findAll({
attributes: { exclude: ["createdAt", "updatedAt"] }
})
.then(client => {
res.json(client);
})
.catch(error => res.status(400).send(error))
};
// Find a Customer by Id
exports.findById = (req, res) => {
Client.findById(req.params.cardcode, { attributes: { exclude: ["createdAt", "updatedAt"] } })
.then(client => {
if (!client) {
return res.status(404).json({ message: "Client Not Found" })
}
return res.status(200).json(client)
})
.catch(error => res.status(400).send(error));
};
// Update a Customer
exports.update = (req, res) => {
return Client.findById(req.params.cardcode)
.then(
client => {
if (!client) {
return res.status(404).json({
message: 'Customer Not Found',
});
}
return client.update({
Cardname: req.body.Cardname,
address: req.body.address,
country: req.body.country,
cargo: req.body.cargo,
Tel: req.body.Tel,
FirstName: req.body.FirstName,
LastName: req.body.LastName,
E_MailL: req.body.E_MailL,
})
.then(() => res.status(200).json(client))
.catch((error) => res.status(400).send(error));
}
)
.catch((error) => res.status(400).send(error));
};
// Delete a Customer by Id
exports.delete = (req, res) => {
return Client
.findById(req.params.cardcode)
.then(client => {
if (!client) {
return res.status(400).send({
message: 'Customer Not Found',
});
}
return client.destroy()
.then(() => res.status(200).json({ message: "Destroy successfully!" }))
.catch(error => res.status(400).send(error));
})
.catch(error => res.status(400).send(error));
};