diff --git a/backend/app.js b/backend/app.js index ae076ad..31c41ca 100644 --- a/backend/app.js +++ b/backend/app.js @@ -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([ diff --git a/backend/controllers/bullpenSession.controller.js b/backend/controllers/bullpenSession.controller.js index d2b4d42..85b9c73 100644 --- a/backend/controllers/bullpenSession.controller.js +++ b/backend/controllers/bullpenSession.controller.js @@ -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}` + }); + }); +} diff --git a/backend/controllers/pitchType.controller.js b/backend/controllers/pitchType.controller.js index fbc6fb0..56bc6e2 100644 --- a/backend/controllers/pitchType.controller.js +++ b/backend/controllers/pitchType.controller.js @@ -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}` }); }); }; diff --git a/backend/models/bullpenSession.model.js b/backend/models/bullpenSession.model.js index a070051..6e61c8d 100644 --- a/backend/models/bullpenSession.model.js +++ b/backend/models/bullpenSession.model.js @@ -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, diff --git a/backend/models/pitch.model.js b/backend/models/pitch.model.js index 6e0716f..939fbe6 100644 --- a/backend/models/pitch.model.js +++ b/backend/models/pitch.model.js @@ -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 @@ -49,4 +34,4 @@ module.exports = (sequelize, DataTypes) => { tableName: 'Pitches' }); return Pitch; -}; \ No newline at end of file +}; diff --git a/backend/routes/bullpenSession.routes.js b/backend/routes/bullpenSession.routes.js new file mode 100644 index 0000000..da17692 --- /dev/null +++ b/backend/routes/bullpenSession.routes.js @@ -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); +}; diff --git a/backend/test/bullpenSession.test.js b/backend/test/bullpenSession.test.js new file mode 100644 index 0000000..7635f76 --- /dev/null +++ b/backend/test/bullpenSession.test.js @@ -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)); + }); +}) diff --git a/backend/test/data/bullpenSession.test.data.js b/backend/test/data/bullpenSession.test.data.js index 1927217..07f7023 100644 --- a/backend/test/data/bullpenSession.test.data.js +++ b/backend/test/data/bullpenSession.test.data.js @@ -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 }