progress on team and player positions

This commit is contained in:
Sascha Kühl 2025-05-29 22:28:26 +02:00
parent 8618533283
commit 35d8633567
16 changed files with 191 additions and 12 deletions

View File

@ -47,7 +47,15 @@ exports.findOne = (req, res) => {
const id = req.params.id; const id = req.params.id;
Player.findByPk(id, { Player.findByPk(id, {
include: { model: User } include: {
model: User,
as: 'user',
include: {
model: Auth,
as: 'auth',
attributes: ['email']
}
}
}).then(data => { }).then(data => {
if (data) { if (data) {
res.send(data); res.send(data);

View File

@ -0,0 +1,41 @@
const db = require("../models/index");
const Team = db.Team;
const Player = db.Player;
const Op = db.Sequelize.Op;
exports.findAll = (req, res) => {
const title = req.query.title;
const condition = title ? {title: {[Op.iLike]: `%${title}%`}} : null;
Team.findAll({
where: condition,
include: ['players']
}).then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving teams."
});
});
};
exports.findOne = (req, res) => {
const id = req.params.id;
Team.findByPk(id, {
include: ['players']
}).then(data => {
res.send(data);
}).catch(err => {
res.status(500).send({
message: `Error retrieving team with id=${id}: ${err.message}`
});
});
};
exports.addPlayer = (req, res) => {
const id = req.params.id;
const playerId = req.params.playerId;
}

View File

View File

View File

@ -18,10 +18,10 @@ module.exports = {
onDelete: 'CASCADE' onDelete: 'CASCADE'
}, },
height: { height: {
type: Sequelize.FLOAT type: Sequelize.INTEGER
}, },
weight: { weight: {
type: Sequelize.FLOAT type: Sequelize.INTEGER
}, },
gender: { gender: {
allowNull: false, allowNull: false,

View File

@ -1,4 +1,3 @@
'use strict';
const { Model } = require('sequelize'); const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => { module.exports = (sequelize, DataTypes) => {
class PitchType extends Model { class PitchType extends Model {

View File

@ -12,6 +12,16 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: "userId", foreignKey: "userId",
as: "user" as: "user"
}); });
Player.belongsToMany(models.Team, {
through: "PlayerTeams",
foreignKey: 'playerId',
otherKey: 'teamId',
as: 'teams',
});
Player.hasMany(models.Position, {
through: "PlayerPositions",
as: 'positions',
})
} }
} }
Player.init({ Player.init({

View File

@ -0,0 +1,30 @@
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class PlayerTeam extends Model {
static associate(models) {}
}
PlayerTeam.init({
playerId: {
type: DataTypes.INTEGER,
references: {
model: 'Players',
key: 'id'
}
},
teamId: {
type: DataTypes.INTEGER,
references: {
model: 'Teams',
key: 'id'
}
}
}, {
sequelize,
modelName: 'PlayerTeam',
tableName: 'PlayerTeams'
});
return PlayerTeam;
};

View File

@ -0,0 +1,33 @@
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Position extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
}
Position.init({
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
abbreviation: {
type: DataTypes.STRING,
allowNull: false
},
description: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'Position',
tableName: 'Positions'
});
return Position;
};

View File

@ -0,0 +1,34 @@
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Team extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
Team.belongsToMany(models.Player, {
through: "PlayerTeams",
foreignKey: 'teamId',
otherKey: 'playerId',
as: 'players',
});
}
}
Team.init({
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
description: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: 'Team',
tableName: 'Teams'
});
return Team;
};

View File

@ -0,0 +1,20 @@
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
return queryInterface.bulkInsert('Positions',[
{ name: 'Pitcher', abbreviation: 'P', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Catcher', abbreviation: 'C', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'First Baseman', abbreviation: '1B', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Second Baseman', abbreviation: '2B', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Third Baseman', abbreviation: '3B', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Short Stop', abbreviation: 'SS', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Left Fielder', abbreviation: 'LF', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Center Fielder', abbreviation: 'CF', description: '', createdAt: new Date(), updatedAt: new Date() },
{ name: 'Right Fielder', abbreviation: 'RF', description: '', createdAt: new Date(), updatedAt: new Date() },
]);
},
async down (queryInterface, Sequelize) {
return queryInterface.dropTable('Positions');
}
};

View File

@ -1,17 +1,21 @@
const signupPlayer = { const signupPlayer = {
firstName: "Hans", height: 183,
lastName: "Zimmer", weight: 83,
dateOfBirth: "1956-11-23",
email: "base.ball@email.com",
height: 183.3,
weight: 83.3,
gender: "male", gender: "male",
bats: "right", bats: "right",
throws: "left", throws: "left",
jerseyNumber: 13, jerseyNumber: 13,
state: "inactive", state: "inactive",
password: "base$123", user: {
roles: ['player'] firstName: "Hans",
lastName: "Zimmer",
dateOfBirth: "1956-11-23",
roles: ['player'],
auth: {
email: "base.ball@email.com",
password: "base$123"
}
}
} }
module.exports = { module.exports = {