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

Commit

Permalink
Filter and order in same method. Webapp requesst also added
Browse files Browse the repository at this point in the history
  • Loading branch information
pablo268la committed Apr 6, 2022
1 parent 359276c commit d65b9ec
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 49 deletions.
29 changes: 14 additions & 15 deletions restapi/products/ProductController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
};
4 changes: 1 addition & 3 deletions restapi/products/ProductRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion restapi/products/product.http
Original file line number Diff line number Diff line change
Expand Up @@ -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
82 changes: 52 additions & 30 deletions webapp/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
let response = await fetch(apiEndPoint + "/users", {
method: "POST",
Expand Down Expand Up @@ -44,6 +46,8 @@ export async function getUser(userEmail: String): Promise<User> {
return response.json();
}

// PRODUCTS

export async function getProducts(): Promise<Product[]> {
let response = await fetch(apiEndPoint + "/products/");
return response.json();
Expand All @@ -56,35 +60,6 @@ export async function getProduct(productCode: string): Promise<Product> {
return response.json();
}

export async function getPlaces(
x: number,
y: number,
radiusMeters: number,
maxResults: number
): Promise<any> {
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",
Expand Down Expand Up @@ -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<Product[]> {
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",
Expand Down Expand Up @@ -182,6 +171,8 @@ export async function getOrders(): Promise<Order[]> {
return response.json();
}

// REVIEWS

export async function getReviewsByCode(code: string): Promise<Review[]> {
let response = await fetch(apiEndPoint + "/reviews/listByCode/" + code);
return response.json();
Expand Down Expand Up @@ -215,3 +206,34 @@ export async function addReview(review: Review): Promise<boolean> {
return true;
} else return false;
}

// PLACES

export async function getPlaces(
x: number,
y: number,
radiusMeters: number,
maxResults: number
): Promise<any> {
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;
}

0 comments on commit d65b9ec

Please sign in to comment.