Skip to content

Commit

Permalink
feat(analyze): added new route that shows the user their average mood…
Browse files Browse the repository at this point in the history
… from all their entries
  • Loading branch information
Ali-Aftab committed Feb 17, 2021
1 parent bf714c3 commit bea6c45
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 37 additions & 1 deletion server/controllers/analyze.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Op } = require("sequelize");
const Sequelize = require("sequelize");
const { Op } = Sequelize;
const { EntryTone, SentenceTone } = require("../db");

module.exports.searchEntries = async (req, res) => {
Expand Down Expand Up @@ -42,3 +43,38 @@ module.exports.searchSentences = async (req, res) => {
res.json({ message: "Error occured when searching your query!", error });
}
};

module.exports.detectAverageMood = async (req, res) => {
try {
const { userId } = req;
let averageMood;

const allEntries = await EntryTone.findAll({
where: {
userId,
},
attributes: [
[Sequelize.fn("AVG", Sequelize.col("anger")), "anger"],
[Sequelize.fn("AVG", Sequelize.col("disgust")), "disgust"],
[Sequelize.fn("AVG", Sequelize.col("fear")), "fear"],
[Sequelize.fn("AVG", Sequelize.col("joy")), "joy"],
[Sequelize.fn("AVG", Sequelize.col("sadness")), "sadness"],
[Sequelize.fn("AVG", Sequelize.col("analytical")), "analytical"],
[Sequelize.fn("AVG", Sequelize.col("confident")), "confident"],
[Sequelize.fn("AVG", Sequelize.col("tentative")), "tentative"],
],
}).then((data) => {
averageMood = data;
});
res.json({
message: "Your average mood has been computed!",
data: averageMood,
});
} catch (error) {
console.log(error);
res.json({
message: "Error occured when trying to detect the average mood!",
error,
});
}
};
3 changes: 3 additions & 0 deletions server/routes/analyze.route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
searchEntries,
searchSentences,
detectAverageMood,
} = require("../controllers/analyze.controller");
const { authJwt } = require("../middleware");

Expand All @@ -19,4 +20,6 @@ module.exports = function (app) {
[authJwt.verifyToken],
searchSentences
);

app.get("/api/analyze/averagemood", [authJwt.verifyToken], detectAverageMood);
};

0 comments on commit bea6c45

Please sign in to comment.