diff --git a/package-lock.json b/package-lock.json index 07305f5..421d9c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3982,7 +3982,8 @@ }, "ansi-regex": { "version": "2.1.1", - "bundled": true + "bundled": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -4000,11 +4001,13 @@ }, "balanced-match": { "version": "1.0.0", - "bundled": true + "bundled": true, + "optional": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, + "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -4017,15 +4020,18 @@ }, "code-point-at": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "concat-map": { "version": "0.0.1", - "bundled": true + "bundled": true, + "optional": true }, "console-control-strings": { "version": "1.1.0", - "bundled": true + "bundled": true, + "optional": true }, "core-util-is": { "version": "1.0.2", @@ -4128,7 +4134,8 @@ }, "inherits": { "version": "2.0.3", - "bundled": true + "bundled": true, + "optional": true }, "ini": { "version": "1.3.5", @@ -4138,6 +4145,7 @@ "is-fullwidth-code-point": { "version": "1.0.0", "bundled": true, + "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -4150,17 +4158,20 @@ "minimatch": { "version": "3.0.4", "bundled": true, + "optional": true, "requires": { "brace-expansion": "^1.1.7" } }, "minimist": { "version": "0.0.8", - "bundled": true + "bundled": true, + "optional": true }, "minipass": { "version": "2.3.5", "bundled": true, + "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -4177,6 +4188,7 @@ "mkdirp": { "version": "0.5.1", "bundled": true, + "optional": true, "requires": { "minimist": "0.0.8" } @@ -4249,7 +4261,8 @@ }, "number-is-nan": { "version": "1.0.1", - "bundled": true + "bundled": true, + "optional": true }, "object-assign": { "version": "4.1.1", @@ -4259,6 +4272,7 @@ "once": { "version": "1.4.0", "bundled": true, + "optional": true, "requires": { "wrappy": "1" } @@ -4334,7 +4348,8 @@ }, "safe-buffer": { "version": "5.1.2", - "bundled": true + "bundled": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -4364,6 +4379,7 @@ "string-width": { "version": "1.0.2", "bundled": true, + "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -4381,6 +4397,7 @@ "strip-ansi": { "version": "3.0.1", "bundled": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -4419,11 +4436,13 @@ }, "wrappy": { "version": "1.0.2", - "bundled": true + "bundled": true, + "optional": true }, "yallist": { "version": "3.0.3", - "bundled": true + "bundled": true, + "optional": true } } }, diff --git a/src/controllers/product.controller.js b/src/controllers/product.controller.js index ba000db..480345c 100755 --- a/src/controllers/product.controller.js +++ b/src/controllers/product.controller.js @@ -80,7 +80,7 @@ class ProductController { } /** - * get all products by caetgory + * get all products by category * * @static * @param {object} req express request object @@ -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); + } } /** @@ -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); + } } /** @@ -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); + } } } diff --git a/src/routes/api/product.route.js b/src/routes/api/product.route.js index c1436b1..bfd1787 100755 --- a/src/routes/api/product.route.js +++ b/src/routes/api/product.route.js @@ -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;