added endpoints for retreiving info about items

This commit is contained in:
leca 2024-11-07 21:44:10 +03:00
parent 5853023069
commit 7a89ce2840
8 changed files with 54 additions and 2 deletions

View File

@ -59,6 +59,26 @@ class AbstractProductController {
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); 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(); export default new AbstractProductController();

View File

@ -20,6 +20,14 @@ class CategoryController {
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); 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(); export default new CategoryController();

View File

@ -27,6 +27,14 @@ class AbstractProductController {
return res.status(200).send(translate(req.headers["accept-language"], responseCodes.responses.general.ok)); 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(); export default new AbstractProductController();

View File

@ -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('/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.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; export default AbstractProductRouter;

View File

@ -6,7 +6,8 @@ import existance from '../middlewares/existance.js';
const CategoryRouter = new Router(); const CategoryRouter = new Router();
CategoryRouter.post('/create', tryHandler(auth.authenticate), tryHandler(existance.groupExists), tryHandler(CategoryController.create)); 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(existance.localIdExists), tryHandler(CategoryController.update)); 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; export default CategoryRouter;

View File

@ -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('/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.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; export default ProductRouter;

View File

@ -64,6 +64,12 @@ class AbstractProductService {
if (!result) return false; if (!result) return false;
return true; 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(); export default new AbstractProductService();

View File

@ -51,6 +51,12 @@ class ProductService {
if (!result) return false; if (!result) return false;
return true; 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(); export default new ProductService();