setup the dm apis

This commit is contained in:
jmosrael@gmail.com
2024-07-12 00:46:53 -07:00
parent 9df2470f65
commit 6d03e450c9
6 changed files with 77 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ import { OrderByParser, FilterParser } from './tokenizer'
import { addGameApis } from './gameservice'
import { addCharacterApis } from './characterservice'
import { addRushStatsApis } from './rushstatsservice'
import { addDmApis } from './dmservice'
const app = express()
const jsonParser = json()
@@ -14,7 +15,7 @@ const port = 3001
addGameApis(app, jsonParser)
addCharacterApis(app, jsonParser)
addRushStatsApis(app, jsonParser)
addDmApis(app, jsonParser)
app.use(express.static(__dirname + '/frontend'))
app.use('/', (req, res) => {

View File

@@ -5,7 +5,7 @@ export function addCharacterApis(app, jsonParser) {
app.get('/api/character/:characterId', async (req, res) => {
try {
const character = await Character.findOne({
attributes: ["id", "characterName", "playerName", "role", "creationDate", "status"],
attributes: ['id', 'characterName', 'playerName', 'role', 'creationDate', 'status'],
where: { id: req.params['characterId'] }
})
if (character) {
@@ -35,11 +35,9 @@ export function addCharacterApis(app, jsonParser) {
where: filter
})
const totalCount = await Character.count({
where: filter
})
const pageCount = Math.ceil(
totalCount / count
)
where: filter
})
const pageCount = Math.ceil(totalCount / count)
res.setHeader('Content-Type', 'application/json')
res.send({ characterData, pageCount, totalCount })

58
backend/src/dmservice.ts Normal file
View File

@@ -0,0 +1,58 @@
import { database, Character, Game, Pick, App } from './db'
import { OrderByParser, FilterParser, ParsingError } from './tokenizer'
import { fn, col } from 'sequelize'
export function addDmApis(app, jsonParser) {
app.get('/api/dm/:dmName', async (req, res) => {
const dmName = req.params.dmName
let games = await Game.findAll({
where: { gamemaster: dmName }
})
res.send(games)
})
app.post('/api/dm', jsonParser, async (req, res) => {
try {
const fp = new FilterParser()
const obp = new OrderByParser()
const page = req.body.page || 0
const orderBy = req.body.orderBy ? obp.parse(req.body.orderBy) : ['id']
const count = req.body.count || 10
const filter = req.body.filter ? fp.parse(req.body.filter) : {}
const dms = await Game.findAll({
offset: page * count,
limit: count,
attributes: ['gamemaster', [fn('count', col('id')), 'gamecount']],
group: ['gamemaster'],
order: orderBy,
where: filter
})
const totalCount = (
await Game.count({
group: ['gamemaster'],
where: filter
})
).length
const pageCount = Math.ceil(totalCount / count)
res.setHeader('Content-Type', 'application/json')
res.send({
dms,
pageCount,
totalCount
})
} catch (e) {
if (e instanceof ParsingError) {
res.status(400).send('Could not parse filter.')
} else {
res.status(500).send(e)
}
}
})
}

View File

@@ -33,11 +33,9 @@ export function addGameApis(app, jsonParser) {
where: filter
})
const totalCount = await Game.count({
where: filter
})
const pageCount = Math.ceil(
totalCount / count
)
where: filter
})
const pageCount = Math.ceil(totalCount / count)
res.setHeader('Content-Type', 'application/json')
res.send({ gameData, pageCount, totalCount })

View File

@@ -98,7 +98,6 @@ export class FilterParser {
ctx.accept('value', m[3])
})
this.lexer.rule(spacerRegex, (ctx, m) => {
ctx.ignore()
})