Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semana 4 > Actividad 4 > "Contribution Open Source" #161

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/controllers/ExplorerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,39 @@ const ExplorerService = require("../services/ExplorerService");
const FizzbuzzService = require("../services/FizzbuzzService");
const Reader = require("../utils/reader");

class ExplorerController{
static getExplorersByMission(mission){
class ExplorerController {
static getExplorersByMission(mission) {
const explorers = Reader.readJsonFile("explorers.json");
return ExplorerService.filterByMission(explorers, mission);
}

static applyFizzbuzz(score){
static applyFizzbuzz(score) {
return FizzbuzzService.applyValidationInNumber(score);
}

static getExplorersUsernamesByMission(mission){
static getExplorersUsernamesByMission(mission) {
const explorers = Reader.readJsonFile("explorers.json");
return ExplorerService.getExplorersUsernamesByMission(explorers, mission);
return ExplorerService.getExplorersUsernamesByMission(
explorers,
mission
);
}

static getExplorersAmonutByMission(mission){
static getExplorersAmonutByMission(mission) {
const explorers = Reader.readJsonFile("explorers.json");
return ExplorerService.getAmountOfExplorersByMission(explorers, mission);
return ExplorerService.getAmountOfExplorersByMission(
explorers,
mission
);
}

static getExplorerByStack(stack) {
const explorer = Reader.readJsonFile("explorers.json");
const explorerStack = ExplorerService.getExplorersUsernamesByStack(
explorer,
stack
);
return explorerStack;
}
}

Expand Down
34 changes: 27 additions & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,55 @@ const app = express();
app.use(express.json());
const port = 3000;


app.get("/", (request, response) => {
response.json({message: "FizzBuzz Api welcome!"});
response.json({ message: "FizzBuzz Api welcome!" });
});

app.get("/v1/explorers/:mission", (request, response) => {
const mission = request.params.mission;
const explorersInMission = ExplorerController.getExplorersByMission(mission);
const explorersInMission =
ExplorerController.getExplorersByMission(mission);
response.json(explorersInMission);
});

app.get("/v1/explorers/amount/:mission", (request, response) => {
const mission = request.params.mission;
const explorersAmountInMission = ExplorerController.getExplorersAmonutByMission(mission);
response.json({mission: request.params.mission, quantity: explorersAmountInMission});
const explorersAmountInMission =
ExplorerController.getExplorersAmonutByMission(mission);
response.json({
mission: request.params.mission,
quantity: explorersAmountInMission,
});
});

app.get("/v1/explorers/usernames/:mission", (request, response) => {
const mission = request.params.mission;
const explorersUsernames = ExplorerController.getExplorersUsernamesByMission(mission);
response.json({mission: request.params.mission, explorers: explorersUsernames});
const explorersUsernames =
ExplorerController.getExplorersUsernamesByMission(mission);
response.json({
mission: request.params.mission,
explorers: explorersUsernames,
});
});

app.get("/v1/fizzbuzz/:score", (request, response) => {
const score = parseInt(request.params.score);
const fizzbuzzTrick = ExplorerController.applyFizzbuzz(score);
response.json({score: score, trick: fizzbuzzTrick});
response.json({ score: score, trick: fizzbuzzTrick });
});

app.get("/v1/explorers/stack/:stack", (req, res) => {
const stack = req.params.stack;
const result = ExplorerController.getExplorerByStack(stack);
res.json({
stack: req.params.stack,
explorers: result,
});
});

app.listen(port, () => {
console.log(`FizzBuzz API in localhost:${port}`);
});

module.exports = app;
35 changes: 27 additions & 8 deletions lib/services/ExplorerService.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
class ExplorerService {

static filterByMission(explorers, mission){
const explorersByMission = explorers.filter((explorer) => explorer.mission == mission);
static filterByMission(explorers, mission) {
const explorersByMission = explorers.filter(
(explorer) => explorer.mission == mission
);
return explorersByMission;
}

static getAmountOfExplorersByMission(explorers, mission){
const explorersByMission = ExplorerService.filterByMission(explorers, mission);
static getAmountOfExplorersByMission(explorers, mission) {
const explorersByMission = ExplorerService.filterByMission(
explorers,
mission
);
return explorersByMission.length;
}

static getExplorersUsernamesByMission(explorers, mission){
const explorersByMission = ExplorerService.filterByMission(explorers, mission);
const explorersUsernames = explorersByMission.map((explorer) => explorer.githubUsername);
static getExplorersUsernamesByMission(explorers, mission) {
const explorersByMission = ExplorerService.filterByMission(
explorers,
mission
);
const explorersUsernames = explorersByMission.map(
(explorer) => explorer.githubUsername
);
return explorersUsernames;
}

static getExplorersUsernamesByStack(explorers, stack) {
const explorers_JSstack = explorers.filter((singleExplorer) =>
singleExplorer.stacks.includes(stack)
);

const usernames_JSstack = explorers_JSstack.map(
(singleExplorer_JSstack) => singleExplorer_JSstack.githubUsername
);
return usernames_JSstack;
}
}

module.exports = ExplorerService;
28 changes: 28 additions & 0 deletions test/server.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const request = require("supertest");
const app = require("./../lib/server");

describe("== Unit Testing for server.js ==", () => {
test("1.- Solicitud GET para '/' muestra 'status code' 200", async () => {
const response = await request(app).get("/").send();
expect(response.statusCode).toBe(200);
});

test("2.- Solicitud GET para '/v1/explorers/node' muestra 'status code' 200", async () => {
const response = await request(app).get("/v1/explorers/node").send();
expect(response.statusCode).toBe(200);
});

test("3.- Solicitud GET para '/v1/explorers/username/node' muestra 'status code' 200", async () => {
const response = await request(app).get("/v1/explorers/usernames/node").send();
expect(response.statusCode).toBe(200);
});

test("4.- Solicitud GET para '/v1/explorers/amount/node' muestra 'status code' 200", async () => {
const response = await request(app).get("/v1/explorers/amount/node").send();
expect(response.statusCode).toBe(200);
});
test("5.- Solicitud GET para '/v1/explorers/stack/javascript' muestra 'status code' 200", async () => {
const response = await request(app).get("/v1/explorers/stack/javascript").send();
expect(response.statusCode).toBe(200);
});
});
10 changes: 9 additions & 1 deletion test/services/ExplorerService.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const ExplorerService = require("./../../lib/services/ExplorerService");
const Reader = require("./../../lib/utils/Reader");
const ExplorerService = require("../../lib/services/ExplorerService");
const explorers = Reader.readJsonFile("explorers.json");

describe("Tests para ExplorerService", () => {
test("Requerimiento 1: Calcular todos los explorers en una misión", () => {
Expand All @@ -8,3 +10,9 @@ describe("Tests para ExplorerService", () => {
});

});


test("2.- Test for getExplorersUsernamesByStack", () => {
const exploresUsernamesByStack = ExplorerService.getExplorersUsernamesByStack(explorers, "javascript");
expect(exploresUsernamesByStack[0]).toBe("ajolonauta1");
});