diff --git a/restapi/products/ProductController.ts b/restapi/products/ProductController.ts index 3846e9bf..58e281bc 100644 --- a/restapi/products/ProductController.ts +++ b/restapi/products/ProductController.ts @@ -85,7 +85,8 @@ export const updateProduct: RequestHandler = async (req, res) => { } }; -export const filterBy: RequestHandler = async (req, res) => { +export const filterAndOrderBy: RequestHandler = async (req, res) => { + let mode = req.params.mode; const category = req.params.category; let products; @@ -95,21 +96,19 @@ export const filterBy: RequestHandler = async (req, res) => { 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(); + if (mode !== "asc" && mode !== "desc") { + products = await productModel.find(); + } else { + products = await productModel.find().sort({ price: mode }); + } } else { - products = await productModel.find().sort({ price: mode }); + if (mode !== "asc" && mode !== "desc") { + products = await productModel.find({ category: category }); + } else { + products = await productModel + .find({ category: category }) + .sort({ price: mode }); + } } return res.json(products); }; diff --git a/restapi/products/ProductRoutes.ts b/restapi/products/ProductRoutes.ts index 08244d1c..ccd6567b 100644 --- a/restapi/products/ProductRoutes.ts +++ b/restapi/products/ProductRoutes.ts @@ -9,9 +9,7 @@ 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.get('/products/filter&order/:category&:mode', ProdctController.filterAndOrderBy) api.post("/products", multer.single("image"), ProdctController.createProduct); diff --git a/restapi/products/product.http b/restapi/products/product.http index a233cd92..125dfe74 100644 --- a/restapi/products/product.http +++ b/restapi/products/product.http @@ -32,4 +32,4 @@ Content-Type: application/json GET {{url}}/products/order/desc ### -GET {{url}}/products/filter/Electronics +GET {{url}}/products/filter&order/Electronics&asc diff --git a/webapp/src/api/api.ts b/webapp/src/api/api.ts index 07d96049..97510551 100644 --- a/webapp/src/api/api.ts +++ b/webapp/src/api/api.ts @@ -1,7 +1,9 @@ -import { Order, Review, User, Product } from "../shared/shareddtypes"; +import { Order, Product, Review, User } from "../shared/shareddtypes"; const apiEndPoint = process.env.REACT_APP_API_URI || "http://localhost:5000"; +// USERS + export async function addUser(user: User): Promise { let response = await fetch(apiEndPoint + "/users", { method: "POST", @@ -44,6 +46,8 @@ export async function getUser(userEmail: String): Promise { return response.json(); } +// PRODUCTS + export async function getProducts(): Promise { let response = await fetch(apiEndPoint + "/products/"); return response.json(); @@ -56,35 +60,6 @@ export async function getProduct(productCode: string): Promise { return response.json(); } -export async function getPlaces( - x: number, - y: number, - radiusMeters: number, - maxResults: number -): Promise { - const url = - "https://api.geoapify.com/v2/places?categories=commercial&filter=circle:" + - x + - "," + - y + - "," + - radiusMeters + - "&bias=proximity:" + - x + - "," + - y + - "&limit=" + - maxResults + - "&apiKey=" + - process.env.REACT_APP_GEOAPIFY_KEY; - - let places; - await fetch(url, { - method: "GET", - }).then((response) => (places = response.json())); - return places; -} - export async function updateProduct(product: Product) { await fetch(apiEndPoint + "/products/update/" + product.code, { method: "POST", @@ -133,6 +108,20 @@ export async function deleteProduct(code: string) { }); } +// Mode must be desc or asc. If not default order +// Category must be Clothes, Decoration, Elecrtonics or Miscellaneous. If not all categories +export async function filterProductsByCategory( + category: string, + mode: string +): Promise { + let response = await fetch( + apiEndPoint + "/products/filter&order/" + category + "&" + mode + ); + return response.json(); +} + +// ORDERS + export async function createOrder(body: any) { await fetch(apiEndPoint + "/orders", { method: "POST", @@ -182,6 +171,8 @@ export async function getOrders(): Promise { return response.json(); } +// REVIEWS + export async function getReviewsByCode(code: string): Promise { let response = await fetch(apiEndPoint + "/reviews/listByCode/" + code); return response.json(); @@ -215,3 +206,34 @@ export async function addReview(review: Review): Promise { return true; } else return false; } + +// PLACES + +export async function getPlaces( + x: number, + y: number, + radiusMeters: number, + maxResults: number +): Promise { + const url = + "https://api.geoapify.com/v2/places?categories=commercial&filter=circle:" + + x + + "," + + y + + "," + + radiusMeters + + "&bias=proximity:" + + x + + "," + + y + + "&limit=" + + maxResults + + "&apiKey=" + + process.env.REACT_APP_GEOAPIFY_KEY; + + let places; + await fetch(url, { + method: "GET", + }).then((response) => (places = response.json())); + return places; +}