Last active 1749834976

lolpee69 revised this gist 1749834975. Go to revision

1 file changed, 105 insertions

sellandbuy.js(file created)

@@ -0,0 +1,105 @@
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;
Newer Older