Files
RushStatistics/backend/src/app.ts
jmosrael@gmail.com 3094ae3a08 ast constructed
2024-05-16 22:42:45 -07:00

111 lines
3.0 KiB
TypeScript

import express from 'express'
import { json } from 'body-parser'
import { Sequelize, Op } from 'sequelize'
import { database, Character, Game, Pick, App } from './db'
import { lexr, parseOrderByString, FilterParser } from './tokenizer'
const app = express()
const jsonParser = json()
const port = 3001
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/api/character/:characterId', async (req, res) => {
res.send(await Character.findOne({ where: { id: req.params['characterId'] } }))
})
app.post('/api/character', jsonParser, async (req, res) => {
const page = req.body.page
const orderBy = req.body.orderBy
const count = req.body.count
const characterData = await Character.findAll({
offset: page * count,
limit: count
})
const pageCount = Math.ceil((await Character.count()) / count)
res.setHeader('Content-Type', 'application/json')
res.json({ characterData, pageCount })
})
app.get('/api/game/:gameId', async (req, res) => {
res.send(await Game.findOne({ where: { id: req.params['gameId'] } }))
})
app.post('/api/game', jsonParser, async (req, res) => {
console.log(req.body)
const page = req.body.page || 0
const orderBy = req.body.orderBy ? parseOrderByString(req.body.orderBy) : ['id']
const count = req.body.count || 10
const filter = req.body.filter || ''
const gameData = await Game.findAll({
offset: page * count,
limit: count,
order: orderBy,
where: {
id: { [Op.eq]: 2 }
}
})
const pageCount = Math.ceil((await Character.count()) / count)
let fp = new FilterParser()
fp.lexer
.input(filter)
.tokens()
.forEach((t) => {
console.log(t)
})
console.log(fp.parseFilter(filter))
res.setHeader('Content-Type', 'application/json')
res.send({ gameData, pageCount })
})
app.get('/api/game/:gameId', async (req, res) => {
const game = await Game.findOne({ where: { id: req.params['gameId'] } })
res.send(game)
})
app.post('/api/game/:gameId/apps', async (req, res) => {
const apps = await App.findAll({ where: { gameId: req.params['gameId'] } })
res.send(apps)
})
app.post('/api/character/:characterId/apps', async (req, res) => {
const apps = await App.findAll({ where: { characterId: req.params['characterId'] } })
res.send(apps)
})
app.post('/api/game/:gameId/picks', async (req, res) => {
const picks = await Pick.findAll({ where: { gameId: req.params['gameId'] } })
res.send(picks)
})
app.post('/api/character/:characterId/picks', async (req, res) => {
const picks = await Pick.findAll({ where: { characterId: req.params['characterId'] } })
res.send(picks)
})
app.post('/api/character/:characterId/gameHistory', async (req, res) => {
const gameHistory = await Character.findOne({
include: [
{ model: Game, as: 'characterPickedForGame' },
{ model: Game, as: 'characterAppliedForGame' }
],
where: { id: req.params['characterId'] }
})
res.send(gameHistory)
})
app.listen(port, async () => {
await database.authenticate()
return console.log(`Express is listening at http://localhost:${port}`)
})