diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 12036c07..3eb2b382 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -58,6 +58,22 @@ app.get('/questions', async (req, res) => { } }); +app.get('/questions/:lang/:amount/:type', async (req, res) => { + try { + const lang = req.params.lang; + const amount = req.params.amount; + const type = req.params.type; + // Forward the question request to the quetion service + const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount + '/' + type); + + res.json(questionResponse.data); + } catch (error) { + + res.status(error.response.status).json({ error: error.response.data.error }); + } +}); + + app.get('/questions/:lang/:amount', async (req, res) => { try { const lang = req.params.lang; diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 69a771dd..8a4b0469 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -24,6 +24,10 @@ describe('Gateway Service', () => { if (url.endsWith('/questions')){ return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?", answers: ["225089","272357","267855","231841"]}] }); + } else if (url.endsWith('/questions/es/1/CAPITAL')){ + return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?", + answers: ["225089","272357","267855","231841"]}] }); + } else if (url.endsWith('/questions/es/1')){ return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?", answers: ["225089","272357","267855","231841"]}] }); @@ -75,8 +79,8 @@ describe('Gateway Service', () => { expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?"); }); - // Test /questions/:lang/:amount endpoint - it('should forward questions request to question service', async () => { + // Test /questions/:lang/:amount endpoint + it('should forward questions request to question service', async () => { const response = await request(app) .get('/questions/es/1'); @@ -84,6 +88,15 @@ describe('Gateway Service', () => { expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?"); }); + // Test /questions/:lang/:amount/:type endpoint + it('should forward questions request to question service', async () => { + const response = await request(app) + .get('/questions/es/1/CAPITAL'); + + expect(response.statusCode).toBe(200); + expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?"); + }); + // Test /record endpoint it('should forward record request to record service', async () => { const response = await request(app) diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 9278f131..12f4c7ca 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -30,6 +30,34 @@ app.get('/questions', async (req, res) => { } }); +app.get('/questions/:lang/:amount/:type', async (req, res) => { + try { + const lang = req.params.lang.toString(); + let amount = parseInt(req.params.amount); + const type = req.params.type.toString(); + + if(amount > 20) + amount = 20; + + const questions = await Question.aggregate([ + {$match: {language : lang, type: type}}, //Condition + {$sample: {size:amount}} //5 random from the ones that fullfil the condition + ]); + + let jsonResult = {}; + for (let i = 0; i < questions.length; i++) { + const question = questions[i]; + jsonResult[i] = { + question : question.question, + answers : question.answers + } + } + res.json(jsonResult); + } catch (error) { + res.status(500).json({ error: 'Internal Server Error' }); + } +}); + app.get('/questions/:lang/:amount', async (req, res) => { try { const lang = req.params.lang; diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js index a3f51c1c..6f68c54d 100644 --- a/questionservice/question-service.test.js +++ b/questionservice/question-service.test.js @@ -76,4 +76,19 @@ describe('Question Service', () => { expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?"); expect(Object.keys(response.body).length).toBe(20); }); + + it('Should give 10 questions /questions/es/10/POPULATION', async () => { + + let response = await request(app).get('/questions/es/10/POPULATION'); + expect(response.status).toBe(200); + expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?"); + expect(Object.keys(response.body).length).toBe(10); + }); + + it('Should give 0 questions /questions/es/10/CAPITAL', async () => { + + let response = await request(app).get('/questions/es/10/CAPITAL'); + expect(response.status).toBe(200); + expect(Object.keys(response.body).length).toBe(0); + }); }); \ No newline at end of file