From 03d8d48f1135740df7ad9326d0b071fb859b2911 Mon Sep 17 00:00:00 2001 From: Fig Yogurt Date: Tue, 16 Feb 2021 19:11:29 -0500 Subject: [PATCH] feat(analyze): added route and functionality to allow users to search for a query and provide sentences that match it (with data to let the users analyze it) --- server/controllers/analyze.controller.js | 24 ++++++++++++++++++++++-- server/routes/analyze.route.js | 10 +++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/server/controllers/analyze.controller.js b/server/controllers/analyze.controller.js index 5620e06..71bc035 100644 --- a/server/controllers/analyze.controller.js +++ b/server/controllers/analyze.controller.js @@ -1,5 +1,5 @@ const { Op } = require("sequelize"); -const { EntryTone } = require("../db"); +const { EntryTone, SentenceTone } = require("../db"); module.exports.searchEntries = async (req, res) => { try { @@ -7,7 +7,6 @@ module.exports.searchEntries = async (req, res) => { return res.json({ message: "Please type a search query to search!" }); } const { searchQuery } = req.body; - console.log(searchQuery); const searchList = await EntryTone.findAndCountAll({ where: { userId: req.userId, @@ -22,3 +21,24 @@ module.exports.searchEntries = async (req, res) => { res.json({ message: "Error occured when searching your query!", error }); } }; + +module.exports.searchSentences = async (req, res) => { + try { + if (!req.body.searchQuery) { + return res.json({ message: "Please type a search query to search!" }); + } + const { searchQuery } = req.body; + const searchList = await SentenceTone.findAndCountAll({ + where: { + userId: req.userId, + message: { [Op.iLike]: `%${searchQuery}%` }, + }, + }); + return res.json({ + message: `${searchList.count} entries were found that matched your query!`, + data: searchList.rows, + }); + } catch (error) { + res.json({ message: "Error occured when searching your query!", error }); + } +}; diff --git a/server/routes/analyze.route.js b/server/routes/analyze.route.js index 0a053ed..dbf6e02 100644 --- a/server/routes/analyze.route.js +++ b/server/routes/analyze.route.js @@ -1,4 +1,7 @@ -const { searchEntries } = require("../controllers/analyze.controller"); +const { + searchEntries, + searchSentences, +} = require("../controllers/analyze.controller"); const { authJwt } = require("../middleware"); module.exports = function (app) { @@ -11,4 +14,9 @@ module.exports = function (app) { }); app.get("/api/analyze/search/entries/", [authJwt.verifyToken], searchEntries); + app.get( + "/api/analyze/search/sentences/", + [authJwt.verifyToken], + searchSentences + ); };