Skip to content

Commit

Permalink
Merge pull request #211 from Arquisoft/api_rest
Browse files Browse the repository at this point in the history
Endpoints names updated to follow REST APIs conventions
  • Loading branch information
UO287747 authored May 7, 2024
2 parents a12a1ea + 0f8e251 commit 97dce24
Show file tree
Hide file tree
Showing 28 changed files with 227 additions and 180 deletions.
10 changes: 5 additions & 5 deletions gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const validateRequiredFields = (req, fields) => {
};

// Ruta para agregar un nuevo juego
app.post('/addgame', async (req, res) => {
app.post('/games', async (req, res) => {
try {
validateRequiredFields(req, ['userId', 'pAcertadas', 'pFalladas', 'totalTime', 'gameMode']);

Expand Down Expand Up @@ -80,7 +80,7 @@ app.get('/api/info/users', async (req, res) => {
let usersData=[];
if(username!=undefined){
try{
const user = await axios.get(`${USER_SERVICE_URL}/getUserInfo/${username}`)
const user = await axios.get(`${USER_SERVICE_URL}/users/${username}`)
if (!user.data) {
res.status(400).json({ error: 'User not found' });
return;
Expand All @@ -94,7 +94,7 @@ app.get('/api/info/users', async (req, res) => {
}
}else{
try {
const users = await axios.get(`${USER_SERVICE_URL}/getAllUsers`);
const users = await axios.get(`${USER_SERVICE_URL}/users`);
if(users.data)
users.data.forEach(user => {usersData.push(user)});
} catch (error) {
Expand Down Expand Up @@ -132,7 +132,7 @@ app.get('/api/info/users', async (req, res) => {
});

// Ruta para obtener datos de participación del usuario
app.get('/getParticipation/:userId', async (req, res) => {
app.get('/games/:userId', async (req, res) => {
try {
const userId = req.params.userId;

Expand Down Expand Up @@ -199,7 +199,7 @@ app.get('/api/ranking', async (req, res) => {
const rankedPlayers = [];
for (const entry of ranking) {
try {
const user = await axios.get(`${USER_SERVICE_URL}/getUserInfo/${entry._id}`);
const user = await axios.get(`${USER_SERVICE_URL}/users/${entry._id}`);
rankedPlayers.push({
user: user.data.username, // Puedes usar el campo apropiado según tu esquema de usuario
totalGames: entry.totalGames
Expand Down
10 changes: 5 additions & 5 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ afterEach(async () => {

describe('Game Service', () => {
// Test para agregar un nuevo juego con éxito
it('should add a new game on POST /addgame', async () => {
it('should add a new game on POST /games', async () => {
const newGame = {
userId: userId,
pAcertadas: 5,
Expand All @@ -57,7 +57,7 @@ describe('Game Service', () => {
gameMode: 'classic'
};

const response = await request(app).post('/addgame').send(newGame);
const response = await request(app).post('/games').send(newGame);
expect(response.status).toBe(200);
const data = response.body;
expect(data).toHaveProperty("user");
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('Game Service', () => {



const response = await request(app).get(`/getParticipation/${userId}`);
const response = await request(app).get(`/games/${userId}`);

expect(response.status).toBe(200);
expect(response.body).toEqual(mockParticipationData);
Expand All @@ -97,7 +97,7 @@ describe('Game Service', () => {
// Test para manejar el caso de usuario no encontrado al obtener los datos de participación
it('should return 404 when getting participation data for invalid user', async () => {
const nonExistentUserId = '';
const response = await request(app).get(`/getParticipation/${nonExistentUserId}`);
const response = await request(app).get(`/games/${nonExistentUserId}`);

expect(response.status).toBe(404);
});
Expand All @@ -109,7 +109,7 @@ describe('Game Service', () => {
});
const userNGId = userNoGames._id;

const response = await request(app).get(`/getParticipation/${userNGId}`);
const response = await request(app).get(`/games/${userNGId}`);

expect(response.status).toBe(204);
});
Expand Down
4 changes: 2 additions & 2 deletions gameservice/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ servers:
- url: 'http://localhost:8005'
- url: http://gameservice:8005
paths:
/getParticipation/{userId}:
/games/{userId}:
get:
summary: Get user participation data
parameters:
Expand All @@ -31,7 +31,7 @@ paths:
description: No participation data found for the user
'500':
description: Error occurred while retrieving participation data
/addgame:
/games:
post:
summary: Add a new game
requestBody:
Expand Down
31 changes: 14 additions & 17 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ app.get('/metrics', (req, res) => {
res.end(promClient.register.metrics());
});

app.delete('/deletefriend/:username/:friend', async (req, res, next) => {
app.delete('/friends/:friend_username', async (req, res, next) => {
if (req.headers.authorization) {
try{
const response = await axios.get(`${authServiceUrl}/verify`, {
Expand All @@ -83,7 +83,7 @@ app.delete('/deletefriend/:username/:friend', async (req, res, next) => {
}
});
// Security middleware
app.post(['/addgame','/addfriend'], async (req, res, next) => {
app.post(['/games','/friends'], async (req, res, next) => {
if (req.headers.authorization) {
try{
const response = await axios.get(`${authServiceUrl}/verify`, {
Expand Down Expand Up @@ -144,11 +144,11 @@ app.get('/verify', async (req, res) => {
});


app.post('/adduser', async (req, res) => {
app.post('/users', async (req, res) => {
try {
// Forward the add user request to the user service
try{
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
const userResponse = await axios.post(userServiceUrl+'/users', req.body);
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json(error.response.data);
Expand Down Expand Up @@ -232,11 +232,11 @@ app.get('/api/info/games', async (req, res) => {
});

// Ruta para agregar una nuevo amigo
app.post('/addgame', async (req, res) => {
app.post('/games', async (req, res) => {
try {
try{
// Forward the add game request to the games service
const gameResponse = await axios.post(gameServiceUrl + '/addgame', req.body);
const gameResponse = await axios.post(gameServiceUrl + '/games', req.body);
res.json(gameResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
Expand All @@ -247,11 +247,11 @@ app.post('/addgame', async (req, res) => {
});

// Ruta para agregar una nuevo game
app.post('/addfriend', async (req, res) => {
app.post('/friends', async (req, res) => {
try {
try{
// Forward the add game request to the games service
const friendsResponse = await axios.post(friendServiceUrl + '/addfriend', req.body);
const friendsResponse = await axios.post(friendServiceUrl + '/friends', req.body);
res.json(friendsResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
Expand All @@ -260,13 +260,10 @@ app.post('/addfriend', async (req, res) => {
res.status(500).json({ error: "Service down" });
}
});
app.delete('/deletefriend/:username/:friend', async (req, res) => {
app.delete('/friends/:friend_username', async (req, res) => {
try {
try{
if(req.body.user!==req.params.username){
throw new Error('Unauthorized');
}
const friendsResponse = await axios.delete(friendServiceUrl + '/deletefriend/'+req.body.user+'/'+req.params.friend);
const friendsResponse = await axios.delete(friendServiceUrl + '/friends/'+req.body.user.toString()+'/'+req.params.friend_username.toString());
res.json(friendsResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
Expand All @@ -275,10 +272,10 @@ app.delete('/deletefriend/:username/:friend', async (req, res) => {
res.status(500).json({ error: "Service down" });
}
});
app.get('/getFriends/:username', async (req, res) => {
app.get('/friends/:username', async (req, res) => {
try {
try{
const friendsResponse = await axios.get(friendServiceUrl + '/getFriends/'+req.params.username);
const friendsResponse = await axios.get(friendServiceUrl + '/friends/'+req.params.username.toString());
res.json(friendsResponse.data);
}catch(error){
res.status(error.response.status).json(error.response.data);
Expand All @@ -289,10 +286,10 @@ app.get('/getFriends/:username', async (req, res) => {
});

/// Ruta para obtener la participación del usuario
app.get('/getParticipation/:userId', async (req, res) => {
app.get('/games/:userId', async (req, res) => {
try {
const userId = req.params.userId;
const apiUrl = `${gameServiceUrl}/getParticipation/${userId}`;
const apiUrl = `${gameServiceUrl}/games/${userId}`;
try{
const gameResponse = await axios.get(apiUrl);
res.json(gameResponse.data);
Expand Down
Loading

0 comments on commit 97dce24

Please sign in to comment.