diff --git a/pelis.json b/pelis.json new file mode 100644 index 00000000..80139c8c --- /dev/null +++ b/pelis.json @@ -0,0 +1 @@ +[{"id":1,"title":"Matrix","tags":["action","classic"]},{"id":2,"title":"Titanic","tags":["drama","romance"]},{"id":3,"title":"Shrek","tags":["animation","comedy"]},{"id":4,"title":"Inception","tags":["sci-fi","thriller"]},{"id":219023,"title":"title 220623219023","tags":["tt","rr"]},{"id":134101,"title":"title 155745134101","tags":["tt","rr"]},{"id":146272,"title":"title 220623146272","tags":["yy","uu"]},{"id":201411,"title":"title 155745201411","tags":["yy","uu"]},{"id":198933,"title":"una peli 198933","tags":["classic","tag 198933"]},{"id":206432,"title":"otra peli un poco más divertida","tags":["tag 198933"]},{"id":4411,"title":"Título de la nueva peli","tags":["action","classic"]}] diff --git a/src/controllers.ts b/src/controllers.ts index cd5aa0a1..bfd1d350 100644 --- a/src/controllers.ts +++ b/src/controllers.ts @@ -1,6 +1,44 @@ import { PelisCollection, Peli } from "./models"; -class PelisController { - constructor() {} +export type Options = { + search?: { + title?: string; + tag?: string; + }; + id?: number; +}; + +export class PelisController { + collection: PelisCollection; + + constructor() { + this.collection = new PelisCollection(); + } + + async add(peli: Peli): Promise { + return this.collection.add(peli); + } + + async get(options?: Options): Promise { + if (!options || (!options.search && !options.id)) { + return this.collection.getAll(); + } + + if (options.search) { + return this.collection.search(options.search); + } + + if (options.id !== undefined) { + const peli = await this.collection.getById(options.id); + return peli ? [peli] : []; + } + + return []; + } + + async getOne(options: { id: number }): Promise { + const peli = await this.collection.getById(options.id); + if (!peli) throw new Error("Peli no encontrada"); + return peli; + } } -export { PelisController }; diff --git a/src/index.ts b/src/index.ts index 5270ef60..cee348cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,61 @@ import minimist from "minimist"; +import { PelisController, Options } from "./controllers"; +import { Peli } from "./models"; -function parseaParams(argv) { +// Función que parsea los argumentos de la terminal +function parseaParams(argv: string[]) { const resultado = minimist(argv); - return resultado; } -function main() { +async function main() { const params = parseaParams(process.argv.slice(2)); + const controller = new PelisController(); + + const comando = params._[0]; // Primer argumento posicional + + switch (comando) { + case "add": + const nuevaPeli: Peli = { + id: Number(params.id), + title: params.title, + tags: Array.isArray(params.tags) ? params.tags : [params.tags], + }; + const agregada = await controller.add(nuevaPeli); + console.log( + agregada + ? "Peli agregada correctamente" + : "No se pudo agregar la peli (ID duplicado)" + ); + break; + + case "get": + if (params._[1]) { + const id = Number(params._[1]); + const peliPorId = await controller.getOne({ id }); + console.log(peliPorId); + } else { + const todas = await controller.get(); + console.log(todas); + } + break; + + case "search": + const opcionesSearch: Options = { + search: { + title: params.title, + tag: params.tag, + }, + }; + const resultados = await controller.get(opcionesSearch); + console.log(resultados); + break; - console.log(params); + default: + const todas = await controller.get(); + console.log(todas); + break; + } } main(); diff --git a/src/models.ts b/src/models.ts index 12715038..b6dab01c 100644 --- a/src/models.ts +++ b/src/models.ts @@ -1,23 +1,44 @@ -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 +import jsonfile from "jsonfile"; -// no modificar estas propiedades, agregar todas las que quieras -class Peli { +export type Peli = { id: number; title: string; tags: string[]; -} +}; + +export class PelisCollection { + async getAll(): Promise { + try { + const pelis: Peli[] = await jsonfile.readFile("./pelis.json"); + return pelis; + } catch { + return []; // Si no existe o está vacío + } + } + + async add(peli: Peli): Promise { + const all = await this.getAll(); + if (all.find(p => p.id === peli.id)) { + return false; // No se permite ID duplicado + } + all.push(peli); + await jsonfile.writeFile("./pelis.json", all); + return true; + } + + async getById(id: number): Promise { + const all = await this.getAll(); + return all.find(p => p.id === id); + } -class PelisCollection { - getAll(): Promise { - return jsonfile.readFile("...laRutaDelArchivo").then(() => { - // la respuesta de la promesa - return []; - }); + async search(options: { title?: string; tag?: string }): Promise { + let all = await this.getAll(); + if (options.title) { + all = all.filter(p => p.title.includes(options.title!)); + } + if (options.tag) { + all = all.filter(p => p.tags.includes(options.tag!)); + } + return all; } } -export { PelisCollection, Peli }; diff --git a/src/pelis.json b/src/pelis.json deleted file mode 100644 index fe51488c..00000000 --- a/src/pelis.json +++ /dev/null @@ -1 +0,0 @@ -[]