Skip to content

Commit

Permalink
Added new endpoints for taking different sizes of questions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Mario committed Apr 14, 2024
1 parent 4153efc commit 6b48eac
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
14 changes: 14 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ app.get('/questions', async (req, res) => {
}
});

app.get('/questions/:lang/:amount', async (req, res) => {
try {
const lang = req.params.lang;
const amount = req.params.amount;
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount);

res.json(questionResponse.data);
} catch (error) {

res.status(error.response.status).json({ error: error.response.data.error });
}
});

app.get('/questions/:lang', async (req, res) => {
try {
const lang = req.params.lang;
Expand Down
12 changes: 12 additions & 0 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ 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')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
} else if (url.endsWith('/questions/es')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
Expand Down Expand Up @@ -72,6 +75,15 @@ 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 () => {
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 /record endpoint
it('should forward record request to record service', async () => {
const response = await request(app)
Expand Down
27 changes: 27 additions & 0 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ app.get('/questions', async (req, res) => {
}
});

app.get('/questions/:lang/:amount', async (req, res) => {
try {
const lang = req.params.lang;
let amount = parseInt(req.params.amount);

if(amount > 20)
amount = 20;

const questions = await Question.aggregate([
{$match: {language : lang}}, //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', async (req, res) => {
try {
const lang = req.params.lang;
Expand Down
18 changes: 17 additions & 1 deletion questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ beforeAll(async () => {
app = require('./question-service');

//Populate db
for(let i = 0; i < 6 ; i++){
for(let i = 0; i < 21 ; i++){
const question = new Question( {
question: "¿Cuál es la población de Oviedo?",
answers: [
Expand Down Expand Up @@ -60,4 +60,20 @@ describe('Question Service', () => {
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(5);
});

it('Should give 10 questions /questions/es/10', async () => {

let response = await request(app).get('/questions/es/10');
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 20 questions as the max is 20 /questions/es/21', async () => {

let response = await request(app).get('/questions/es/21');
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(20);
});
});

0 comments on commit 6b48eac

Please sign in to comment.