Skip to content

categories requests has been done except product category #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 33 additions & 8 deletions src/controllers/product.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ProductController {
}

/**
* get all products by caetgory
* get all products by category
*
* @static
* @param {object} req express request object
Expand Down Expand Up @@ -215,7 +215,12 @@ class ProductController {
*/
static async getAllCategories(req, res, next) {
// Implement code to get all categories here
return res.status(200).json({ message: 'this works' });
try {
const categories = await Category.findAll();
return res.status(200).json(categories);
} catch (error) {
return next(error);
}
}

/**
Expand All @@ -225,9 +230,21 @@ class ProductController {
* @param {*} next
*/
static async getSingleCategory(req, res, next) {
const { category_id } = req.params; // eslint-disable-line
// implement code to get a single category here
return res.status(200).json({ message: 'this works' });
const { category_id } = req.params; // eslint-disable-line
try {
const category = await Category.findByPk(category_id);
if (category) {
return res.status(200).json(category);
}
return res.status(404).json({
error: {
status: 404,
message: `Category with id ${category_id} does not exist`, // eslint-disable-line
}
});
} catch (error) {
return next(error);
}
}

/**
Expand All @@ -237,9 +254,17 @@ class ProductController {
* @param {*} next
*/
static async getDepartmentCategories(req, res, next) {
const { department_id } = req.params; // eslint-disable-line
// implement code to get categories in a department here
return res.status(200).json({ message: 'this works' });
try {
const { department_id } = req.params; // eslint-disable-line
const categories = await Category.findAll({
where: {
department_id,
},
});
return res.status(200).json(categories);
} catch (error) {
return next(error);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/routes/api/product.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ router.get('/products/inDepartment/:department_id', ProductController.getProduct
router.get('/departments', ProductController.getAllDepartments);
router.get('/departments/:department_id', ProductController.getDepartment);
router.get('/categories', ProductController.getAllCategories);
router.get('/categories/:category_id');
router.get('/categories/:category_id', ProductController.getSingleCategory);
router.get('/categories/inDepartment/:department_id', ProductController.getDepartmentCategories);
router.get('/categories/inProduct/:product_id', ProductController.getProductsByCategory);

export default router;