bullpen session api progress
This commit is contained in:
parent
394ca64c34
commit
fa2dd0d0cc
|
|
@ -36,6 +36,7 @@ app.get("/", (req, res) => {
|
||||||
require('./routes/auth.routes')(app);
|
require('./routes/auth.routes')(app);
|
||||||
require('./routes/user.routes')(app);
|
require('./routes/user.routes')(app);
|
||||||
require('./routes/pitchType.routes')(app);
|
require('./routes/pitchType.routes')(app);
|
||||||
|
require('./routes/bullpenSession.routes')(app);
|
||||||
|
|
||||||
function initial() {
|
function initial() {
|
||||||
Role.bulkCreate([
|
Role.bulkCreate([
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,53 @@
|
||||||
const db = require("../models/index");
|
const db = require("../models/index");
|
||||||
const BullpenSession = db.BullpenSession;
|
const BullpenSession = db.BullpenSession;
|
||||||
|
const Pitch = db.Pitch;
|
||||||
|
|
||||||
exports.insert = (req, res) => {
|
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}`
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ exports.findOne = (req, res) => {
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
res.status(500).send({
|
res.status(500).send({
|
||||||
message: "Error retrieving pitch type with id=" + id
|
message: `Error retrieving pitch type with id=${id}: ${err.message}`
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -13,19 +13,11 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
as: 'pitches',
|
as: 'pitches',
|
||||||
});
|
});
|
||||||
BullpenSession.belongsTo(models.User, {
|
BullpenSession.belongsTo(models.User, {
|
||||||
as: "pitcher"
|
foreignKey: "pitcherId"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BullpenSession.init({
|
BullpenSession.init({
|
||||||
pitcherId: {
|
|
||||||
type: DataTypes.INTEGER,
|
|
||||||
allowNull: false,
|
|
||||||
references: {
|
|
||||||
model: 'User',
|
|
||||||
key: 'id'
|
|
||||||
}
|
|
||||||
},
|
|
||||||
startedAt: {
|
startedAt: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: false,
|
allowNull: false,
|
||||||
|
|
|
||||||
|
|
@ -9,28 +9,13 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
static associate(models) {
|
static associate(models) {
|
||||||
Pitch.belongsTo(models.BullpenSession, {
|
Pitch.belongsTo(models.BullpenSession, {
|
||||||
foreignKey: 'bullpenSessionId',
|
foreignKey: 'bullpenSessionId',
|
||||||
as: 'bullpenSession'
|
|
||||||
});
|
});
|
||||||
Pitch.belongsTo(models.PitchType);
|
Pitch.belongsTo(models.PitchType, {
|
||||||
|
foreignKey: 'pitchTypeId'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Pitch.init({
|
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: {
|
pitchTime: {
|
||||||
type: DataTypes.DATE,
|
type: DataTypes.DATE,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
|
|
@ -49,4 +34,4 @@ module.exports = (sequelize, DataTypes) => {
|
||||||
tableName: 'Pitches'
|
tableName: 'Pitches'
|
||||||
});
|
});
|
||||||
return Pitch;
|
return Pitch;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
};
|
||||||
|
|
@ -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));
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const bullpenData = {
|
const bullpenSession = {
|
||||||
pitcherId: 1,
|
pitcherId: 1,
|
||||||
startedAt: new Date(2025, 3, 22, 16, 5, 13),
|
startedAt: new Date(2025, 3, 22, 16, 5, 13),
|
||||||
finishedAt: new Date(2025, 3, 22, 16, 23, 45),
|
finishedAt: new Date(2025, 3, 22, 16, 23, 45),
|
||||||
|
|
@ -16,5 +16,5 @@ const bullpenData = {
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
bullpenData
|
bullpenSession
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue