progress on team and player positions
This commit is contained in:
parent
8618533283
commit
35d8633567
|
|
@ -47,7 +47,15 @@ exports.findOne = (req, res) => {
|
|||
const id = req.params.id;
|
||||
|
||||
Player.findByPk(id, {
|
||||
include: { model: User }
|
||||
include: {
|
||||
model: User,
|
||||
as: 'user',
|
||||
include: {
|
||||
model: Auth,
|
||||
as: 'auth',
|
||||
attributes: ['email']
|
||||
}
|
||||
}
|
||||
}).then(data => {
|
||||
if (data) {
|
||||
res.send(data);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
@ -18,10 +18,10 @@ module.exports = {
|
|||
onDelete: 'CASCADE'
|
||||
},
|
||||
height: {
|
||||
type: Sequelize.FLOAT
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
weight: {
|
||||
type: Sequelize.FLOAT
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
gender: {
|
||||
allowNull: false,
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
'use strict';
|
||||
const { Model } = require('sequelize');
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
class PitchType extends Model {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,16 @@ module.exports = (sequelize, DataTypes) => {
|
|||
foreignKey: "userId",
|
||||
as: "user"
|
||||
});
|
||||
Player.belongsToMany(models.Team, {
|
||||
through: "PlayerTeams",
|
||||
foreignKey: 'playerId',
|
||||
otherKey: 'teamId',
|
||||
as: 'teams',
|
||||
});
|
||||
Player.hasMany(models.Position, {
|
||||
through: "PlayerPositions",
|
||||
as: 'positions',
|
||||
})
|
||||
}
|
||||
}
|
||||
Player.init({
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
};
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
@ -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');
|
||||
}
|
||||
};
|
||||
|
|
@ -1,17 +1,21 @@
|
|||
const signupPlayer = {
|
||||
firstName: "Hans",
|
||||
lastName: "Zimmer",
|
||||
dateOfBirth: "1956-11-23",
|
||||
email: "base.ball@email.com",
|
||||
height: 183.3,
|
||||
weight: 83.3,
|
||||
height: 183,
|
||||
weight: 83,
|
||||
gender: "male",
|
||||
bats: "right",
|
||||
throws: "left",
|
||||
jerseyNumber: 13,
|
||||
state: "inactive",
|
||||
password: "base$123",
|
||||
roles: ['player']
|
||||
user: {
|
||||
firstName: "Hans",
|
||||
lastName: "Zimmer",
|
||||
dateOfBirth: "1956-11-23",
|
||||
roles: ['player'],
|
||||
auth: {
|
||||
email: "base.ball@email.com",
|
||||
password: "base$123"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
|
|
|||
Loading…
Reference in New Issue