From c39e8c979053891702b4d6d8f698bff97ed0d194 Mon Sep 17 00:00:00 2001 From: leca Date: Mon, 11 Nov 2024 01:11:24 +0300 Subject: [PATCH] added endpoints for object deletion --- src/controllers/abstractproduct.js | 10 +++++++++- src/controllers/category.js | 10 ++++++++++ src/controllers/product.js | 10 ++++++++++ src/routers/abstractproduct.js | 1 + src/routers/category.js | 3 ++- src/routers/product.js | 1 + src/services/abstractproduct.js | 14 ++++++++++++++ src/services/category.js | 13 +++++++++++++ src/services/product.js | 9 +++++++++ 9 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/controllers/abstractproduct.js b/src/controllers/abstractproduct.js index d34a520..cfd6161 100644 --- a/src/controllers/abstractproduct.js +++ b/src/controllers/abstractproduct.js @@ -1,4 +1,5 @@ import AbstractProductService from '../services/abstractproduct.js'; +import ProductService from '../services/product.js'; import fs from 'fs'; import path from 'path'; import customError from '../response/customError.js'; @@ -69,7 +70,7 @@ class AbstractProductController { } async getImage(req, res) { - let { localId, groupId } = req.params + 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`); @@ -79,6 +80,13 @@ class AbstractProductController { return res.status(200).send(image) } + async delete(req, res) { + let { localId, groupId } = req.params; + + await AbstractProductService.delete(groupId, localId) + + return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)) + } }; export default new AbstractProductController(); diff --git a/src/controllers/category.js b/src/controllers/category.js index f6e8c8b..5f51e44 100644 --- a/src/controllers/category.js +++ b/src/controllers/category.js @@ -1,4 +1,6 @@ import CategoryService from "../services/category.js"; +import AbstractProductService from "../services/abstractproduct.js"; +import ProductService from "../services/product.js"; import translate from "../utils/translate.js"; import responseCodes from "../response/responseCodes.js"; @@ -28,6 +30,14 @@ class CategoryController { return res.status(200).send(result) } + + async delete(req, res) { + const { groupId, localId } = req.params; + + await CategoryService.delete(groupId, localId); + + return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)) + } }; export default new CategoryController(); \ No newline at end of file diff --git a/src/controllers/product.js b/src/controllers/product.js index e7c7852..2f4b257 100644 --- a/src/controllers/product.js +++ b/src/controllers/product.js @@ -35,6 +35,16 @@ class AbstractProductController { return res.status(200).send(result) } + + async delete(req, res) { + let { groupId, localId } = req.params; + + let id = (await ProductService.getByLocalId(groupId, localId)).id + + await ProductService.delete(id) + + return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); + } }; export default new AbstractProductController(); \ No newline at end of file diff --git a/src/routers/abstractproduct.js b/src/routers/abstractproduct.js index f20a6c1..630f309 100644 --- a/src/routers/abstractproduct.js +++ b/src/routers/abstractproduct.js @@ -16,5 +16,6 @@ AbstractProductRouter.post('/create', upload.single("file"), tryHandler(auth.aut 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)); +AbstractProductRouter.delete('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(AbstractProductController.delete)); export default AbstractProductRouter; \ No newline at end of file diff --git a/src/routers/category.js b/src/routers/category.js index 388c83b..03b0aa4 100644 --- a/src/routers/category.js +++ b/src/routers/category.js @@ -8,6 +8,7 @@ const CategoryRouter = new Router(); 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)) +CategoryRouter.get('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(CategoryController.getByLocalId)); +CategoryRouter.delete('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(auth.userIsInGroup), tryHandler(existance.localIdExists), tryHandler(CategoryController.delete)); export default CategoryRouter; \ No newline at end of file diff --git a/src/routers/product.js b/src/routers/product.js index 4811773..47a9a4b 100644 --- a/src/routers/product.js +++ b/src/routers/product.js @@ -9,5 +9,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)) +ProductRouter.delete('/:groupId/:localId', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(existance.localIdExists), tryHandler(auth.userIsInGroup), tryHandler(ProductController.delete)) export default ProductRouter; \ No newline at end of file diff --git a/src/services/abstractproduct.js b/src/services/abstractproduct.js index 0c0b48e..71b7538 100644 --- a/src/services/abstractproduct.js +++ b/src/services/abstractproduct.js @@ -1,3 +1,4 @@ +import ProductService from './product.js'; import db from '../db.js'; import errorHandler from '../utils/pgerrorhandler.js'; import responses from '../response/responseCodes.js'; @@ -70,6 +71,19 @@ class AbstractProductService { if (result.rowCount == 0) throw new customError(`Abstract product not found`, responses.responses.abstractproducts.not_found) return result.rows[0] } + + async getAllFromCategory(groupId, categoryId) { + let result = (await db.query("SELECT * FROM abstract_products WHERE group_id = $1 AND category = $2", [groupId, categoryId])); + return result.rows + } + + async delete(groupId, localId) { + let productsOfAbstractProduct = await ProductService.getByAbstractProductID(groupId, localId); + productsOfAbstractProduct.forEach(async (product) => { + await ProductService.delete(product.id) + }); + await db.query("DELETE FROM abstract_products WHERE group_id = $1 AND local_id = $2", [groupId, localId]) + } }; export default new AbstractProductService(); \ No newline at end of file diff --git a/src/services/category.js b/src/services/category.js index e14f00c..882b418 100644 --- a/src/services/category.js +++ b/src/services/category.js @@ -1,6 +1,9 @@ +import ProductService from "./product.js"; + import db from '../db.js'; import customError from '../response/customError.js'; import responseCodes from '../response/responseCodes.js'; +import AbstractProductService from "./abstractproduct.js"; class CategoryService { async create(groupId, categoryId, name) { @@ -27,6 +30,16 @@ class CategoryService { let result = (await db.query("SELECT group_id, local_id, name FROM categories WHERE group_id = $1", [groupId])).rows return result } + + + async delete(groupId, localId) { + let abstractProductInCategory = await AbstractProductService.getAllFromCategory(groupId, localId); + abstractProductInCategory.forEach(async (abstractProduct) => { + await AbstractProductService.delete(groupId, abstractProduct.local_id) + }); + + await db.query("DELETE FROM categories WHERE group_id = $1 AND local_id = $2", [groupId, localId]); + } } export default new CategoryService(); \ No newline at end of file diff --git a/src/services/product.js b/src/services/product.js index e7d0e3f..961defd 100644 --- a/src/services/product.js +++ b/src/services/product.js @@ -57,6 +57,15 @@ class ProductService { if (result.rowCount == 0) throw new customError(`getByLocalId product not found`, responseCodes.responses.products.not_found); return result.rows[0]; } + + async getByAbstractProductID(groupId, abstractProductID) { + let result = (await db.query("SELECT * FROM products WHERE group_id = $1 AND abstract_product_id = $2", [groupId, abstractProductID])) + return result.rows + } + + async delete(ID) { + await db.query("DELETE FROM products WHERE id = $1", [ID]) + } }; export default new ProductService();