mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
/q json response, searching with "" subj if there is no result
This commit is contained in:
parent
bf86b91172
commit
a1e99c53ae
3 changed files with 29 additions and 8 deletions
|
@ -248,32 +248,46 @@ app.route('/badtestsender').post(function (req, res, next) {
|
|||
|
||||
app.post('/isAdding', function (req, res) {
|
||||
logger.LogReq(req)
|
||||
// automatically saves to dataFile every n write
|
||||
let result = actions.ProcessIncomingRequest(req.body.datatoadd, data)
|
||||
res.json({
|
||||
success: result !== -1,
|
||||
newQuestions: result
|
||||
})
|
||||
utils.WriteBackup() // TODO: write every n times / after n minutes
|
||||
})
|
||||
|
||||
app.get('/q', function (req, res) {
|
||||
logger.LogReq(req)
|
||||
if (Object.keys(req.query).length === 0) {
|
||||
res.send('ask something!')
|
||||
res.json({
|
||||
message: `ask something! ?q=[question]&subj=[subject]&data=[question data]. 'subj' is optimal for faster result`,
|
||||
result: [],
|
||||
recievedData: JSON.stringify(req.query),
|
||||
success: false
|
||||
})
|
||||
} else {
|
||||
if (req.query.q) {
|
||||
if (req.query.q && req.query.data) {
|
||||
let subj = req.query.subj || ''
|
||||
let question = decodeURIComponent(req.query.q)
|
||||
let recData = {}
|
||||
try {
|
||||
recData = JSON.parse(req.query.data)
|
||||
} catch (e) {
|
||||
console.log(e) // TODO: normal error logging
|
||||
logger.Log(`Unable to parse recieved question data! '${req.query.data}'`, logger.GetColor('redbg'))
|
||||
}
|
||||
let r = data.Search(decodeURIComponent(req.query.q), subj, recData)
|
||||
let r = data.Search(question, subj, recData)
|
||||
|
||||
res.json(r)
|
||||
res.json({
|
||||
result: r,
|
||||
success: true
|
||||
})
|
||||
} else {
|
||||
res.send('no key "q" in query' + JSON.stringify(req.query))
|
||||
res.json({
|
||||
message: `Invalid question :(`,
|
||||
result: [],
|
||||
recievedData: JSON.stringify(req.query),
|
||||
success: false
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
@ -62,7 +62,13 @@ function ProcessIncomingRequest (recievedData, qdb) {
|
|||
|
||||
d.quiz.forEach((question) => {
|
||||
let q = new classes.Question(question.Q, question.A, question.data)
|
||||
let sames = qdb.Search(q, '') // TODO d.subj
|
||||
let sames = qdb.Search(q, d.subj)
|
||||
if (sames.length === 0) {
|
||||
sames = qdb.Search(q, d.subj)
|
||||
if (sames.length !== 0) {
|
||||
logger.Log(`\t'${d.subj}' gave no result but '' did! (adding new questions)`, logger.GetColor('redbg'))
|
||||
}
|
||||
}
|
||||
// if it didnt find any question, or every found questions match is lower thatn 80
|
||||
let isNew = sames.length === 0 || sames.every(searchResItem => {
|
||||
return searchResItem.match < classes.minMatchAmmount
|
||||
|
|
|
@ -133,6 +133,7 @@ function setNoLogReadInterval () {
|
|||
}
|
||||
|
||||
function Load () {
|
||||
Log('Loading logger...')
|
||||
try {
|
||||
var prevData = utils.ReadFile(statFile)
|
||||
data = JSON.parse(prevData)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue