Skip to content

Commit

Permalink
narrow search by author (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecM33 authored Mar 24, 2024
1 parent 4acb7a0 commit 8498fe5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
6 changes: 5 additions & 1 deletion commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ module.exports = {
.setDescription('Search for quotes.')
.addStringOption(option =>
option.setName('search_string')
.setDescription('a keyword or keyphrase by which to search for quotes')
.setDescription('a keyword or keyphrase')
.setRequired(true))
.addStringOption(option =>
option.setName('author')
.setDescription('narrow your search by a given author')
.setRequired(false))
.addBooleanOption(option =>
option.setName('include_identifier')
.setDescription('include the unique identifier for each resulting quote, which can be used to delete that quote.')
Expand Down
20 changes: 19 additions & 1 deletion database/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ module.exports = {

fetchUniqueAuthors: (guildId) => {
return query({
text: `SELECT DISTINCT author FROM quotes WHERE guild_id = $1;`,
text: `SELECT DISTINCT author FROM quotes WHERE guild_id = $1 ORDER BY author;`,
values: [
guildId
]
Expand Down Expand Up @@ -88,6 +88,24 @@ module.exports = {
});
},

fetchQuotesBySearchStringAndAuthor: (searchString, guildId, author) => {
return query({
text: `SELECT
id,
PGP_SYM_DECRYPT(quotation::bytea, $1) as quotation,
author,
said_at FROM quotes
WHERE author = $2 AND LOWER(PGP_SYM_DECRYPT(quotation::bytea, $3)) LIKE LOWER($4) AND guild_id = $5;`,
values: [
encryptionKey,
author,
encryptionKey,
'%' + searchString + '%',
guildId
]
});
},

fetchQuotesBySearchString: (searchString, guildId) => {
return query({
text: `SELECT
Expand Down
15 changes: 10 additions & 5 deletions modules/interaction-handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,16 @@ module.exports = {
await interaction.deferReply();
const searchString = interaction.options.getString('search_string')?.trim();
const includeIdentifier = interaction.options.getBoolean('include_identifier');
const searchResults = await queries.fetchQuotesBySearchString(searchString, interaction.guildId).catch(async (e) => {
console.error(e);
await interaction.followUp({ content: responseMessages.GENERIC_INTERACTION_ERROR });
});

const author = interaction.options.getString('author')?.trim();
const searchResults = author && author.length > 0
? await queries.fetchQuotesBySearchStringAndAuthor(searchString, interaction.guildId, author).catch(async (e) => {
console.error(e);
await interaction.followUp({ content: responseMessages.GENERIC_INTERACTION_ERROR });
})
: await queries.fetchQuotesBySearchString(searchString, interaction.guildId).catch(async (e) => {
console.error(e);
await interaction.followUp({ content: responseMessages.GENERIC_INTERACTION_ERROR });
});
let reply = '';
if (searchResults.length === 0) {
reply += responseMessages.EMPTY_QUERY;
Expand Down

0 comments on commit 8498fe5

Please sign in to comment.