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

PR de contribución con nuevo endpoint de filtrado por stack #173

Open
wants to merge 4 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
38 changes: 22 additions & 16 deletions lib/controllers/ExplorerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@ const ExplorerService = require("../services/ExplorerService");
const FizzbuzzService = require("../services/FizzbuzzService");
const Reader = require("../utils/reader");

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

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

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

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

static getExplorersByStack(stack) {
const explorers = Reader.readJsonFile("explorers.json");
const resultado = ExplorerService.getExplorersByStack(explorers, stack);
return resultado;
}
}

module.exports = ExplorerController;
44 changes: 29 additions & 15 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,52 @@
const ExplorerController = require("./controllers/ExplorerController");
const express = require("express");
const { query } = require("express");
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);
response.json(explorersInMission);
const mission = request.params.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 mission = request.params.mission;
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 mission = request.params.mission;
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});
const score = parseInt(request.params.score);
const fizzbuzzTrick = ExplorerController.applyFizzbuzz(score);
response.json({ score: score, trick: fizzbuzzTrick });
});

app.listen(port, () => {
console.log(`FizzBuzz API in localhost:${port}`);
app.get("/v1/explorers/stack/:stack", (request, response) => {
const stack = request.params.stack;
const explorersByStack = ExplorerController.getExplorersByStack(stack);
response.json({ stack: stack, stacks: explorersByStack });
});

app.listen(port, () => {
console.log(`FizzBuzz API in localhost:${port}`);
});
51 changes: 38 additions & 13 deletions lib/services/ExplorerService.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
const { text } = require("express");

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

static filterByMission(explorers, mission){
const explorersByMission = explorers.filter((explorer) => explorer.mission == mission);
return explorersByMission;
}
static filterByStack(explorers, stack) {
const explorersByStack = explorers.filter((explorer) =>
explorer.stacks.includes(stack)
);
return explorersByStack;
}

static getAmountOfExplorersByMission(explorers, mission){
const explorersByMission = ExplorerService.filterByMission(explorers, mission);
return explorersByMission.length;
}
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);
return explorersUsernames;
}
static getExplorersUsernamesByMission(explorers, mission) {
const explorersByMission = ExplorerService.filterByMission(
explorers,
mission
);
const explorersUsernames = explorersByMission.map(
(explorer) => explorer.githubUsername
);
return explorersUsernames;
}

static getExplorersByStack(explorers, stack) {
const explorersByStack = ExplorerService.filterByStack(explorers, stack);
const explorersName = explorersByStack.map(
(explorer) => explorer.name + ": " + explorer.stacks
);
return explorersName;
}
}

module.exports = ExplorerService;
7 changes: 7 additions & 0 deletions test/controllers/ExplorerController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const ExplorerController = require("./../../lib/controllers/ExplorerController");
describe("Test para ExplorerController", () => {
test("1) Filtrar por stack", () => {
const respuesta = ExplorerController.getExplorersByStack("javascript");
expect(respuesta[0]).toMatch("javascript");
});
});