added endpoints for object deletion
This commit is contained in:
		@@ -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();
 | 
			
		||||
@@ -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();
 | 
			
		||||
@@ -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();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user