Skip to content

Instantly share code, notes, and snippets.

@Kamilczak020
Created October 11, 2017 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kamilczak020/cdf3da925a955010b07ca690e3a7329a to your computer and use it in GitHub Desktop.
Save Kamilczak020/cdf3da925a955010b07ca690e3a7329a to your computer and use it in GitHub Desktop.
export function getMessagesQuery(roomId: number, userId: number, responseId: number, timestamp: moment.Moment) {
let query = 'SELECT * FROM messages';
let queryObjects = [];
if (isNaN(roomId) && isNaN(userId) && isNaN(responseId) && !timestamp.isValid()) {
return query;
}
if (roomId !== undefined) {
queryObjects.push(`room_id = ${roomId}`);
}
if (userId !== undefined) {
queryObjects.push(`message_id = ${userId}`);
}
if (responseId !== undefined) {
queryObjects.push(`response_id = ${responseId}`);
}
if (timestamp.isValid()) {
queryObjects.push(`timestamp = '${timestamp.format('YYYY-MM-DD')}'`);
}
query += ' WHERE ';
query += queryObjects.join(' AND ');
return query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment