Added todo endpoint

This commit is contained in:
mrfry 2020-11-18 15:37:51 +01:00
parent 71790e8348
commit 803cb677ba

View file

@ -45,6 +45,7 @@ const quickVotes = 'stats/qvote/votes.json'
const testUsersFile = 'data/testUsers.json'
const idStatFile = 'stats/idstats'
const idvStatFile = 'stats/idvstats'
const todosFile = 'data/todos.json'
// other constants
const maxVeteranPwGetCount = 10
@ -215,6 +216,45 @@ function GetApp() {
// -------------------------------------------------------------
app.get('/todos', (req, res) => {
logger.LogReq(req)
const userId = req.session.user.id
const id = req.query.id
const todos = utils.ReadJSON(todosFile)
if (id) {
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({
msg: 'updated',
result: 'ok',
})
} else {
res.json({
todos: todos,
userId: userId,
result: 'ok',
})
}
})
app.get('/ranklist', (req, res) => {
logger.LogReq(req)
let result