setup the dm apis
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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) {
|
||||
@@ -37,9 +37,7 @@ export function addCharacterApis(app, jsonParser) {
|
||||
const totalCount = await Character.count({
|
||||
where: filter
|
||||
})
|
||||
const pageCount = Math.ceil(
|
||||
totalCount / count
|
||||
)
|
||||
const pageCount = Math.ceil(totalCount / count)
|
||||
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.send({ characterData, pageCount, totalCount })
|
||||
|
||||
58
backend/src/dmservice.ts
Normal file
58
backend/src/dmservice.ts
Normal 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)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -35,9 +35,7 @@ export function addGameApis(app, jsonParser) {
|
||||
const totalCount = await Game.count({
|
||||
where: filter
|
||||
})
|
||||
const pageCount = Math.ceil(
|
||||
totalCount / count
|
||||
)
|
||||
const pageCount = Math.ceil(totalCount / count)
|
||||
|
||||
res.setHeader('Content-Type', 'application/json')
|
||||
res.send({ gameData, pageCount, totalCount })
|
||||
|
||||
@@ -98,7 +98,6 @@ export class FilterParser {
|
||||
ctx.accept('value', m[3])
|
||||
})
|
||||
|
||||
|
||||
this.lexer.rule(spacerRegex, (ctx, m) => {
|
||||
ctx.ignore()
|
||||
})
|
||||
|
||||
10
frontend/src/vues/DmList.vue
Normal file
10
frontend/src/vues/DmList.vue
Normal file
@@ -0,0 +1,10 @@
|
||||
<template>
|
||||
|
||||
</template>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
|
||||
<script setup lang="ts">
|
||||
|
||||
</script>
|
||||
Reference in New Issue
Block a user