From 977eafeaa55f5bc8f10b620ef8ed628c5821c9bd Mon Sep 17 00:00:00 2001 From: MalinduPanchala Date: Sun, 18 Oct 2020 10:49:19 +0530 Subject: [PATCH] Changes in files --- backend/routes/supplier.js | 56 ++++++++++++++++++++++++ backend/routes/supply.js | 12 ++--- src/components/add-supplier.component.js | 25 ++++++----- src/components/edit-supply.component.js | 35 +++++++++++++-- 4 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 backend/routes/supplier.js diff --git a/backend/routes/supplier.js b/backend/routes/supplier.js new file mode 100644 index 0000000..70a8599 --- /dev/null +++ b/backend/routes/supplier.js @@ -0,0 +1,56 @@ +const router = require("express").Router(); +let Supplier = require("../models/supply.model"); + +router.route("/").get((req, res) => { + Supplier + .find() + .then((suppliers) => res.json(suppliers)) + .catch((err) => res.status(400).json("Error : " + err)); +}); + +router.route("/add").post((req, res) => { + const supplierName = (req.body.supplierName); + const address = (req.body.address); + const contactNumber = (req.body.contactNumber); + const email = (req.body.email); + + const newSuppliers = new Supplier({ + supplierName, + address, + contactNumber, + email + }); + newSuppliers + .save() + .then(() => res.json("supplier added!")) + .catch((err) => res.status(400).json("Error : " + err)); +}); +router.route("/:id").get((req, res) => { + Supplier.findById(req.params.id) + .then((supplier) => res.json(supplier)) + .catch((err) => res.status(400).json("Error : " + err)); +}); + +router.route("/:id").delete((req, res) => { + supplier.findByIdAndDelete(req.params.id) + .then(() => res.json("supply deleted")) + .catch((err) => res.status(400).json("Error : " + err)); +}); + +router.route("/update/:id").post((req, res) => { + Supplier.findById(req.params.id) + .then((supplieirs) => { + suppliers.supplierName = (req.body.supplierName); + suppliers.address = (req.body.address); + suppliers.contactNumber = (req.body.contactNumber); + suppliers.email = (req.body.email); + + suppliers + .save() + .then(() => res.json("supplier updated")) + .catch((err) => res.status(400).json("Error : " + err)); + }) + + .catch((err) => res.status(400).json("Error : " + err)); +}); +module.exports = router; diff --git a/backend/routes/supply.js b/backend/routes/supply.js index 2a59d23..70d3d02 100644 --- a/backend/routes/supply.js +++ b/backend/routes/supply.js @@ -1,8 +1,8 @@ const router = require("express").Router(); -let supply = require("../models/supply.model"); +let Supply = require("../models/supply.model"); router.route("/").get((req, res) => { - supply + Supply .find() .then((supplies) => res.json(supplies)) .catch((err) => res.status(400).json("Error : " + err)); @@ -16,7 +16,7 @@ router.route("/add").post((req, res) => { const date = (req.body.date); const description = (req.body.description); - const newSupplies = new supply({ + const newSupplies = new Supply({ itemName, supplierName, price, @@ -30,19 +30,19 @@ router.route("/add").post((req, res) => { .catch((err) => res.status(400).json("Error : " + err)); }); router.route("/:id").get((req, res) => { - supply.findById(req.params.id) + Supply.findById(req.params.id) .then((supplies) => res.json(supplies)) .catch((err) => res.status(400).json("Error : " + err)); }); router.route("/:id").delete((req, res) => { - supply.findByIdAndDelete(req.params.id) + Supply.findByIdAndDelete(req.params.id) .then(() => res.json("supply deleted")) .catch((err) => res.status(400).json("Error : " + err)); }); router.route("/update/:id").post((req, res) => { - supply.findById(req.params.id) + Supply.findById(req.params.id) .then((supplies) => { supplies.itemName = (req.body.itemName); supplies.supplierName = (req.body.supplierName); diff --git a/src/components/add-supplier.component.js b/src/components/add-supplier.component.js index 743f79b..699a593 100644 --- a/src/components/add-supplier.component.js +++ b/src/components/add-supplier.component.js @@ -131,17 +131,22 @@ export default class AddSupplier extends Component { this.validateContact(); this.validateEmail(); - if (this.state.validEmail || this.state.validContact) { - window.confirm( - "Are you sure to submit the for without filling all the fields" - ); - } - - /*axios + if ( + this.state.validSupplierName == true && + this.state.validAddress == true + ) { + axios .post("http://localhost:5000/supply/add", supply) - .then((res) => console.log(res.data)); - - window.location = "/supplyView";*/ + .then((res) => { + alert("Insert Success"); + window.location = "/viewSupply"; + }) + .catch((err) => { + console.log(err); + }); + } else { + window.alert("hello"); + } } render() { diff --git a/src/components/edit-supply.component.js b/src/components/edit-supply.component.js index 9c18171..8e205d4 100644 --- a/src/components/edit-supply.component.js +++ b/src/components/edit-supply.component.js @@ -30,13 +30,36 @@ export default class EditSupply extends Component { validQuantity: true, validDescrption: true, }; + } - this.supplyReset = { - itemName: this.state.itemName, - }; + componentDidMount() { + axios + .get("http://localhost:5000/supply/" + props.match.params.id) + .then((res) => { + this.setState({ + id: res.data._id, + itemName: res.data.itemName, + supplierName: res.data.supplierName, + date: res.data.date, + price: res.data.price, + quantity: res.data.quantity, + description: res.data.description + }); + + this.supplyReset = { + id: res.data._id, + itemName: res.data.itemName, + supplierName: res.data.supplierName, + date: res.data.date, + price: res.data.price, + quantity: res.data.quantity, + description: res.data.description + } + }); } supplyReset = { + id: "", itemName: "", supplierName: "", date: "", @@ -158,7 +181,11 @@ export default class EditSupply extends Component { onClickDelete(e) { e.preventDefault(); - window.confirm("Are you sure to delete this record"); + if (window.confirm("Are you sure to delete this record")){ + axios.delete('http://localhost:5000/supply/'+this.state.id).then((res)=> { + console.log("delete successfull") + }).catch((err) => console.log(err)); + } } onSubmit(e) {