Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Adding filter and order requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo268la committed Apr 6, 2022
1 parent 924a01c commit 6d14dc5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
29 changes: 29 additions & 0 deletions restapi/products/ProductController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,32 @@ export const updateProduct: RequestHandler = async (req, res) => {
res.status(403).json();
}
};

export const filterBy: RequestHandler = async (req, res) => {
const category = req.params.category;
let products;

if (
category !== "Clothes" &&
category !== "Decoration" &&
category !== "Electronics" &&
category !== "Miscellaneous"
) {
products = await productModel.find();
} else {
products = await productModel.find({ category: category });
}

return res.json(products);
};

export const orderBy: RequestHandler = async (req, res) => {
let mode = req.params.mode;
let products;
if (mode !== "asc" && mode !== "desc") {
products = await productModel.find();
} else {
products = await productModel.find().sort({ price: mode });
}
return res.json(products);
};
8 changes: 6 additions & 2 deletions restapi/products/ProductRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import express, { Request, Response, Router } from "express";
import express, { Router } from "express";
import multer from "../utils/multer";
import * as ProdctController from "./ProductController";

import multer from "../utils/multer";

const api: Router = express.Router();

api.get("/products", ProdctController.getProducts);

api.get("/products/findByCode/:code", ProdctController.getProduct);

api.get('/products/filter/:category', ProdctController.filterBy)

api.get('/products/order/:mode', ProdctController.orderBy)

api.post("/products", multer.single("image"), ProdctController.createProduct);

api.post("/products/delete/:code", ProdctController.deleteProduct);
Expand Down
6 changes: 6 additions & 0 deletions restapi/products/product.http
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ Content-Type: application/json
{
"category": "Miscellaneous"
}

###
GET {{url}}/products/order/desc

###
GET {{url}}/products/filter/Electronics

0 comments on commit 6d14dc5

Please sign in to comment.