mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Saving questions
This commit is contained in:
parent
8d2bc709f3
commit
2998239344
3 changed files with 86 additions and 27 deletions
|
@ -70,7 +70,6 @@ const todosFile = 'data/todos.json'
|
||||||
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
const userScriptFile = 'submodules/moodle-test-userscript/stable.user.js'
|
||||||
const rootRedirectToFile = 'data/apiRootRedirectTo'
|
const rootRedirectToFile = 'data/apiRootRedirectTo'
|
||||||
const recievedQuestionFile = 'stats/recievedQuestions'
|
const recievedQuestionFile = 'stats/recievedQuestions'
|
||||||
const dbsFile = 'data/dbs.json'
|
|
||||||
|
|
||||||
// other constants
|
// other constants
|
||||||
const line = '====================================================' // lol
|
const line = '====================================================' // lol
|
||||||
|
@ -94,6 +93,10 @@ function GetApp(): ModuleType {
|
||||||
}
|
}
|
||||||
|
|
||||||
// files in public dirs
|
// files in public dirs
|
||||||
|
const questionDbsDir = publicDir + 'questionDbs'
|
||||||
|
const dbsFile = publicDir + 'questionDbs.json'
|
||||||
|
const savedQuestionsFile = publicDir + 'savedQuestions.json'
|
||||||
|
const savedQuestionsDir = publicDir + 'savedQuestions'
|
||||||
const recivedFiles = publicDir + 'recivedfiles'
|
const recivedFiles = publicDir + 'recivedfiles'
|
||||||
const uloadFiles = publicDir + 'f'
|
const uloadFiles = publicDir + 'f'
|
||||||
const dataFiles: Array<DataFile> = utils.ReadJSON(dbsFile)
|
const dataFiles: Array<DataFile> = utils.ReadJSON(dbsFile)
|
||||||
|
@ -144,7 +147,7 @@ function GetApp(): ModuleType {
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
const questionDbs = loadJSON(dataFiles, publicDir)
|
const questionDbs = loadJSON(dataFiles, questionDbsDir)
|
||||||
let version = ''
|
let version = ''
|
||||||
let rootRedirectURL = ''
|
let rootRedirectURL = ''
|
||||||
let motd = ''
|
let motd = ''
|
||||||
|
@ -782,9 +785,29 @@ function GetApp(): ModuleType {
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/allqr.txt', function(req: Request, res: any) {
|
app.get('/allqr.txt', function(req: Request, res: any) {
|
||||||
// TODO: if dataId param exists download only that db
|
logger.LogReq(req)
|
||||||
|
const db: any = req.query.db
|
||||||
|
let stringifiedData = ''
|
||||||
|
|
||||||
|
if (db) {
|
||||||
|
const requestedDb = questionDbs.find((qdb) => {
|
||||||
|
return qdb.name === db
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!requestedDb) {
|
||||||
|
res.end(`No such db ${db}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stringifiedData = '\n' + line
|
||||||
|
stringifiedData += ` Questions in ${requestedDb.name}: `
|
||||||
|
stringifiedData += line + '\n'
|
||||||
|
stringifiedData += dataToString(requestedDb.data)
|
||||||
|
stringifiedData += '\n' + line + line + '\n'
|
||||||
|
} else {
|
||||||
res.set('Content-Type', 'text/plain')
|
res.set('Content-Type', 'text/plain')
|
||||||
const stringifiedData = questionDbs.map((qdb) => {
|
stringifiedData = questionDbs
|
||||||
|
.map((qdb) => {
|
||||||
let result = ''
|
let result = ''
|
||||||
result += '\n' + line
|
result += '\n' + line
|
||||||
result += ` Questions in ${qdb.name}: `
|
result += ` Questions in ${qdb.name}: `
|
||||||
|
@ -793,9 +816,9 @@ function GetApp(): ModuleType {
|
||||||
result += '\n' + line + line + '\n'
|
result += '\n' + line + line + '\n'
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
res.send(stringifiedData.join('\n\n'))
|
.join('\n\n')
|
||||||
res.end()
|
}
|
||||||
logger.LogReq(req)
|
res.end(stringifiedData)
|
||||||
})
|
})
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------------------
|
// -------------------------------------------------------------------------------------------
|
||||||
|
@ -1026,7 +1049,32 @@ function GetApp(): ModuleType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function saveQuestion(questions, subj, location, userid) {
|
||||||
|
const toWrite = {
|
||||||
|
questions: questions,
|
||||||
|
subj: subj,
|
||||||
|
userid: userid,
|
||||||
|
location: location,
|
||||||
|
date: new Date(),
|
||||||
|
}
|
||||||
|
const fname = `${utils.GetDateString()}_${userid}_${location}.json`
|
||||||
|
|
||||||
|
const savedQuestions = utils.ReadJSON(savedQuestionsFile)
|
||||||
|
savedQuestions.push({
|
||||||
|
fname: fname,
|
||||||
|
subj: subj,
|
||||||
|
userid: userid,
|
||||||
|
location: location,
|
||||||
|
date: new Date(),
|
||||||
|
})
|
||||||
|
utils.WriteFile(JSON.stringify(savedQuestions), savedQuestionsFile)
|
||||||
|
|
||||||
|
utils.WriteFile(JSON.stringify(toWrite), `${savedQuestionsDir}/${fname}`)
|
||||||
|
}
|
||||||
|
|
||||||
app.post('/ask', function(req: Request, res) {
|
app.post('/ask', function(req: Request, res) {
|
||||||
|
const user: User = req.session.user
|
||||||
|
|
||||||
if (!req.body.questions) {
|
if (!req.body.questions) {
|
||||||
res.json({
|
res.json({
|
||||||
message:
|
message:
|
||||||
|
@ -1038,6 +1086,7 @@ function GetApp(): ModuleType {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const subj: any = req.body.subj || ''
|
const subj: any = req.body.subj || ''
|
||||||
|
const location = req.body.location.split('/')[2]
|
||||||
|
|
||||||
writeAskData(req.body)
|
writeAskData(req.body)
|
||||||
|
|
||||||
|
@ -1046,14 +1095,24 @@ function GetApp(): ModuleType {
|
||||||
})
|
})
|
||||||
|
|
||||||
Promise.all(resultPromises).then((results) => {
|
Promise.all(resultPromises).then((results) => {
|
||||||
res.json(
|
const response = results.map((result: any) => {
|
||||||
results.map((result: any) => {
|
|
||||||
return {
|
return {
|
||||||
answers: result.result,
|
answers: result.result,
|
||||||
question: result.question,
|
question: result.question,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
res.json(response)
|
||||||
|
|
||||||
|
const saveableQuestions = response.reduce((acc, res) => {
|
||||||
|
// TODO
|
||||||
|
// if (res.answers.length === 0) {
|
||||||
|
// acc.push(res.question)
|
||||||
|
// }
|
||||||
|
acc.push(res.question)
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
saveQuestion(saveableQuestions, subj, location, user.id)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -296,7 +296,7 @@ export function loadJSON(
|
||||||
dataDir: string
|
dataDir: string
|
||||||
): Array<QuestionDb> {
|
): Array<QuestionDb> {
|
||||||
return dataFiles.reduce((acc, dataFile, index) => {
|
return dataFiles.reduce((acc, dataFile, index) => {
|
||||||
const dataPath = dataDir + dataFile.path
|
const dataPath = dataDir + '/' + dataFile.path
|
||||||
|
|
||||||
if (!utils.FileExists(dataPath)) {
|
if (!utils.FileExists(dataPath)) {
|
||||||
utils.WriteFile(JSON.stringify([]), dataPath)
|
utils.WriteFile(JSON.stringify([]), dataPath)
|
||||||
|
|
|
@ -93,9 +93,9 @@ function getAWorker(i, initData) {
|
||||||
// logger.Log(`[MAIN]: Msg from worker #${i}`, msg)
|
// logger.Log(`[MAIN]: Msg from worker #${i}`, msg)
|
||||||
// })
|
// })
|
||||||
|
|
||||||
worker.on('online', () => {
|
// worker.on('online', () => {
|
||||||
logger.Log(`[THREAD #${i}]: Worker ${i} online`)
|
// logger.Log(`[THREAD #${i}]: Worker ${i} online`)
|
||||||
})
|
// })
|
||||||
|
|
||||||
worker.on('error', (err) => {
|
worker.on('error', (err) => {
|
||||||
logger.Log('Worker error!', logger.GetColor('redbg'))
|
logger.Log('Worker error!', logger.GetColor('redbg'))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue