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
9 changes: 9 additions & 0 deletions reto_semana1/Artist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Artist{
constructor(id, country, songs){
this.id = id;
this.country = country;
this.songs = songs;
}
}

module.exports = Artist;
15 changes: 15 additions & 0 deletions reto_semana1/ArtistDao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Db = require("./Db.js");
const ArtistFactory = require("./ArtistFactory.js");

class ArtistDao{
constructor(){
this.artistDb = new Db();
this.artistFactory = new ArtistFactory();
}
persistArtist(artistType, params){
const artist = this.artistFactory.createArtist(artistType, params);
this.artistDb.items.push(artist);
}
}

module.exports = ArtistDao;
17 changes: 17 additions & 0 deletions reto_semana1/ArtistFactory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Band = require("./Band.js");
const Singer = require("./Singer.js");

class ArtistFactory{
constructor(){}
createArtist(artistType, params){
let singer = null;
if(artistType === 'singer'){
singer = new Singer(params.id, params.country, params.songs, params.firstName, params.lastName);
}else if(artistType === 'band'){
singer = new Band(params.id, params.country, params.songs, params.name);
}
return singer;
}
}

module.exports = ArtistFactory;
14 changes: 14 additions & 0 deletions reto_semana1/Band.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const Artist = require('./Artist.js');
class Band extends Artist{

constructor(id, country, songs, name){
super(id, country, songs);
this.name = name;
}
getName(){
return this.name;
}

}

module.exports = Band;
7 changes: 7 additions & 0 deletions reto_semana1/Db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Db{
constructor(){
this.items = [];
}
}

module.exports = Db;
17 changes: 17 additions & 0 deletions reto_semana1/Main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
const Spotify = require("./Spotify");

class Main{
constructor(){
this.spotify = new Spotify();
}

main(){
this.spotify.init();
this.spotify.createSomeArtists();
this.spotify.play();
}
}

let app = new Main();
app.main();
13 changes: 13 additions & 0 deletions reto_semana1/Singer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Artist = require('./Artist.js');
class Singer extends Artist{

constructor(id, country, songs, firstname, lastName){
super(id, country, songs);
this.firstname = firstname;
this.lastName = lastName;
}
getName(){
return (this.firstname + ' ' + this.lastName);
}
}
module.exports = Singer;
9 changes: 9 additions & 0 deletions reto_semana1/Song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Song{
constructor(id, name){
this.id = id;
this.name = name;
}

}

module.exports = Song;
51 changes: 51 additions & 0 deletions reto_semana1/Spotify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const ArtistDao = require("./ArtistDao.js");
const Song = require("./Song.js");

class Spotify{
constructor(){
this.artistDao = new ArtistDao();
this.stackArtist = [];
}
init(){
console.log("Welcome to Spotify ...");
}

createSomeArtists(){
console.log("Creating and persisting some artists");
let artist1Params = {
id: 1,
country: 'England',
name: 'Oasis',
songs: []
};
artist1Params.songs.push(new Song(1, 'Supersonic'));
artist1Params.songs.push(new Song(2, 'Bring it on down'));
this.artistDao.persistArtist('band', artist1Params);

let artist2Params = {
id: 2,
country: 'Peru',
firstName: 'Pedro',
lastName: 'Suarez',
songs: []
};
artist2Params.songs.push(new Song(3, 'Los globos del cielo'));
artist2Params.songs.push(new Song(4, 'Cuando pienses en volver'));
this.artistDao.persistArtist('singer', artist2Params);
}

play(){
console.log("Querying persisted artists..");
this.stackArtist = this.artistDao.artistDb.items;
console.log("Done");

for( let artist of this.stackArtist ){
console.log(`Current Artist: ${artist.getName()}`);
for(let song of artist.songs){
console.log(`Now playing ${song.name}`);
}
}
}
}

module.exports = Spotify;
95 changes: 95 additions & 0 deletions reto_semana2/api/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
const express = require('express');
const router = express.Router();
let db = require('../db_contacts.json');

router.route('/contacts')
.all( (req, res, next)=>{
console.log('all mw');
next();
})
.get((req, res)=>{
let viewDb = JSON.parse( JSON.stringify(db) );
let queryParams = req.query;
if( 'search' in queryParams ){
viewDb = viewDb.filter((contact)=>{
return contact.first_name.toLowerCase().includes(queryParams.search.toLowerCase()) ||
contact.last_name.toLowerCase().includes(queryParams.search.toLowerCase());
});
}
if( 'limit' in queryParams && 'page' in queryParams){
let limit = parseInt(queryParams.limit), page = parseInt(queryParams.page);
if( !isNaN( limit ) && !isNaN( page ) ){
const indexStart = limit * (page - 1);
viewDb = viewDb.splice(indexStart, limit);
}
}
res.json(viewDb);
})
.post((req, res)=>{
let newContact = req.body;
let foundedContact = db.find((contact) => contact.id === newContact.id );
if( foundedContact ){
res.status(500);
res.send(`id ${newContact.id} ya utilizado `);
}else{
db.push(newContact);
res.sendStatus(201);
}
})
.put((req, res)=>{
let replaceContact = req.body;
let foundedContact = db.find((contact) => contact.id === replaceContact.id );
if( !foundedContact ){
res.status(500);
res.send(`id ${replaceContact.id} no existe para reemplazar contacto`);
}else{
db = db.map( contact => {
if(contact.id === replaceContact.id) return replaceContact;
else return contact;
});
res.sendStatus(200);
}
})
.patch((req, res)=>{
let replaceContact = req.body;
let foundedContact = db.find((contact) => contact.id === replaceContact.id );
if( !foundedContact ){
res.status(500);
res.send(`id ${replaceContact.id} no existe para reemplazar atributos`);
}else{
db = db.map( contact => {
if(contact.id === replaceContact.id){
for(let key in replaceContact){
contact[key] = replaceContact[key];
}
return contact;
}else return contact;
});
res.sendStatus(200);
}
});

router.get('/contacts/:id', (req, res)=>{
res.json(db.find((user)=> +req.params.id === user.id ));
});

router.delete('/contacts/:id', (req, res)=>{
const deleteId = parseInt(req.params.id);
if( isNaN(deleteId) ){
res.status(500);
res.send('el id debe ser integer');
}else{
let foundedContact = db.find((contact) => contact.id === deleteId );
if( !foundedContact ){
res.status(404);
res.send(`id ${deleteId} no existe para eliminar`);
}else{
db = db.filter( contact => {
return contact.id !== deleteId;
});
res.sendStatus(200);
}
}
});

module.exports = router;
5 changes: 5 additions & 0 deletions reto_semana2/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const contacts = require('./contacts');

module.exports = {
contacts
};
Loading