const Commercial = require('../models/commercial.model.js');
// Create and Save a new admin
exports.create = (req, res) => {
// Validate request
if (!req.body.cin) {
return res.status(400).send({
message: "commercial content can not be empty"
});
}
// Create a admin
const commercial = new Commercial({
cin: req.body.cin || "Untitled commercial",
nomPrenom: req.body.nomPrenom,
idDepartement: req.body.idDepartement,
zoneDeSuivie: req.body.zoneDeSuivie,
numTelephone: req.body.numTelephone,
fonction: req.body.fonction,
statue: req.body.statue,
email: req.body.email,
login: req.body.login,
password: req.body.password,
});
// Save admin in the database
commercial.save()
.then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while creating the commercial."
});
});
};
// Retrieve and return all admin from the database.
exports.findAll = (req, res) => {
Commercial.find()
.then(commercials => {
res.send(commercials);
}).catch(err => {
res.status(500).send({
message: err.message || "Some error occurred while retrieving commercials."
});
});
};
// Find a single admin with a adminId
exports.findOne = (req, res) => {
Commercial.findById(req.params.commercialId)
.then(commercial => {
if (!commercial) {
return res.status(404).send({
message: "commercial not found with id " + req.params.commercialId
});
}
res.send(commercial);
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).send({
message: "commercial not found with id " + req.params.commercialId
});
}
return res.status(500).send({
message: "Error retrieving commercial with id " + req.params.commercialId
});
});
};
// Update a admin identified by the adminId in the request
exports.update = (req, res) => {
// Validate Request
if (!req.body.cin) {
return res.status(400).send({
message: "commercial content can not be empty"
});
}
// Find admin and update it with the request body
Commercial.findByIdAndUpdate(req.params.commercialId, {
cin: req.body.cin || "Untitled commercial",
nomPrenom: req.body.nomPrenom,
idDepartement: req.body.idDepartement,
zoneDeSuivie: req.body.zoneDeSuivie,
numTelephone: req.body.numTelephone,
fonction: req.body.fonction,
statue: req.body.statue,
email: req.body.email,
login: req.body.login,
password: req.body.password,
}, { new: true })
.then(commercial => {
if (!commercial) {
return res.status(404).send({
message: "commercial not found with id " + req.params.commercialId
});
}
res.send(commercial);
}).catch(err => {
if (err.kind === 'ObjectId') {
return res.status(404).send({
message: "commercial not found with id " + req.params.commercialId
});
}
return res.status(500).send({
message: "Error updating commercial with id " + req.params.commercialId
});
});
};
// Delete a admin with the specified adminId in the request
exports.delete = (req, res) => {
Commercial.findByIdAndRemove(req.params.commercialId)
.then(commercial => {
if (!commercial) {
return res.status(404).send({
message: "commercial not found with id " + req.params.commercialId
});
}
res.send({ message: "commercial deleted successfully!" });
}).catch(err => {
if (err.kind === 'ObjectId' || err.name === 'NotFound') {
return res.status(404).send({
message: "commercial not found with id " + req.params.commercialId
});
}
return res.status(500).send({
message: "Could not delete commercial with id " + req.params.commercialId
});
});
};