added route to retrieve player by user id

This commit is contained in:
Sascha Kühl 2025-05-25 21:01:56 +02:00
parent eca0126f45
commit dec7922727
3 changed files with 30 additions and 6 deletions

View File

@ -34,9 +34,7 @@ exports.findAll = (req, res) => {
Player.findAll({
where: condition,
include: {
model: User
}
include: { model: User }
}).then(data => {
res.send(data);
}).catch(err => {
@ -48,9 +46,7 @@ exports.findOne = (req, res) => {
const id = req.params.id;
Player.findByPk(id, {
include: {
model: User
}
include: { model: User }
}).then(data => {
if (data) {
res.send(data);
@ -61,3 +57,20 @@ exports.findOne = (req, res) => {
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 }
}).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}).`});
});
}

View File

@ -22,4 +22,8 @@ module.exports = function(app) {
"/api/players/:id",
[authJwt.verifyToken, authJwt.isCoachOrAdmin],
controller.findOne);
app.get(
"/api/players/user/:userId",
[authJwt.verifyToken, authJwt.isCoachOrAdmin],
controller.findOneByUserId);
};

View File

@ -34,6 +34,13 @@ describe("Test player creation, authentication and retrieval", () => {
expect(response.statusCode).toBe(200);
expect(response.body).toEqual(player);
response = await request(app)
.get(`/api/players/user/${player.userId}`)
.set('x-access-token', user.accessToken);
expect(response.header['content-type']).toBe('application/json; charset=utf-8');
expect(response.statusCode).toBe(200);
expect(response.body).toEqual(player);
response = await request(app)
.get('/api/players')
.set('x-access-token', user.accessToken);