working example
This commit is contained in:
@@ -14,26 +14,29 @@ export class VinylCatalog implements IMusicRepository {
|
||||
get(): Track[] {
|
||||
return this.vinylList;
|
||||
}
|
||||
getById(id: number): Track {
|
||||
return this.vinylList.filter(track => track.Id == id).pop();
|
||||
getById(id: number): Track | null {
|
||||
return this.vinylList.filter(track => track.Id == id).pop() as Track;
|
||||
}
|
||||
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;
|
||||
|
||||
if (existingTrack) {
|
||||
existingTrack.Artist = track.Artist;
|
||||
existingTrack.Title = track.Title;
|
||||
existingTrack.Duration = track.Duration;
|
||||
return existingTrack;
|
||||
}
|
||||
throw new Error("Track not found");
|
||||
}
|
||||
delete(id: number): Track {
|
||||
delete(id: number): Track | null {
|
||||
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];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
|
||||
export interface IPortProvider {
|
||||
|
||||
}
|
||||
@@ -3,8 +3,8 @@ import { Track } from "../models/Track";
|
||||
|
||||
export interface IMusicRepository {
|
||||
get() : Track[];
|
||||
getById(id: number) : Track;
|
||||
getById(id: number) : Track | null;
|
||||
add(track: Track) : number;
|
||||
edit(id: number, track: Track) : Track;
|
||||
delete(id: number) : Track;
|
||||
delete(id: number) : Track | null;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ export class MusicCatalogService {
|
||||
get(): Track[] {
|
||||
return this.repository.get();
|
||||
}
|
||||
getById(id: number): Track {
|
||||
getById(id: number): Track | null {
|
||||
return this.repository.getById(id);
|
||||
}
|
||||
add(track: Track): number {
|
||||
@@ -21,7 +21,7 @@ export class MusicCatalogService {
|
||||
edit(id: number, track: Track): Track {
|
||||
return this.repository.edit(id, track);
|
||||
}
|
||||
delete(id: number): Track {
|
||||
delete(id: number): Track | null {
|
||||
return this.repository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,9 @@ import { MusicComponent } from "./MusicComponent";
|
||||
describe("MusicComponent", () => {
|
||||
|
||||
it('should create a new MusicComponent', (done) => {
|
||||
console.log('Creating MusicComponent');
|
||||
let musicComponent = new MusicComponent();
|
||||
console.log('MusicComponent created!');
|
||||
done();
|
||||
});
|
||||
|
||||
|
||||
@@ -1,23 +1,37 @@
|
||||
|
||||
import { MusicCatalogService } from "../domain/services/MusicCatalogService"
|
||||
import { IMusicRepository } from "../domain/ports/IMusicRepository";
|
||||
|
||||
import { VinylCatalog } from "../adapters/music/VinylCatalog"
|
||||
import { Track } from "../domain/models/Track";
|
||||
import { VinylCatalog } from "../adapters/music/VinylCatalog";
|
||||
|
||||
export class MusicComponent {
|
||||
constructor() {
|
||||
|
||||
let container = {
|
||||
IMusicRepository: () => new VinylCatalog()
|
||||
let container : any = { };
|
||||
|
||||
let register = (name : string, fn : Function) => {
|
||||
container[name] = fn;
|
||||
};
|
||||
|
||||
let inject = (name : string) : any => {
|
||||
if (container[name]) {
|
||||
return container[name];
|
||||
return container[name]();
|
||||
}
|
||||
throw new Error(`Failed to resolve ${name}`)
|
||||
};
|
||||
let musicCatalogService = new MusicCatalogService(inject("IMusicRepository") as IMusicRepository);
|
||||
|
||||
console.log('registering IMusicRepository');
|
||||
register('IMusicRepository', () => new VinylCatalog());
|
||||
|
||||
let musicRepository = inject("IMusicRepository") as IMusicRepository;
|
||||
console.log('IMusicRepository is', musicRepository);
|
||||
|
||||
let musicCatalogService = new MusicCatalogService(musicRepository);
|
||||
|
||||
let track = new Track(8, "Niels 1st", "Niels Kooiman", 45);
|
||||
musicCatalogService.add(track);
|
||||
|
||||
let tracks = musicCatalogService.get();
|
||||
console.log(tracks);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user