feat: Generalize public endpoints

This commit is contained in:
2023-10-09 13:29:00 -06:00
parent 4e6dbdcedb
commit 74ad7c5e3f
8 changed files with 98 additions and 62 deletions

View File

@@ -1,6 +1,6 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/cities.model.js` );

View File

@@ -1,28 +1,33 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/countries.model.js` );
const generic = new GenericHandler( Model, "country_name" );
const getList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page , elements, Model );
const retVal = await generic.getList(page , elements);
res.send( retVal );
};
const findList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "country_name" : { $regex: re, $options: 'i' }};
}
const findString = req.query.regex || null;
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page, elements, Model, filter );
let retVal;
if( findString ){
retVal = await generic.findList( findString, page, elements );
}else{
retVal = await generic.getList(page , elements);
}
res.send( retVal );
};
const getById = async(req, res) => {
const retVal = await Model.findById( req.params.id );
const id=req.params.id;
const retVal = await generic.getById( id );
res.send( retVal );
};
module.exports = { getList, findList, getById };
module.exports = { getList , findList , getById };

View File

@@ -1,28 +1,33 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/meta-data.model.js` );
const generic = new GenericHandler( Model, "meta_value" );
const getList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page , elements, Model );
const retVal = await generic.getList(page , elements);
res.send( retVal );
};
const findList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "meta_value" : { $regex: re, $options: 'i' }};
}
const findString = req.query.regex || null;
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page, elements, Model, filter );
let retVal;
if( findString ){
retVal = await generic.findList( findString, page, elements );
}else{
retVal = await generic.getList(page , elements);
}
res.send( retVal );
};
const getById = async(req, res) => {
const retVal = await Model.findById( req.params.id );
const id=req.params.id;
const retVal = await generic.getById( id );
res.send( retVal );
};
module.exports = { getList, findList, getById };
module.exports = { getList , findList , getById };

View File

@@ -1,28 +1,33 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/meta-groups.model.js` );
const generic = new GenericHandler( Model, "group_label" );
const getList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page , elements, Model );
const retVal = await generic.getList(page , elements);
res.send( retVal );
};
const findList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "group_label" : { $regex: re, $options: 'i' }};
}
const findString = req.query.regex || null;
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page, elements, Model, filter );
let retVal;
if( findString ){
retVal = await generic.findList( findString, page, elements );
}else{
retVal = await generic.getList(page , elements);
}
res.send( retVal );
};
const getById = async(req, res) => {
const retVal = await Model.findById( req.params.id );
const id=req.params.id;
const retVal = await generic.getById( id );
res.send( retVal );
};
module.exports = { getList, findList, getById };
module.exports = { getList , findList , getById };

View File

@@ -1,28 +1,33 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/product-categories.model.js` );
const generic = new GenericHandler( Model, "name" );
const getList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page , elements, Model );
const retVal = await generic.getList(page , elements);
res.send( retVal );
};
const findList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "name" : { $regex: re, $options: 'i' }};
}
const findString = req.query.regex || null;
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page, elements, Model, filter );
let retVal;
if( findString ){
retVal = await generic.findList( findString, page, elements );
}else{
retVal = await generic.getList(page , elements);
}
res.send( retVal );
};
const getById = async(req, res) => {
const retVal = await Model.findById( req.params.id );
const id=req.params.id;
const retVal = await generic.getById( id );
res.send( retVal );
};
module.exports = { getList, findList, getById };
module.exports = { getList , findList , getById };

View File

@@ -1,28 +1,33 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { getPagination } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/products.model.js` );
const generic = new GenericHandler( Model, "name" );
const getList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page , elements, Model );
const retVal = await generic.getList(page , elements);
res.send( retVal );
};
const findList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "name" : { $regex: re, $options: 'i' }};
}
const findString = req.query.regex || null;
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page, elements, Model, filter );
let retVal;
if( findString ){
retVal = await generic.findList( findString, page, elements );
}else{
retVal = await generic.getList(page , elements);
}
res.send( retVal );
};
const getById = async(req, res) => {
const retVal = await Model.findById( req.params.id );
const id=req.params.id;
const retVal = await generic.getById( id );
res.send( retVal );
};
module.exports = { getList, findList, getById };
module.exports = { getList , findList , getById };

View File

@@ -1,28 +1,33 @@
"use strict";
const { ROOT_PATH, LIB_PATH, MODELS_PATH, HANDLERS_PATH } = process.env;
const { getPagination , getPage } = require( `${ROOT_PATH}/${LIB_PATH}/Misc.js` );
const { GenericHandler } = require( `${ROOT_PATH}/${HANDLERS_PATH}/Generic.handler.js` );
const Model = require( `${ROOT_PATH}/${MODELS_PATH}/states.model.js` );
const generic = new GenericHandler( Model, "state_name" );
const getList = async(req, res) => {
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page , elements, Model );
const retVal = await generic.getList(page , elements);
res.send( retVal );
};
const findList = async(req, res) => {
let filter=null;
if( req.query.regex ){
const re = new RegExp( req.query.regex );
filter = { "state_name" : { $regex: re, $options: 'i' }};
}
const findString = req.query.regex || null;
const { page , elements } = getPagination( req.query );
const retVal = await getPage( page, elements, Model, filter );
let retVal;
if( findString ){
retVal = await generic.findList( findString, page, elements );
}else{
retVal = await generic.getList(page , elements);
}
res.send( retVal );
};
const getById = async(req, res) => {
const retVal = await Model.findById( req.params.id );
const id=req.params.id;
const retVal = await generic.getById( id );
res.send( retVal );
};
module.exports = { getList, findList, getById };
module.exports = { getList , findList , getById };

View File

@@ -38,8 +38,14 @@ class GenericHandler{
}
async getList( page, elements, filter=null, projection=null ){
const { query } = await getPageQuery( page , elements, this.Model, filter, projection );
return await this.populateQuey( query );
const { query , total, skip } = await getPageQuery( page , elements, this.Model, filter, projection );
const list = await this.populateQuey( query );
return {
total : total,
limit : elements,
skip : skip,
data : list
};
}
async findList( find_string, page, elements, projection=null ){