bullpen session api progress

This commit is contained in:
Sascha Kühl 2025-03-21 18:14:40 +01:00
parent 394ca64c34
commit fa2dd0d0cc
8 changed files with 125 additions and 32 deletions

View File

@ -36,6 +36,7 @@ app.get("/", (req, res) => {
require('./routes/auth.routes')(app);
require('./routes/user.routes')(app);
require('./routes/pitchType.routes')(app);
require('./routes/bullpenSession.routes')(app);
function initial() {
Role.bulkCreate([

View File

@ -1,6 +1,53 @@
const db = require("../models/index");
const BullpenSession = db.BullpenSession;
const Pitch = db.Pitch;
exports.insert = (req, res) => {
BullpenSession.create(req.body)
BullpenSession.create(req.body, { include: [{
model: Pitch,
as: 'pitches'
}]}
)
.then(bullpenSession => {
return res.status(201).send(bullpenSession);
})
.catch(err => {
res.status(500).send({ message: err.message });
});
};
exports.findAll = (req, res) => {
BullpenSession.findAll({
include: {
model: Pitch,
as: 'pitches'
}
})
.then(bullpenSessions => {
res.status(200).send(bullpenSessions);
});
}
exports.findOne = (req, res) => {
const id = req.params.id;
BullpenSession.findByPk(id, {
include: {
model: Pitch,
as: 'pitches'
}
})
.then(data => {
if (data) {
res.send(data);
} else {
res.status(404).send({
message: `Cannot find bullpen session with id=${id}.`
});
}
})
.catch(err => {
res.status(500).send({
message: `Error retrieving bullpen session with id=${id}: ${err.message}`
});
});
}

View File

@ -29,7 +29,7 @@ exports.findOne = (req, res) => {
})
.catch(err => {
res.status(500).send({
message: "Error retrieving pitch type with id=" + id
message: `Error retrieving pitch type with id=${id}: ${err.message}`
});
});
};

View File

@ -13,19 +13,11 @@ module.exports = (sequelize, DataTypes) => {
as: 'pitches',
});
BullpenSession.belongsTo(models.User, {
as: "pitcher"
foreignKey: "pitcherId"
});
}
}
BullpenSession.init({
pitcherId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'User',
key: 'id'
}
},
startedAt: {
type: DataTypes.DATE,
allowNull: false,

View File

@ -9,28 +9,13 @@ module.exports = (sequelize, DataTypes) => {
static associate(models) {
Pitch.belongsTo(models.BullpenSession, {
foreignKey: 'bullpenSessionId',
as: 'bullpenSession'
});
Pitch.belongsTo(models.PitchType);
Pitch.belongsTo(models.PitchType, {
foreignKey: 'pitchTypeId'
});
}
}
Pitch.init({
pitchTypeId: {
type: DataTypes.INTEGER,
allowNull: false,
references: { // User belongsTo PitchType 1:1
model: 'PitchTypes',
key: 'id'
}
},
bullpenSessionId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'BullpenSessions',
key: 'id'
}
},
pitchTime: {
type: DataTypes.DATE,
allowNull: false

View File

@ -0,0 +1,25 @@
const { authJwt } = require("../middleware");
const controller = require("../controllers/bullpenSession.controller");
module.exports = function(app) {
app.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
app.post(
"/api/bullpen_session",
[authJwt.verifyToken],
controller.insert);
app.get(
"/api/bullpen_session",
[authJwt.verifyToken],
controller.findAll);
app.get(
"/api/bullpen_session/:id",
[authJwt.verifyToken],
controller.findOne);
};

View File

@ -0,0 +1,43 @@
const request = require("supertest")
const {
expect,
describe,
test,
} = require('@jest/globals');
const app = require("../app")
const { bullpenSession } = require("./data/bullpenSession.test.data")
describe("Test bullpen session", () => {
test("should create bullpen session with pitches", async () => {
let response = await request(app)
.post("/api/auth/login")
.send({
email: 'ryan.nolan@bullpen.com',
password: 'nolan'
});
const user = response.body;
response = await request(app)
.post("/api/bullpen_session")
.set('x-access-token', user.accessToken)
.send(bullpenSession);
expect(response.statusCode).toBe(201);
const bullpenSessionData = await response.body;
expect(bullpenSessionData).toBeDefined();
expect(bullpenSessionData.id).toBeDefined();
expect(bullpenSessionData.id).toBeGreaterThan(0);
expect(bullpenSessionData.pitches).toBeDefined();
expect(Array.isArray(bullpenSessionData.pitches)).toBe(true);
expect(bullpenSessionData.pitches.length).toBe(2);
response = await request(app)
.get(`/api/bullpen_session/${bullpenSessionData.id}`)
.set('x-access-token', user.accessToken);
expect(response.statusCode).toBe(200);
console.log(JSON.stringify(response.body, null, 2));
});
})

View File

@ -1,4 +1,4 @@
const bullpenData = {
const bullpenSession = {
pitcherId: 1,
startedAt: new Date(2025, 3, 22, 16, 5, 13),
finishedAt: new Date(2025, 3, 22, 16, 23, 45),
@ -16,5 +16,5 @@ const bullpenData = {
};
module.exports = {
bullpenData
bullpenSession
}