added endpoints for object deletion

This commit is contained in:
leca 2024-11-11 01:11:24 +03:00
parent 77d95824f3
commit c39e8c9790
9 changed files with 69 additions and 2 deletions

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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();

View File

@ -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();