Last active
November 17, 2019 04:17
-
-
Save RizkyRajitha/05118f4ae3437422572579859a6a2040 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
var router = express.Router(); | |
var ObjectId = require("mongoose").Types.ObjectId; | |
var { SupplyAd } = require("../models/supplyAd"); | |
router.get("/", (req, res) => { | |
SupplyAd.find({ | |
eDate: { | |
// mechchrai aulthen dmme | |
$gt: new Date().toISOString() //.slice(0, 10) + "T23:59:59.000Z" | |
} | |
}) | |
.then(docs => { | |
console.log(docs); | |
res.json(docs); | |
}) | |
.catch(err => console.log(err)); | |
}); | |
// router.get('/', (req, res) => { | |
// SupplyAd.find((err, docs) => { | |
// if (!err) { | |
// res.send(docs); | |
// } | |
// else { console.log('Error in Retriving SupplyAds :' + JSON.stringify(err, undefined, 2)); } | |
// }); | |
// }); | |
router.post("/", (req, res) => { | |
var sup = new SupplyAd({ | |
name: req.body.name, | |
price: req.body.price, | |
quantity: req.body.quantity, | |
hDate: req.body.hDate, | |
eDate: new Date(req.body.eDate).toISOString(), | |
des: req.body.des | |
}); | |
sup.save((err, doc) => { | |
if (!err) { | |
res.send(doc); | |
} else { | |
console.log( | |
"Error in SupplyAd save :" + JSON.stringify(err, undefined, 2) | |
); | |
} | |
}); | |
}); | |
router.get("/:id", (req, res) => { | |
if (!ObjectId.isValid(req.params.id)) | |
return res.status(400).send(`No record with given id : $(req.params.id)`); | |
SupplyAd.findById(req.params.id, (err, doc) => { | |
if (!err) { | |
res.send(doc); | |
} else { | |
console.log( | |
"Error in Retriving SupplyAd:" + JSON.stringify(err, undefined, 2) | |
); | |
} | |
}); | |
}); | |
router.put("/:id", (req, res) => { | |
if (!ObjectId.isValid(req.params.id)) | |
return res.status(400).send(`No record with given id : ${req.params.id}`); | |
var sup = { | |
name: req.body.name, | |
price: req.body.price, | |
quantity: req.body.quantity, | |
hDate: req.body.hDate, | |
eDate: req.body.eDate, | |
des: req.body.des | |
}; | |
SupplyAd.findByIdAndUpdate( | |
req.params.id, | |
{ $set: sup }, | |
{ new: true }, | |
(err, doc) => { | |
if (!err) { | |
res.send(doc); | |
} else { | |
console.log( | |
"Error in SupplyAd Upadte:" + JSON.stringify(err, undefined, 2) | |
); | |
} | |
} | |
); | |
}); | |
router.delete("/:id", (req, res) => { | |
if (!ObjectId.isValid(req.params.id)) | |
return res.status(400).send(`No record with given id : $(req.params.id)`); | |
SupplyAd.findByIdAndRemove(req.params.id, (err, doc) => { | |
if (!err) { | |
res.send(doc); | |
} else { | |
console.log( | |
"Error in SupplyAd Delete:" + JSON.stringify(err, undefined, 2) | |
); | |
} | |
}); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment