Skip to content

Commit

Permalink
Added new endpoint to get an specific amount of questions of a specif…
Browse files Browse the repository at this point in the history
…ic type
  • Loading branch information
Mister-Mario committed Apr 15, 2024
1 parent 6b48eac commit aa34cf9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
16 changes: 16 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 15 additions & 2 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}] });
Expand Down Expand Up @@ -75,15 +79,24 @@ 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');

expect(response.statusCode).toBe(200);
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)
Expand Down
28 changes: 28 additions & 0 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit aa34cf9

Please sign in to comment.