Skip to content

Commit

Permalink
Changes in files
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinduPanchala committed Oct 18, 2020
1 parent 1c4f4b7 commit 977eafe
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 20 deletions.
56 changes: 56 additions & 0 deletions backend/routes/supplier.js
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 6 additions & 6 deletions backend/routes/supply.js
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -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,
Expand All @@ -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);
Expand Down
25 changes: 15 additions & 10 deletions src/components/add-supplier.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
35 changes: 31 additions & 4 deletions src/components/edit-supply.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 977eafe

Please sign in to comment.