Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-155
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021-155
2021-155
Commits
b77927d4
Commit
b77927d4
authored
Aug 20, 2021
by
dasunx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Automated answer routes and controller functions added
parent
1e511967
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
78 additions
and
21 deletions
+78
-21
backend/controllers/automated-answer.js
backend/controllers/automated-answer.js
+63
-0
backend/controllers/questions.js
backend/controllers/questions.js
+9
-18
backend/routes.js
backend/routes.js
+6
-3
No files found.
backend/controllers/automated-answer.js
0 → 100644
View file @
b77927d4
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
);
}
};
backend/controllers/questions.js
View file @
b77927d4
const
Question
=
require
(
'
../models/question
'
);
const
User
=
require
(
'
../models/user
'
);
const
AutomatedAnswer
=
require
(
'
../models/automatedAnswer
'
);
const
User
=
require
(
'
../models/user
'
);
const
{
body
,
validationResult
}
=
require
(
'
express-validator
'
);
const
{
spawn
}
=
require
(
'
child_process
'
);
const
path
=
require
(
'
path
'
);
...
...
@@ -35,12 +35,18 @@ exports.createQuestion = async (req, res, next) => {
});
res
.
status
(
201
).
json
(
question
);
const
autoAnswer
=
await
AutomatedAnswer
.
create
({
question
:
question
.
_id
});
spawn
(
'
python
'
,
[
const
pythonScript
=
spawn
(
'
python
'
,
[
path
.
join
(
__dirname
,
'
..
'
,
'
python
'
,
'
auto-answer
'
,
'
scrapper.py
'
),
title
,
tags
,
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
)
{
next
(
error
);
}
...
...
@@ -96,6 +102,7 @@ exports.removeQuestion = async (req, res, next) => {
try
{
await
req
.
question
.
remove
();
res
.
json
({
message
:
'
Your question successfully deleted.
'
});
await
AutomatedAnswer
.
findOneAndRemove
({
question
:
req
.
question
.
_id
});
}
catch
(
error
)
{
next
(
error
);
}
...
...
@@ -113,22 +120,6 @@ exports.loadComment = async (req, res, next, id) => {
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
=
[
body
(
'
title
'
)
.
exists
()
...
...
backend/routes.js
View file @
b77927d4
...
...
@@ -14,8 +14,7 @@ const {
listQuestions
,
listByTags
,
listByUser
,
removeQuestion
,
getAutomatedAnswer
removeQuestion
}
=
require
(
'
./controllers/questions
'
);
const
{
loadAnswers
,
...
...
@@ -31,6 +30,7 @@ const requireAuth = require('./middlewares/requireAuth');
const
questionAuth
=
require
(
'
./middlewares/questionAuth
'
);
const
commentAuth
=
require
(
'
./middlewares/commentAuth
'
);
const
answerAuth
=
require
(
'
./middlewares/answerAuth
'
);
const
{
requestAutomatedAnswer
,
getAutomatedAnswer
}
=
require
(
'
./controllers/automated-answer
'
);
const
router
=
require
(
'
express
'
).
Router
();
...
...
@@ -51,7 +51,6 @@ router.get('/question', listQuestions);
router
.
get
(
'
/questions/:tags
'
,
listByTags
);
router
.
get
(
'
/question/user/:username
'
,
listByUser
);
router
.
delete
(
'
/question/:question
'
,
[
requireAuth
,
questionAuth
],
removeQuestion
);
router
.
get
(
'
/automatedanswer/:q
'
,
getAutomatedAnswer
);
//tags
router
.
get
(
'
/tags/populertags
'
,
listPopulerTags
);
...
...
@@ -74,6 +73,10 @@ router.post('/comment/:question/:answer?', [requireAuth, validate], createCommen
router
.
delete
(
'
/comment/:question/: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
)
=>
{
app
.
use
(
'
/api
'
,
router
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment