Using One Request For Couple URL Endpoints - Angular
I will explain, I am trying to use getProducts (backend controller) for 3 pages in the website who uses resolver for displaying data, here are the options - http://localhost:4200/p
Solution 1:
You posted alot of code, for the next time please provide the minimal amount of code to reproduce the problem.
I changed the controller to check for gender and/or category and then use these values in the ProductDB.find() function.
UPDATED CONTROLLER
const getProducts = async (
req: IgetProductsRequest,
res: IgetProductsResponse
) => {
ServerGlobal.getInstance().logger.info(
`<getProducts>: Start processing request filtered by \
category ${req.params.category} and gender ${req.params.gender}`
);
if (!req.params.category && !req.params.gender) {
res.status(400).send({
success: false,
message: "Please provide a category and/or gender",
});
return;
}
const dbQueryParams = {};
if (req.params.category) {
if (!ServerGlobal.getInstance().isValidCategoryValue(+req.params.category!)) {
ServerGlobal.getInstance().logger.error(`<getProducts>: Failed to get products because \
of invalid category filtered by category ${req.params.category} \
and gender ${req.params.gender}`);
res.status(400).send({
success: false,
message: "Please provide valid category",
});
return;
}
dbQueryParams.category = +req.params.category!
}
if (req.params.gender) {
if (!ServerGlobal.getInstance().isValidGenderValue(+req.params.gender!) {
ServerGlobal.getInstance().logger.error(`<getProducts>: Failed to get products because \
of invalid gender filtered by category ${req.params.category} \
and gender ${req.params.gender}`);
res.status(400).send({
success: false,
message: "Please provide valid gender",
});
return;
}
dbQueryParams.gender = +req.params.gender!
}
try {
const products = await ProductDB.find(dbQueryParams);
ServerGlobal.getInstance().logger.info(
`<getProducts>: Successfully got the products filtered by \
category ${req.params.category} and gender ${req.params.gender}`
);
res.status(200).send({
success: true,
message: "Successfully retrieved products",
data: products.map((product) => ({
id: product.id as string,
category: {
value: product.category,
label: ServerGlobal.getInstance().getCategoryLabel(product.category)!,
},
gender: {
value: product.gender,
label: ServerGlobal.getInstance().getGenderLabel(product.gender)!,
},
title: product.title,
description: product.description,
price: product.price,
imageFilename: product.imageFilename,
})),
});
return;
} catch (e) {
ServerGlobal.getInstance().logger.error(
`<getProducts>: Failed to get products filtered by \
category ${req.params.category} and gender ${req.params.gender} because of server error: ${e}`
);
res.status(500).send({
success: false,
message: "Server error",
});
return;
}
};
Post a Comment for "Using One Request For Couple URL Endpoints - Angular"