| const e = require("express");
|
| const express = require("express");
|
| const SellBuy =require("../mongoose/models/sellBuy")
|
|
|
| // setting up the router
|
|
|
| const sellAndBuyRouter = new express.Router();
|
|
|
| sellAndBuyRouter.get("/sellProduct", async(req, res) => {
|
| try {
|
| let sort;
|
| let filter={};
|
| if (req.query.product) {
|
| filter.productName = req.query.product;
|
| }
|
| if(req.query.sortBy==="lowerCostPrice") {
|
| sort={ costPrice: 1};
|
| }else if(req.query.sortBy==="higherCostPrice") {
|
| sort={costPrice:-1};
|
| }else if(req.query.sortBy==="lowerSoldPrice") {
|
| sort={soldPrice:1};
|
| }else if(req.query.sortBy==="higherSoldPrice") {
|
| sort={soldPrice:-1};
|
| }
|
| const sellBuy = await SellBuy.find(filter).sort(sort);
|
| res.status(200).json(sellBuys);
|
| } catch {
|
| res.status(400);
|
| }
|
| })
|
|
|
| sellAndBuyRouter.post("/sellProduct", async(req, res) => {
|
| const {productName,costPrice} = req.body;
|
| if(productName.length<4){
|
| res.status(400).send({
|
| error: "product name should have minimun of four characters"
|
| });
|
| }else if(costPrice<0){
|
| res.status(400).send({
|
| error: "cost price value cannot be zero or negative value"
|
| });
|
|
|
| }else {
|
| const sellBuy = new SellBuy(req.body);
|
| await sellBuy.save();
|
| res.status(201).send({
|
| message:"Product Added"
|
| });
|
| }
|
| })
|
|
|
|
|
| sellAndBuyRouter.patch("/sellProduct/:id", async (req, res) => {
|
| const{soldPrice} = req.body
|
| if(soldPrice <=0){
|
| res.status(400).send({
|
| error:"sold price value cannot be zero or negative value"
|
| });
|
| }else{
|
| const sellBuy = await SellBuy.findByIdAndUpdate(req.params.id, req.body);
|
| res.status(200).send({
|
| mesage:"Updated successfully"
|
| });
|
| }
|
|
|
| });
|
|
|
| sellAndBuyRouter.delete("/sellProduct/:id", async (req, res) =>{
|
| async(req,res)=>{
|
| try{
|
| const data = await SellBuy.findByIdAndDelete(req.params.id)
|
| res.status(200).send({
|
| message: "deleted sucessfully"
|
| });
|
| }catch(error){
|
| res.status(400).send();
|
| }
|
| }
|
| })
|
|
|
| module.exports = sellAndBuyRouter;
|