Seperated api module to submodules

This commit is contained in:
mrfry
2021-03-30 16:59:37 +02:00
parent 2a0926aa7e
commit c64c189ce3
11 changed files with 2072 additions and 1895 deletions
+65
View File
@@ -0,0 +1,65 @@
import logger from '../../../utils/logger'
import utils from '../../../utils/utils'
import { Request, SubmoduleData } from '../../../types/basicTypes'
const todosFile = 'data/todos.json'
function setup(data: SubmoduleData): void {
const { app /* userDB, url, publicdirs, moduleSpecificData */ } = data
app.get('/voteTodo', (req: Request, res: any) => {
logger.LogReq(req)
const userId = req.session.user.id
const id: any = req.query.id
const todos = utils.ReadJSON(todosFile)
if (!id) {
res.json({
msg: 'id query undefined',
result: 'not ok',
})
}
const cardIndex = todos.cards.findIndex((currcard) => {
return currcard.id === parseInt(id)
})
if (cardIndex === -1) {
res.json({
msg: 'card not found',
result: 'not ok',
})
return
}
const ind = todos.cards[cardIndex].votes.indexOf(userId)
if (ind === -1) {
todos.cards[cardIndex].votes.push(userId)
} else {
todos.cards[cardIndex].votes.splice(ind, 1)
}
utils.WriteFile(JSON.stringify(todos, null, 2), todosFile)
res.json({
todos: todos,
userId: userId,
msg: 'updated',
result: 'ok',
})
})
app.get('/todos', (req: Request, res: any) => {
logger.LogReq(req)
const userId = req.session.user.id
const todos = utils.ReadJSON(todosFile)
res.json({
todos: todos,
userId: userId,
result: 'ok',
})
})
}
export default {
setup: setup,
}