From 7a89ce2840c481f70f99b1425a0ab9021a6f91b2 Mon Sep 17 00:00:00 2001 From: leca Date: Thu, 7 Nov 2024 21:44:10 +0300 Subject: [PATCH] added endpoints for retreiving info about items --- src/controllers/abstractproduct.js | 20 ++++++++++++++++++++ src/controllers/category.js | 8 ++++++++ src/controllers/product.js | 8 ++++++++ src/routers/abstractproduct.js | 2 ++ src/routers/category.js | 5 +++-- src/routers/product.js | 1 + src/services/abstractproduct.js | 6 ++++++ src/services/product.js | 6 ++++++ 8 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/controllers/abstractproduct.js b/src/controllers/abstractproduct.js index 67bbc24..d34a520 100644 --- a/src/controllers/abstractproduct.js +++ b/src/controllers/abstractproduct.js @@ -59,6 +59,26 @@ class AbstractProductController { return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); } + + async getById(req, res) { + let { localId, groupId } = req.params + + let result = await AbstractProductService.getByLocalId(groupId, localId) + + return res.status(200).send(result) + } + + async getImage(req, res) { + let { localId, groupId } = req.params + + let imageFilename = (await AbstractProductService.getByLocalId(groupId, localId)).image_filename + let imagePath = path.join(path.resolve(path.dirname('')), `/uploads/${imageFilename}.png`); + + let image = fs.readFileSync(imagePath) + + return res.status(200).send(image) + } + }; export default new AbstractProductController(); diff --git a/src/controllers/category.js b/src/controllers/category.js index bfc02be..f6e8c8b 100644 --- a/src/controllers/category.js +++ b/src/controllers/category.js @@ -20,6 +20,14 @@ class CategoryController { return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); } + + async getByLocalId(req, res) { + const { groupId, localId } = req.params; + + let result = await CategoryService.getById(groupId, localId) + + return res.status(200).send(result) + } }; export default new CategoryController(); \ No newline at end of file diff --git a/src/controllers/product.js b/src/controllers/product.js index 2cbab92..e7c7852 100644 --- a/src/controllers/product.js +++ b/src/controllers/product.js @@ -27,6 +27,14 @@ class AbstractProductController { return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); } + + async getByLocalId(req, res) { + let { groupId, localId } = req.params; + + let result = await ProductService.getByLocalId(groupId, localId) + + return res.status(200).send(result) + } }; export default new AbstractProductController(); \ No newline at end of file diff --git a/src/routers/abstractproduct.js b/src/routers/abstractproduct.js index 1cf664c..40d5e38 100644 --- a/src/routers/abstractproduct.js +++ b/src/routers/abstractproduct.js @@ -14,5 +14,7 @@ const AbstractProductRouter = new Router(); AbstractProductRouter.post('/create', upload.single("file"), tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(AbstractProductController.create)); AbstractProductRouter.post('/update', upload.single("file"), tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(AbstractProductController.update)); +AbstractProductRouter.get('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(AbstractProductController.getById)); +AbstractProductRouter.get('/getimage/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(AbstractProductController.getImage)); export default AbstractProductRouter; \ No newline at end of file diff --git a/src/routers/category.js b/src/routers/category.js index 8a5da77..388c83b 100644 --- a/src/routers/category.js +++ b/src/routers/category.js @@ -6,7 +6,8 @@ import existance from '../middlewares/existance.js'; const CategoryRouter = new Router(); -CategoryRouter.post('/create', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(CategoryController.create)); -CategoryRouter.post('/update', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(existance.localIdExists), tryHandler(CategoryController.update)); +CategoryRouter.post('/create', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(CategoryController.create)); +CategoryRouter.post('/update', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(CategoryController.update)); +CategoryRouter.get('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(CategoryController.getByLocalId)) export default CategoryRouter; \ No newline at end of file diff --git a/src/routers/product.js b/src/routers/product.js index 42439c1..4811773 100644 --- a/src/routers/product.js +++ b/src/routers/product.js @@ -8,5 +8,6 @@ const ProductRouter = new Router(); ProductRouter.post('/create', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(ProductController.create)); ProductRouter.post('/update', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(existance.localIdExists), tryHandler(auth.userIsInGroup), tryHandler(ProductController.update)); +ProductRouter.get('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(existance.localIdExists), tryHandler(auth.userIsInGroup), tryHandler(ProductController.getByLocalId)) export default ProductRouter; \ No newline at end of file diff --git a/src/services/abstractproduct.js b/src/services/abstractproduct.js index e955e28..0c0b48e 100644 --- a/src/services/abstractproduct.js +++ b/src/services/abstractproduct.js @@ -64,6 +64,12 @@ class AbstractProductService { if (!result) return false; return true; } + + async getByLocalId(groupId, localId) { + let result = (await db.query("SELECT * FROM abstract_products WHERE group_id = $1 AND local_id = $2", [groupId, localId])) + if (result.rowCount == 0) throw new customError(`Abstract product not found`, responses.responses.abstractproducts.not_found) + return result.rows[0] + } }; export default new AbstractProductService(); \ No newline at end of file diff --git a/src/services/product.js b/src/services/product.js index ef8b924..6d7c7dd 100644 --- a/src/services/product.js +++ b/src/services/product.js @@ -51,6 +51,12 @@ class ProductService { if (!result) return false; return true; } + + async getByLocalId(groupId, localId) { + let result = (await db.query("SELECT * FROM products WHERE group_id = $1 AND local_id = $2", [groupId, localId])); + if (result.rowCount == 0) throw new customError(`getByLocalId product not found`, responseCodes.responses.products.not_found); + return responseCodes.rows[0]; + } }; export default new ProductService(); \ No newline at end of file