Untitled
unknown
plain_text
2 years ago
2.6 kB
8
Indexable
const e = require("express");
const express = require("express");
const SellBuy = require("../mongoose/models/sellBuy")
const router = express.Router()
router.get("/", async (req, res) => {
try {
if (req.query.product) {
const data = await SellBuy.find({ productName: req.query.product })
res.status(200) .json(data)
} else if (req.query.sortBy) {
let list = req.query.sortBy
let ord = -1;
if (list.charAt(0) == "1")
{
ord = 1
}
if (list.includes("S")) {
const data = await SellBuy.find().sort({ soldPrice: ord })
res.status(200).json(data)
} else {
const data = await SellBuy.find().sort({ costPrice: ord })
res.status(200).json(data)
}
} else {
const data = await SellBuy.find()
res.status(290) .json(data)
}
} catch (err) {
res.status(200).json(data)
}
})
router.post("/", async (req, res, next) => {
const data = new SellBuy(req. body)
try {
if (data.productName.length < 4) {
const err = new Error("product name should have minimum of four characters")
err.status=400
next(err)
} else if(data.costPrice<=0){
const err=newError("cost price value cannot be zero or negative value")
err.status=400
next(err)
}else if(data.soldPrice<=0){
const err=newError("sold price value cannot be zero or negative value")
err.status=400
next(err)
}else{
await data.save();
res.status(201).json({message:"Product Added"})
}
}catch (err){
res.status(200).json(data)
}
})
router.patch("/:id",async(req,res,ne><t)=>{
try{
canst data=req.body.soldPrice;
if(data<=0){
const err=new Error("sold price value cannot be zero or negative value")
err.status=400
next(err)
}else{
res.status(200).json({message:"Updated Successfully"})
}
}catch (err){
res.status(200).json(data)
}
})
router.delete("/:id",async(req,res)=>{
try{
let id=req.params.id
if(id===undefined || id === '' || id===null){
res.status(400).json(data)
}
const data=await SellBuy.findByidAndDelete(id)
res.status(200).json({message : "Deleted successfully" })
} catch (err) {
res.status(400).json({error:err})
}
})Editor is loading...
Leave a Comment