sellandbuy.js
· 2.9 KiB · JavaScript
Raw
const e = require("express");
const express = require("express");
const SellBuy = require("../mongoose/models/sellBuy");
const sellAndBuyRouter = new express.Router();
// Route to add a new product
sellAndBuyRouter.post("/sellProduct", async (req, res) => {
try {
const { productName, costPrice } = req.body;
// Validation
if (!productName || productName.length < 4) {
return res.status(400).send({
error: "product name should have minimum of four characters",
});
}
if (costPrice <= 0) {
return res.status(400).send({
error: "cost price value cannot be zero or negative value",
});
}
const product = new SellBuy({ productName, costPrice });
await product.save();
res.status(201).send({ message: "Product Added" });
} catch (e) {
res.status(400).send({ error: "Failed to save product" });
}
});
// Route to update a product's sold price
sellAndBuyRouter.patch("/sellProduct/:id", async (req, res) => {
try {
const { soldPrice } = req.body;
if (soldPrice <= 0) {
return res.status(400).send({
error: "sold price value cannot be zero or negative value",
});
}
const product = await SellBuy.findById(req.params.id);
if (!product) {
return res.status(400).send({ error: "Product not found" });
}
product.soldPrice = soldPrice;
await product.save();
res.status(200).send({ message: "Updated Successfully" });
} catch (e) {
res.status(400).send({ error: "Failed to update product" });
}
});
// Route to get all products or filtered/sorted data
sellAndBuyRouter.get("/sellProduct", async (req, res) => {
try {
let query = {};
if (req.query.product) {
query.productName = req.query.product;
}
let products = await SellBuy.find(query);
if (req.query.sortBy) {
switch (req.query.sortBy) {
case "lowerCostPrice":
products = products.sort((a, b) => a.costPrice - b.costPrice);
break;
case "higherCostPrice":
products = products.sort((a, b) => b.costPrice - a.costPrice);
break;
case "lowerSoldPrice":
products = products.sort((a, b) => (a.soldPrice || 0) - (b.soldPrice || 0));
break;
case "higherSoldPrice":
products = products.sort((a, b) => (b.soldPrice || 0) - (a.soldPrice || 0));
break;
}
}
res.status(200).send(products);
} catch (e) {
res.status(400).send({ error: "Failed to fetch products" });
}
});
// Route to delete a product
sellAndBuyRouter.delete("/sellProduct/:id", async (req, res) => {
try {
const product = await SellBuy.findByIdAndDelete(req.params.id);
if (!product) {
return res.status(404).send({ error: "Product not found" });
}
res.status(200).send({ message: "Deleted successfully" });
} catch (e) {
res.status(400).send({ error: "Failed to delete product" });
}
});
module.exports = sellAndBuyRouter;
1 | const e = require("express"); |
2 | const express = require("express"); |
3 | const SellBuy = require("../mongoose/models/sellBuy"); |
4 | |
5 | const sellAndBuyRouter = new express.Router(); |
6 | |
7 | // Route to add a new product |
8 | sellAndBuyRouter.post("/sellProduct", async (req, res) => { |
9 | try { |
10 | const { productName, costPrice } = req.body; |
11 | |
12 | // Validation |
13 | if (!productName || productName.length < 4) { |
14 | return res.status(400).send({ |
15 | error: "product name should have minimum of four characters", |
16 | }); |
17 | } |
18 | if (costPrice <= 0) { |
19 | return res.status(400).send({ |
20 | error: "cost price value cannot be zero or negative value", |
21 | }); |
22 | } |
23 | |
24 | const product = new SellBuy({ productName, costPrice }); |
25 | await product.save(); |
26 | |
27 | res.status(201).send({ message: "Product Added" }); |
28 | } catch (e) { |
29 | res.status(400).send({ error: "Failed to save product" }); |
30 | } |
31 | }); |
32 | |
33 | // Route to update a product's sold price |
34 | sellAndBuyRouter.patch("/sellProduct/:id", async (req, res) => { |
35 | try { |
36 | const { soldPrice } = req.body; |
37 | |
38 | if (soldPrice <= 0) { |
39 | return res.status(400).send({ |
40 | error: "sold price value cannot be zero or negative value", |
41 | }); |
42 | } |
43 | |
44 | const product = await SellBuy.findById(req.params.id); |
45 | if (!product) { |
46 | return res.status(400).send({ error: "Product not found" }); |
47 | } |
48 | |
49 | product.soldPrice = soldPrice; |
50 | await product.save(); |
51 | |
52 | res.status(200).send({ message: "Updated Successfully" }); |
53 | } catch (e) { |
54 | res.status(400).send({ error: "Failed to update product" }); |
55 | } |
56 | }); |
57 | |
58 | // Route to get all products or filtered/sorted data |
59 | sellAndBuyRouter.get("/sellProduct", async (req, res) => { |
60 | try { |
61 | let query = {}; |
62 | if (req.query.product) { |
63 | query.productName = req.query.product; |
64 | } |
65 | |
66 | let products = await SellBuy.find(query); |
67 | |
68 | if (req.query.sortBy) { |
69 | switch (req.query.sortBy) { |
70 | case "lowerCostPrice": |
71 | products = products.sort((a, b) => a.costPrice - b.costPrice); |
72 | break; |
73 | case "higherCostPrice": |
74 | products = products.sort((a, b) => b.costPrice - a.costPrice); |
75 | break; |
76 | case "lowerSoldPrice": |
77 | products = products.sort((a, b) => (a.soldPrice || 0) - (b.soldPrice || 0)); |
78 | break; |
79 | case "higherSoldPrice": |
80 | products = products.sort((a, b) => (b.soldPrice || 0) - (a.soldPrice || 0)); |
81 | break; |
82 | } |
83 | } |
84 | |
85 | res.status(200).send(products); |
86 | } catch (e) { |
87 | res.status(400).send({ error: "Failed to fetch products" }); |
88 | } |
89 | }); |
90 | |
91 | // Route to delete a product |
92 | sellAndBuyRouter.delete("/sellProduct/:id", async (req, res) => { |
93 | try { |
94 | const product = await SellBuy.findByIdAndDelete(req.params.id); |
95 | if (!product) { |
96 | return res.status(404).send({ error: "Product not found" }); |
97 | } |
98 | |
99 | res.status(200).send({ message: "Deleted successfully" }); |
100 | } catch (e) { |
101 | res.status(400).send({ error: "Failed to delete product" }); |
102 | } |
103 | }); |
104 | |
105 | module.exports = sellAndBuyRouter; |
106 |