added example files

This commit is contained in:
2018-02-18 00:18:16 +01:00
parent 3654c08bc2
commit e5ae0c75cf
17 changed files with 342 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
import { Track } from "../../domain/models/Track";
import { IMusicRepository } from "../../domain/ports/IMusicRepository";
export class VinylCatalog implements IMusicRepository {
private vinylList: Track[] = new Array(
new Track(1, "DNA.", "Kendrick Lamar", 340),
new Track(2, "Come Down", "Anderson Paak.", 430),
new Track(3, "DNA.", "Kendrick Lamar", 340),
new Track(4, "DNA.", "Kendrick Lamar", 340),
new Track(5, "DNA.", "Kendrick Lamar", 340)
);
get(): Track[] {
return this.vinylList;
}
getById(id: number): Track {
return this.vinylList.filter(track => track.Id == id).pop();
}
add(track: Track): number {
return this.vinylList.push(track);
}
edit(id: number, track: Track): Track {
var existingTrack = this.getById(id);
existingTrack.Artist = track.Artist;
existingTrack.Title = track.Title;
existingTrack.Duration = track.Duration;
return existingTrack;
}
delete(id: number): Track {
var track = this.getById(id);
if (track) {
var targetIndex = this.vinylList.indexOf(track);
if (targetIndex < -1) return null;
return this.vinylList.splice(targetIndex, 1)[0];
}
}
}

View File

@@ -0,0 +1,4 @@
export interface IPortProvider {
}

View File

@@ -0,0 +1,15 @@
export class Track{
constructor(id: number, title: string, artist: string, duration:number){
this.Id= id;
this.Title= title;
this.Artist= artist;
this.Duration= duration;
}
public Id : number;
public Title: string;
public Artist: string;
public Duration: number;
}

View File

@@ -0,0 +1,10 @@
import { Track } from "../models/Track";
export interface IMusicRepository {
get() : Track[];
getById(id: number) : Track;
add(track: Track) : number;
edit(id: number, track: Track) : Track;
delete(id: number) : Track;
}

View File

@@ -0,0 +1,27 @@
import { IMusicRepository } from "../../domain/ports/IMusicRepository";
import { Track } from "../../domain/models/Track";
export class MusicCatalogService {
private repository: IMusicRepository;
constructor(repository:IMusicRepository){
this.repository= repository;
}
get(): Track[] {
return this.repository.get();
}
getById(id: number): Track {
return this.repository.getById(id);
}
add(track: Track): number {
return this.repository.add(track);
}
edit(id: number, track: Track): Track {
return this.repository.edit(id, track);
}
delete(id: number): Track {
return this.repository.delete(id);
}
}

View File

@@ -0,0 +1,10 @@
import { MusicComponent } from "./MusicComponent";
describe("MusicComponent", () => {
it('should create a new MusicComponent', (done) => {
let musicComponent = new MusicComponent();
done();
});
});

View File

@@ -0,0 +1,23 @@
import { MusicCatalogService } from "../domain/services/MusicCatalogService"
import { IMusicRepository } from "../domain/ports/IMusicRepository";
import { VinylCatalog } from "../adapters/music/VinylCatalog"
export class MusicComponent {
constructor() {
let container = {
IMusicRepository: () => new VinylCatalog()
};
let inject = (name : string) : any => {
if (container[name]) {
return container[name];
}
throw new Error(`Failed to resolve ${name}`)
};
let musicCatalogService = new MusicCatalogService(inject("IMusicRepository") as IMusicRepository);
}
}