94 lines
2.7 KiB
JavaScript
94 lines
2.7 KiB
JavaScript
const db = require("../models/index");
|
|
const {registerUser} = require("../helper/user.helper");
|
|
const Player = db.Player;
|
|
const User = db.User;
|
|
const Auth = db.Auth;
|
|
const Op = db.Sequelize.Op;
|
|
|
|
exports.insert = (req, res) => {
|
|
registerUser(req.body.user).then(user => {
|
|
return Player.create({
|
|
firstName: req.body.firstName,
|
|
lastName: req.body.lastName,
|
|
dateOfBirth: req.body.dateOfBirth,
|
|
gender: req.body.gender,
|
|
height: req.body.height ? req.body.height : null,
|
|
weight: req.body.weight ? req.body.weight : null,
|
|
bats: req.body.bats ? req.body.bats : 'right',
|
|
throws: req.body.throws ? req.body.throws : 'right',
|
|
state: req.body.state ? req.body.state : 'inactive',
|
|
jerseyNumber: req.body.jerseyNumber,
|
|
userId: user.id
|
|
}).then(player => {
|
|
return player.reload({ include: { model: User }});
|
|
});
|
|
}).then(player => {
|
|
res.status(201).send(player);
|
|
}).catch(err => {
|
|
res.status(500).send({message: err.message});
|
|
});
|
|
};
|
|
|
|
exports.findAll = (req, res) => {
|
|
const title = req.query.title;
|
|
const condition = title ? {title: {[Op.iLike]: `%${title}%`}} : null;
|
|
|
|
Player.findAll({
|
|
where: condition,
|
|
include: ['user']
|
|
}).then(data => {
|
|
res.send(data);
|
|
}).catch(err => {
|
|
res.status(500).send({message:err.message || "Some error occurred while retrieving players."});
|
|
});
|
|
};
|
|
|
|
exports.findOne = (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
Player.findByPk(id, {
|
|
include: {
|
|
model: User,
|
|
as: 'user',
|
|
include: {
|
|
model: Auth,
|
|
as: 'auth',
|
|
attributes: ['email']
|
|
}
|
|
}
|
|
}).then(data => {
|
|
if (data) {
|
|
res.send(data);
|
|
} else {
|
|
res.status(404).send({message: `Cannot find user with id=${id}.`});
|
|
}
|
|
}).catch(err => {
|
|
res.status(500).send({message: `Error retrieving user with id=${id} (${err.message}).`});
|
|
});
|
|
}
|
|
|
|
exports.findOneByUserId = (req, res) => {
|
|
const userId = req.params.userId;
|
|
|
|
Player.findOne({
|
|
where: { userId: userId },
|
|
include: {
|
|
model: User,
|
|
as: 'user',
|
|
include: {
|
|
model: Auth,
|
|
as: 'auth',
|
|
attributes: ['email']
|
|
}
|
|
}
|
|
}).then(data => {
|
|
if (data) {
|
|
res.send(data);
|
|
} else {
|
|
res.status(404).send({message: `Cannot find player with user id=${userId}.`});
|
|
}
|
|
}).catch(err => {
|
|
res.status(500).send({message: `Error retrieving player with user id=${userId} (${err.message}).`});
|
|
});
|
|
}
|