Created
April 24, 2021 14:47
-
-
Save PabloMur/aed03dab9e8608f16f323294287122be to your computer and use it in GitHub Desktop.
desa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PelisCollection, Peli } from "./models"; | |
class PelisController { | |
pelis: PelisCollection; | |
promesa: Promise<any>; | |
constructor() { | |
this.pelis = new PelisCollection(); | |
let promesa = this.pelis.getAll(); | |
this.promesa = promesa; | |
} | |
get(options: any) { | |
if (options.action == "esta vacio") { | |
return this.pelis.getAll(); | |
} | |
if (options.action == "get") { | |
return this.pelis.getById(options.id); | |
} | |
if (options.action == "search" && options.params == "title") { | |
return this.pelis.getAll().then(() => { | |
return this.pelis.search(options.params, options.do); | |
}); | |
} | |
if (options.action == "search" && options.params == "tags") { | |
return this.pelis.getAll().then(() => { | |
return this.pelis.search(options.params, options.do); | |
}); | |
} | |
} | |
add(options: any) { | |
let peli = new Peli(); | |
peli.id = options.id; | |
peli.title = options.title; | |
peli.tags = options.tags; | |
return this.pelis.add(peli); | |
} | |
} | |
export { PelisController }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PelisController } from "./controllers"; | |
import * as minimist from "minimist"; | |
import * as vacio from "lodash/isEmpty"; | |
function parseaParams(argv) { | |
const resultado = minimist(argv); | |
//el "_" de resultado representa el array que genera minimist dentro del objeto respuesta | |
let controller = new PelisController(); | |
if (vacio(resultado._)) { | |
let options = { | |
action: "esta vacio", | |
}; | |
return options; | |
} | |
if (resultado._.includes("get")) { | |
let options = { | |
action: "get", | |
id: resultado._[1], | |
}; | |
return options; | |
} | |
if (resultado._.includes("search") && resultado.title) { | |
let options = { | |
action: "search", | |
params: "title", | |
do: resultado.title, | |
}; | |
return options; | |
} | |
if (resultado._.includes("search") && resultado.tag) { | |
let options = { | |
action: "search", | |
params: "tags", | |
do: resultado.tag, | |
}; | |
return options; | |
} | |
if (resultado._[0] == "add") { | |
let options = { | |
action: "add", | |
peli: { | |
id: resultado.id, | |
title: resultado.title, | |
tags: resultado.tags, | |
}, | |
}; | |
return options; | |
} | |
} | |
function main() { | |
const collection = new PelisController(); | |
collection.promesa.then(() => { | |
const res = parseaParams(process.argv.slice(2)); | |
if (res.action !== "add") { | |
collection.get(res).then((r) => { | |
console.log(r); | |
}); | |
} else { | |
collection.add(res).then((r) => { | |
console.log(r); | |
}); | |
} | |
}); | |
} | |
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as jsonfile from "jsonfile"; | |
import * as concat from "lodash/concat"; | |
import * as find from "lodash/find"; | |
class Peli { | |
id: number; | |
title: string; | |
tags: string[]; | |
} | |
class PelisCollection { | |
peliculas: Peli[] = []; | |
promesa: Promise<any>; | |
getAll(): Promise<any> { | |
return jsonfile.readFile("./pelis.json").then((pelis) => { | |
this.peliculas = pelis; | |
return this.peliculas; | |
}); | |
} | |
getById(id: number): Promise<any> { | |
return this.getAll().then((p) => { | |
return find(p, { id: id }); | |
}); | |
} | |
search(action, objetivo): Promise<any> { | |
if (action == "title") { | |
return this.getAll().then((res) => { | |
return find(res, { title: objetivo }); | |
}); | |
} | |
if (action == "tags") { | |
return this.getAll().then((pelis) => { | |
return pelis.filter((p) => { | |
return p.tags.includes(objetivo); | |
}); | |
}); | |
} | |
} | |
//mètodo en proceso | |
add(peli: Peli): Promise<boolean> { | |
const promesaUno = this.getById(peli.id).then((peliExistente) => { | |
if (peliExistente) { | |
console.log("ya existe"); | |
return false; | |
} else { | |
// magia que agrega la pelicula a un objeto data | |
concat(this.peliculas, peli); | |
const promesaDos = jsonfile.writeFile("./pelis.json", this.peliculas); | |
return promesaDos.then(() => { | |
console.log("peli agregada correctamente"); | |
return true; | |
}); | |
} | |
}); | |
return promesaUno; | |
} | |
} | |
export { PelisCollection, Peli }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":123,"title":"die hard","tags":["action","guns","violence"]},{"id":568,"title":"crazy marie","tags":["comedy","romantic comedy","drama"]},{"id":3241,"title":"what?","tags":["dark","thriller","horror"]},{"id":4411,"title":"Título de la nueva peli","tags":["action","classic"]},{"id":4321865,"title":"peli de la terminal 4321865","tags":["rr","ww"]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment