You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
3.1 KiB
99 lines
3.1 KiB
Etudiant = require('../models/etudiantModel');
|
|
|
|
// Handle index actions (method: GET)
|
|
exports.index = function (req, res) {
|
|
Etudiant.get(function (err, etudiants) {
|
|
if (err) {
|
|
res.json({
|
|
status: "error",
|
|
message: err,
|
|
});
|
|
}
|
|
res.json({
|
|
status: "success",
|
|
message: "Voici la liste des etudiants",
|
|
data: etudiants
|
|
});
|
|
});
|
|
};
|
|
|
|
// Handling etudiant creation actions (method: POST)
|
|
exports.new = function (req, res) {
|
|
var etudiant = new Etudiant();
|
|
etudiant.numEtudiant = req.body.numEtudiant ? req.body.numEtudiant : etudiant.numEtudiant;
|
|
etudiant.firstname = req.body.firstname;
|
|
etudiant.lastname = req.body.lastname;
|
|
etudiant.cycle = req.body.cycle;
|
|
etudiant.adresse = req.body.adresse;
|
|
etudiant.email = req.body.email;
|
|
etudiant.cours = req.body.cours;
|
|
|
|
// fait un save de notre etudiant envoyé à l'API (display l'erreur si il y en a une)
|
|
etudiant.save(function (err) {
|
|
if (err){
|
|
res.json(err);
|
|
} else {
|
|
res.json({
|
|
message: 'New etudiant created!',
|
|
data: etudiant
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
// Handle view etudiant info ById
|
|
exports.view = function (req, res) {
|
|
Etudiant.findById(req.params.etudiant_id, function (err, etudiant) {
|
|
// NOTE: Etudiant.findById(..) revient à faire Etudiant.findOne({_id: id}, function (err, user) { ... });
|
|
// directement dans le shell MongoDB tester: db.etudiant.find({_id:ObjectId("5e7b57286335730f8ae4aaed")}).pretty()
|
|
if (err){
|
|
res.send(err);
|
|
} else {
|
|
res.json({
|
|
message: 'loading etudiant details with _id: '+req.params.etudiant_id,
|
|
data: etudiant
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
// Handle update etudiant info (method: PUT|PATCH)
|
|
exports.update = function (req, res) {
|
|
Etudiant.findById(req.params.etudiant_id, function (err, etudiant) {
|
|
if (err)
|
|
res.send(err);
|
|
etudiant.numEtudiant = req.body.numEtudiant ? req.body.numEtudiant : etudiant.numEtudiant;
|
|
etudiant.firstname = req.body.firstname;
|
|
etudiant.lastname = req.body.lastname;
|
|
etudiant.cycle = req.body.cycle;
|
|
etudiant.adresse = req.body.adresse;
|
|
etudiant.email = req.body.email;
|
|
etudiant.cours = req.body.cours;
|
|
|
|
// save the etudiant and check for errors
|
|
etudiant.save(function (err) {
|
|
if (err)
|
|
res.json(err);
|
|
res.json({
|
|
message: 'etudiant Info updated',
|
|
data: etudiant
|
|
});
|
|
});
|
|
});
|
|
};
|
|
|
|
// Handle delete etudiant (method: DELETE)
|
|
exports.delete = function (req, res) {
|
|
Etudiant.remove({
|
|
_id: req.params.etudiant_id
|
|
}, function (err, etudiant) {
|
|
if (err){
|
|
res.send(err);
|
|
} else {
|
|
res.json({
|
|
status: "success",
|
|
message: 'etudiant deleted'
|
|
});
|
|
}
|
|
});
|
|
};
|