I'm trying to build a dynamic string but I'm facing a problem, so I kindly ask help to the community.
A have a string to build a rest call filter, but I'm lost with the and's
This query only works if only one filter condition is provided, but I have to provide as AND
var query;
var queryDefault = "IsLastForLevelAndParticipant eq 1";
this.state.participantFirstName
? query = "substringof('" this.state.participantFirstName "',Participant/FirstName)"
: queryDefault
this.state.participantLastName
? query = "substringof('" this.state.participantLastName "',Participant/LastName)"
: queryDefault
So let´s see.
If I start building this and only one filter is provided I'll have a plus and
var query;
var queryDefault = "IsLastForLevelAndParticipant eq 1";
this.state.participantFirstName
? query = "substringof('" this.state.participantFirstName "',Participant/FirstName) and "
: queryDefault
this.state.participantLastName
? query = "substringof('" this.state.participantLastName "',Participant/LastName)"
: queryDefault
query = " and " queryDefault
I have 12 filters and I must know how many have values in order to provide the and
clause
This is my state
//Filters Certificates
startEmission: string;
endEmission: string;
startValidity: string;
endValidity: string;
participantFirstName: string;
participantLastName: string;
paticipantCertNumber: string;
selectYesNo: string;
selectLevel: string;
Any help is a bonus.
CodePudding user response:
Add all possible filters to an array, filter the falsy values and finally join all items with " and "
.
var query = [
queryDefault,
this.state.participantFirstName && "substringof('" this.state.participantFirstName "',Participant/FirstName)"
this.state.participantLastName && "substringof('" this.state.participantLastName "',Participant/LastName)"
]
.filter(item => !!item)
.join(" and ");
BTW. are you using OData? the structure of the filter looks familiar. If you do, I would recommend you to use this library: https://github.com/techniq/odata-query#readme
CodePudding user response:
Since I'm using datetime
also, here it is the final solution
var queryDefault = "IsLastForLevelAndParticipant eq 1";
var query = [
queryDefault,
this.state.startEmission && "(Date1 ge datetime'" moment.utc(this.state.startEmission).toISOString() "') and (Date1 le datetime'"
moment.utc(this.state.endEmission).toISOString() "')",
this.state.startValidity && "(Validto ge datetime'" moment.utc(this.state.startValidity).toISOString() "') and (Validto le datetime'"
moment.utc(this.state.endValidity).toISOString() "')",
this.state.participantFirstName && "(substringof('" this.state.participantFirstName "',Participant/FirstName))",
this.state.participantLastName && "(substringof('" this.state.participantLastName "',Participant/LastName))",
this.state.selectYesNo && "IsPrinted eq " this.state.selectYesNo,
this.state.selectLevel && "Level/Title eq '" this.state.selectLevel "'"
]
.filter(filter => !! filter)
.join(" and ");
var q = JSON.stringify(query).substring(1).slice(0, -1);
console.log(JSON.stringify(query));