Skip to content
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
34 changes: 32 additions & 2 deletions src/controllers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { PelisCollection, Peli } from "./models";

type Options = {
id?: number;
search?: {
title?: string;
tag?: string;
};
};
class PelisController {
constructor() {}
model: PelisCollection;
constructor() {
this.model = new PelisCollection();
}

async get(opcion?: Options): Promise<Peli[]> {
if (opcion?.id) {
const peliBuscada = await this.model.getById(opcion.id);
return peliBuscada ? [peliBuscada] : [];
}
if (opcion?.search) {
return await this.model.search(opcion.search);
}
return await this.model.getAll();
}

async getOne(options: Options): Promise<Peli | undefined> {
const pelis = await this.get(options);
return pelis[0];
}

async add(peli: Peli) {
return await this.model.add(peli);
}
}
export { PelisController };
export { PelisController };
64 changes: 60 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,71 @@
import { PelisController } from "./controllers";
import minimist from "minimist";

function parseaParams(argv) {
const resultado = minimist(argv);
return {
action: resultado._[0],
params: {
id: resultado._[1] || resultado.id,
title: resultado.title,
tag: resultado.tag,
tags: resultado.tags,
},
_: resultado._
};
}


function proceso(params) {
const controller = new PelisController();

if (params._[0] === "add") {
const tags = Array.isArray(params.params.tags) ? params.params.tags : [params.params.tags];
const peliNueva = {
id: params.params.id,
title: params.params.title,
tags: tags,
};

controller.add(peliNueva).then((resultado) => {
console.log("Película agregada:", resultado);
});
}

if (params._[0] === "get") {
const objeto = {
id: params.params.id,
};
controller.get(objeto).then((respuesta) => {
console.log(respuesta);
});
}

return resultado;

if (params._[0] === "search") {
const objeto = {
search: {
title: params.params.title,
tag: params.params.tag,
},
};

controller.get(objeto).then((respuesta) => {
console.log(respuesta);
});
}

if (params._[0] === undefined) {
controller.get({}).then((respuesta) => {
console.log(respuesta);
});
}
}

function main() {
const params = parseaParams(process.argv.slice(2));

console.log(params);
proceso(params);
}

main();

main();
9 changes: 9 additions & 0 deletions src/jsonfileMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const data: any = {};

export const readFile = async (path: string) => {
return data[path] || [];
};

export const writeFile = async (path: string, content: any) => {
data[path] = content;
};
1 change: 1 addition & 0 deletions src/models.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import anyTest, { TestFn } from "ava";
import { PelisCollection, Peli } from "./models";
import * as jsonfile from "./jsonfileMock"; // Importa el mock

export const getRandomId = () => {
const randomNumber = Math.floor(Math.random() * 100000);
Expand Down
54 changes: 48 additions & 6 deletions src/models.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
import * as jsonfile from "jsonfile";
// El siguiente import no se usa pero es necesario
import "./pelis.json";
// de esta forma Typescript se entera que tiene que incluir
// el .json y pasarlo a la carpeta /dist
// si no, solo usandolo desde la libreria jsonfile, no se dá cuenta

// no modificar estas propiedades, agregar todas las que quieras
let jsonfile;
if (process.env.NODE_ENV === 'test') {
jsonfile = require('./jsonfileMock'); // Usar el mock
} else {
jsonfile = require('jsonfile'); // Usar la librería real
}
class Peli {
id: number;
title: string;
tags: string[];
}

type SearchOptions = { title?: string; tag?: string };
class PelisCollection {
getAll(): Promise<Peli[]> {
return jsonfile.readFile("...laRutaDelArchivo").then(() => {
// la respuesta de la promesa
return [];
listaPelis: Peli[] = [];

async getAll(): Promise<Peli[]> {
return await jsonfile.readFile("./src/pelis.json");
}

async getById(id: number): Promise<Peli | undefined> {
const pelis = await this.getAll();
const peliEncontrada = pelis.find((peli) => peli.id === id);
return peliEncontrada;
}

async add(peli: Peli): Promise<boolean> {
const peliExiste = await this.getById(peli.id);
if (peliExiste) return false;
const pelis = await this.getAll();

pelis.push(peli);

await jsonfile.writeFile("./src/pelis.json", pelis, { spaces: 2 });
return true;
}

async search(options: SearchOptions): Promise<Peli[]> {
const lista = await this.getAll();

const listaFiltrada = lista.filter((p) => {
let esteVa = true;
if (options.tag) {
esteVa = esteVa && p.tags.includes(options.tag);
}
if (options.title) {
// Cambié aquí para que sea case insensitive
esteVa = esteVa && p.title.toLowerCase().includes(options.title.toLowerCase());
}
return esteVa;
});

return listaFiltrada;
}

}
export { PelisCollection, Peli };

export { PelisCollection, Peli }
47 changes: 46 additions & 1 deletion src/pelis.json
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
[]
[
{
"id": 1,
"title": "inception",
"tags": [
"sci-fi",
"thriller",
"action"
]
},
{
"id": 2,
"title": "The Shawshank Redemption",
"tags": [
"thriller",
"drama",
"Crime"
]
},
{
"id": 3,
"title": "The Godfather",
"tags": [
"Crime",
"Drama",
"Classic"
]
},
{
"id": 4,
"title": "Jumanji",
"tags": [
"adventure",
"comedy",
"family"
]
},
{
"id": 4411,
"title": "Título de la nueva peli",
"tags": [
"action",
"classic"
]
}
]