add in sequelize, setup apis

This commit is contained in:
iamBadgers
2024-03-06 23:15:47 -08:00
parent f346bcafc0
commit 05e31f5986
3 changed files with 1528 additions and 30 deletions

View File

@@ -1,11 +1,105 @@
import express from 'express'
// import sqlite3 from 'sqlite3'
import { Sequelize, Model, DataTypes } from 'sequelize'
import { json } from 'body-parser'
const databasePath = './testdb.db'
let database
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: databasePath
})
const Character = sequelize.define(
'Characters',
{
id: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT },
playerName: { type: DataTypes.TEXT },
role: { type: DataTypes.TEXT },
creationDate: { type: DataTypes.INTEGER }
},
{ timestamps: false }
)
const Game = sequelize.define(
'Games',
{
id: { type: DataTypes.INTEGER, primaryKey: true },
title: { type: DataTypes.TEXT },
status: { type: DataTypes.TEXT },
fix: { type: DataTypes.BOOLEAN },
postdate: { type: DataTypes.INTEGER },
gamemaster: { type: DataTypes.TEXT },
payoutEB: { type: DataTypes.INTEGER },
payoutIP: { type: DataTypes.INTEGER },
payputLoot: { type: DataTypes.INTEGER }
},
{ timestamps: false }
)
const Pick = sequelize.define(
'Picks',
{
gameId: { type: DataTypes.INTEGER, primaryKey: true },
gameTitle: { type: DataTypes.TEXT },
character: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
const App = sequelize.define(
'Apps',
{
gameId: { type: DataTypes.INTEGER, primaryKey: true },
gameTitle: { type: DataTypes.TEXT },
characterId: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
const app = express()
const port = 3000
const jsonParser = json()
const port = 3001
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
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
res.setHeader('Content-Type', 'application/json')
res.send(await Character.findAll({offset: page * count, limit: count}))
})
app.get('/api/game/:characterId', async (req, res) => {
res.send(await Game.findOne({ where: { id: req.params['characterId'] } }))
})
app.post('/api/game', jsonParser, async (req, res) => {
const page = req.body.page
const orderBy = req.body.orderBy
const count = req.body.count
res.setHeader('Content-Type', 'application/json')
res.send(await Game.findAll({offset: page * count, limit: count}))
})
app.get('/api/game/:gameId', async (req, res) => {
res.send(await Game.findOne({ where: { id: req.params['gameId'] } }))
})
app.listen(port, async () => {
await sequelize.authenticate()
return console.log(`Express is listening at http://localhost:${port}`)
})