mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2026-04-28 19:27:38 +02:00
Seperated api module to submodules
This commit is contained in:
@@ -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,
|
||||
}
|
||||
Reference in New Issue
Block a user