Skip to content

Commit

Permalink
feat(analyze): added route and functionality to allow users to search…
Browse files Browse the repository at this point in the history
… for a query and provide sentences that match it (with data to let the users analyze it)
  • Loading branch information
Ali-Aftab committed Feb 17, 2021
1 parent 85d6ee8 commit 03d8d48
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
24 changes: 22 additions & 2 deletions server/controllers/analyze.controller.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const { Op } = require("sequelize");
const { EntryTone } = require("../db");
const { EntryTone, SentenceTone } = require("../db");

module.exports.searchEntries = async (req, res) => {
try {
if (!req.body.searchQuery) {
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,
Expand All @@ -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 });
}
};
10 changes: 9 additions & 1 deletion server/routes/analyze.route.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
);
};

0 comments on commit 03d8d48

Please sign in to comment.