Simha Chalam Simha Chalam - 1 month ago 7
Node.js Question

how to avoid multiple conditions in switch in a single object?

i getting one object sending from client side like this

var condition={"bedrooms":"1,2,3,4","Inhibition":"1,6","possession":"3","id":"8",toilets:"1,2",...,}


so it must be the same keys and only in single object but length may be 1/2/3/4/..
like this example:

var condition={"bedrooms":"1,2,3,4","Inhibition":"1,6"};
var condition={"bedrooms":"1,2,3,4"};
var condition={"Inhibition":"1,6"};
var condition={"id":"1,6"};
var condition={"toilets":"1,6","possession":"3"};
var condition={"possession":"3"};


like this i have 10 keys
so based on that i am getting almost 100 scenarios for different scenario i have to return the different object.so how can i handle all these scenarios by using switch and if else is not correct.

i tried like this

enter image description here
my code is :

function comparison(condition,requestId,dbId){
var obj={};
var andCondition=[];
var subCondition={};




switch(true) {
case (condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"));

obj['$and']=andCondition;

return obj;

};
break;
case (condition.hasOwnProperty("Inhibition") && condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"));

obj['$and']=andCondition;

return obj;
}
break;

case (condition.hasOwnProperty("Inhibition") && condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange));

obj['$and']=andCondition;

return obj;
}
break;




case (condition.hasOwnProperty("Inhibition") && condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("budgetPrice") && condition.hasOwnProperty("possession")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange),Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"));

obj['$and']=andCondition;

return obj;
}
break;



case (condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("budgetPrice")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange));

obj['$and']=andCondition;

return obj;
}
break;


case (condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("possession")): {

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"))
obj['$and']=andCondition;

return obj;
}
break;


case (condition.hasOwnProperty("budgetPrice") && !condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("possession")): {
var budgetPrice = condition.budgetPrice.split(",");
var minRange = budgetPrice[0];
var maxRange = budgetPrice[1];

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_per_sqft` * `hp_unit_details`.`property_size` BETWEEN " + minRange + " AND " + maxRange))
obj['$and']=andCondition;

return obj;
}
break;
case (condition.hasOwnProperty("possession") && !condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("budgetPrice")): {
subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"))
obj['$and']=andCondition;
return obj;
}

break;

case (condition.hasOwnProperty("possession") && condition.hasOwnProperty("bedrooms") && !condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("budgetPrice")): {

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"))
obj['$and']=andCondition;
return obj;
}
break;

case (condition.hasOwnProperty("possession") && condition.hasOwnProperty("bedrooms") && condition.hasOwnProperty("Inhibition") && !condition.hasOwnProperty("budgetPrice")): {

subCondition[dbId]=requestId;
andCondition.push(subCondition,Sequelize.literal("`hp_property`.`property_completion_date` between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_unit_details`.`hp_unit_type_id` IN (" + condition.bedrooms + ")"),Sequelize.literal("`hp_property`.`hp_property_inhibition_status_id` IN (" + condition.Inhibition + ")"))
obj['$and']=andCondition;
return obj;
}
break;



default:{
obj[dbId] =requestId
return obj;
}




}

}

Answer

It looks like you could just easily treat each condition separately

something like

var andCondition = [];
subCondition[dbId]=requestId;
andCondition.push(subCondition)
if(condition.hasOwnProperty("Inhibition")) {
    andCondition.push(Sequelize.literal("`hp_property`.`hp_property_    inhibition_status_id` IN (" + condition.Inhibition + ")"));
};
if(condition.hasOwnProperty("bedrooms")) {           
    andCondition.push(Sequelize.literal("`hp_unit_details`.`hp_unit_    type_id` IN (" + condition.bedrooms + ")");
}
if(condition.hasOwnProperty("budgetPrice")) {
    var budgetPrice = condition.budgetPrice.split(",");
    var minRange = budgetPrice[0];
    var maxRange = budgetPrice[1];       
    andCondition.push(Sequelize.literal("`hp_unit_details.hp_unit_prices`.`price_    per_sqft` * `hp_unit_details`.`property_size`  BETWEEN " + minRange + " AND " + maxRange));
}
if(condition.hasOwnProperty("possession")): {
    andCondition.push(Sequelize.literal("`hp_property`.`property_completion_date`     between now() and DATE_ADD(now(), INTERVAL "+condition.possession+" MONTH)"));
}
obj['$and']=andCondition;

return obj;
Comments