Commit b77927d4 authored by dasunx's avatar dasunx

Automated answer routes and controller functions added

parent 1e511967
const { spawn } = require('child_process');
const path = require('path');
const AutomatedAnswer = require('../models/automatedAnswer');
const Question = require('../models/question');
/**
* @Method - POST
* @param {*} question_id - ID of the question to create automated answer for
* @returns Whether the automated answer request created or not
*/
exports.requestAutomatedAnswer = async (req, res, next) => {
const { question_id } = req.body;
try {
const question = await Question.findById(question_id);
if (!question) {
return res
.status(404)
.json({ message: 'No question found for this id, Please try again with another question' });
}
let autoAnswer;
autoAnswer = await AutomatedAnswer.findOne({ question: question_id });
if (autoAnswer) {
return res
.status(303)
.json({ message: 'Automated answer is already generated', automatedanswer: autoAnswer });
}
res.status(201).json({ message: 'Automated answer requested! Please wait for few seconds' });
autoAnswer = await AutomatedAnswer.create({ question: question._id, loading: true });
spawn('python', [
path.join(__dirname, '..', 'python', 'auto-answer', 'scrapper.py'),
question.title,
question.tags,
autoAnswer._id
]);
} catch (err) {
next(err);
}
};
/**
*
* @Method - GET
* @qid {*} - question id
* @returns the automated answer for the question(qid) if there is one
*/
exports.getAutomatedAnswer = async (req, res, next) => {
const { qid } = req.params;
console.log('answer is here' + qid);
try {
const answer = await AutomatedAnswer.findOne({ question: qid });
if (!answer) {
return res.status(404).json({ message: 'No automated answer found for this question' });
}
return res.status(200).json({
message: 'Automated answer found',
automatedanswer: answer
});
} catch (err) {
next(err);
}
};
const Question = require('../models/question'); const Question = require('../models/question');
const User = require('../models/user');
const AutomatedAnswer = require('../models/automatedAnswer'); const AutomatedAnswer = require('../models/automatedAnswer');
const User = require('../models/user');
const { body, validationResult } = require('express-validator'); const { body, validationResult } = require('express-validator');
const { spawn } = require('child_process'); const { spawn } = require('child_process');
const path = require('path'); const path = require('path');
...@@ -35,12 +35,18 @@ exports.createQuestion = async (req, res, next) => { ...@@ -35,12 +35,18 @@ exports.createQuestion = async (req, res, next) => {
}); });
res.status(201).json(question); res.status(201).json(question);
const autoAnswer = await AutomatedAnswer.create({ question: question._id }); const autoAnswer = await AutomatedAnswer.create({ question: question._id });
spawn('python', [ const pythonScript = spawn('python', [
path.join(__dirname, '..', 'python', 'auto-answer', 'scrapper.py'), path.join(__dirname, '..', 'python', 'auto-answer', 'scrapper.py'),
title, title,
tags, tags,
autoAnswer._id autoAnswer._id
]); ]);
pythonScript.stdout.on('data', (data) => {
console.log('DATA FROM PYTHON IS HERE' + data);
});
pythonScript.stdout.on('end', function () {
console.log('PYTHON SCRIPT HAS BEEN ENDED');
});
} catch (error) { } catch (error) {
next(error); next(error);
} }
...@@ -96,6 +102,7 @@ exports.removeQuestion = async (req, res, next) => { ...@@ -96,6 +102,7 @@ exports.removeQuestion = async (req, res, next) => {
try { try {
await req.question.remove(); await req.question.remove();
res.json({ message: 'Your question successfully deleted.' }); res.json({ message: 'Your question successfully deleted.' });
await AutomatedAnswer.findOneAndRemove({ question: req.question._id });
} catch (error) { } catch (error) {
next(error); next(error);
} }
...@@ -113,22 +120,6 @@ exports.loadComment = async (req, res, next, id) => { ...@@ -113,22 +120,6 @@ exports.loadComment = async (req, res, next, id) => {
next(); next();
}; };
exports.getAutomatedAnswer = async (req, res, next) => {
console.log('HERE');
try {
const { q } = req.params;
const answer = await AutomatedAnswer.findOne({ question: q });
if (answer) {
res.json(answer);
} else {
res.status(404).json({ msg: 'No automated answer found' });
}
} catch (error) {
next(error);
}
};
exports.questionValidate = [ exports.questionValidate = [
body('title') body('title')
.exists() .exists()
......
...@@ -14,8 +14,7 @@ const { ...@@ -14,8 +14,7 @@ const {
listQuestions, listQuestions,
listByTags, listByTags,
listByUser, listByUser,
removeQuestion, removeQuestion
getAutomatedAnswer
} = require('./controllers/questions'); } = require('./controllers/questions');
const { const {
loadAnswers, loadAnswers,
...@@ -31,6 +30,7 @@ const requireAuth = require('./middlewares/requireAuth'); ...@@ -31,6 +30,7 @@ const requireAuth = require('./middlewares/requireAuth');
const questionAuth = require('./middlewares/questionAuth'); const questionAuth = require('./middlewares/questionAuth');
const commentAuth = require('./middlewares/commentAuth'); const commentAuth = require('./middlewares/commentAuth');
const answerAuth = require('./middlewares/answerAuth'); const answerAuth = require('./middlewares/answerAuth');
const { requestAutomatedAnswer, getAutomatedAnswer } = require('./controllers/automated-answer');
const router = require('express').Router(); const router = require('express').Router();
...@@ -51,7 +51,6 @@ router.get('/question', listQuestions); ...@@ -51,7 +51,6 @@ router.get('/question', listQuestions);
router.get('/questions/:tags', listByTags); router.get('/questions/:tags', listByTags);
router.get('/question/user/:username', listByUser); router.get('/question/user/:username', listByUser);
router.delete('/question/:question', [requireAuth, questionAuth], removeQuestion); router.delete('/question/:question', [requireAuth, questionAuth], removeQuestion);
router.get('/automatedanswer/:q', getAutomatedAnswer);
//tags //tags
router.get('/tags/populertags', listPopulerTags); router.get('/tags/populertags', listPopulerTags);
...@@ -74,6 +73,10 @@ router.post('/comment/:question/:answer?', [requireAuth, validate], createCommen ...@@ -74,6 +73,10 @@ router.post('/comment/:question/:answer?', [requireAuth, validate], createCommen
router.delete('/comment/:question/:comment', [requireAuth, commentAuth], removeComment); router.delete('/comment/:question/:comment', [requireAuth, commentAuth], removeComment);
router.delete('/comment/:question/:answer/:comment', [requireAuth, commentAuth], removeComment); router.delete('/comment/:question/:answer/:comment', [requireAuth, commentAuth], removeComment);
//automate-answer
router.post('/automatedanswer', requireAuth, requestAutomatedAnswer);
router.get('/automatedanswer/:qid', getAutomatedAnswer);
module.exports = (app) => { module.exports = (app) => {
app.use('/api', router); app.use('/api', router);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment