working example

This commit is contained in:
2018-02-26 12:07:12 +01:00
parent e5ae0c75cf
commit 0cc84f5a58
17 changed files with 4526 additions and 130 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,4 @@
dist/
# ---> Node
# Logs
logs
@@ -27,4 +28,3 @@ build/Release
# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

View File

@@ -1,35 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Track_1 = require("../../domain/models/Track");
var VinylCatalog = /** @class */ (function () {
function VinylCatalog() {
this.vinylList = new Array(new Track_1.Track(1, "DNA.", "Kendrick Lamar", 340), new Track_1.Track(2, "Come Down", "Anderson Paak.", 430), new Track_1.Track(3, "DNA.", "Kendrick Lamar", 340), new Track_1.Track(4, "DNA.", "Kendrick Lamar", 340), new Track_1.Track(5, "DNA.", "Kendrick Lamar", 340));
}
VinylCatalog.prototype.get = function () {
return this.vinylList;
};
VinylCatalog.prototype.getById = function (id) {
return this.vinylList.filter(function (track) { return track.Id == id; }).pop();
};
VinylCatalog.prototype.add = function (track) {
return this.vinylList.push(track);
};
VinylCatalog.prototype.edit = function (id, track) {
var existingTrack = this.getById(id);
existingTrack.Artist = track.Artist;
existingTrack.Title = track.Title;
existingTrack.Duration = track.Duration;
return existingTrack;
};
VinylCatalog.prototype.delete = function (id) {
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 VinylCatalog;
}());
exports.VinylCatalog = VinylCatalog;

View File

@@ -1,2 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -1,12 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Track = /** @class */ (function () {
function Track(id, title, artist, duration) {
this.Id = id;
this.Title = title;
this.Artist = artist;
this.Duration = duration;
}
return Track;
}());
exports.Track = Track;

View File

@@ -1,2 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -1,24 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MusicCatalogService = /** @class */ (function () {
function MusicCatalogService(repository) {
this.repository = repository;
}
MusicCatalogService.prototype.get = function () {
return this.repository.get();
};
MusicCatalogService.prototype.getById = function (id) {
return this.repository.getById(id);
};
MusicCatalogService.prototype.add = function (track) {
return this.repository.add(track);
};
MusicCatalogService.prototype.edit = function (id, track) {
return this.repository.edit(id, track);
};
MusicCatalogService.prototype.delete = function (id) {
return this.repository.delete(id);
};
return MusicCatalogService;
}());
exports.MusicCatalogService = MusicCatalogService;

View File

@@ -1,20 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MusicCatalogService_1 = require("../domain/services/MusicCatalogService");
var VinylCatalog_1 = require("../adapters/music/VinylCatalog");
var MusicComponent = /** @class */ (function () {
function MusicComponent() {
var container = {
IMusicRepository: function () { return new VinylCatalog_1.VinylCatalog(); }
};
var inject = function (name) {
if (container[name]) {
return container[name];
}
throw new Error("Failed to resolve " + name);
};
var musicCatalogService = new MusicCatalogService_1.MusicCatalogService(inject("IMusicRepository"));
}
return MusicComponent;
}());
exports.MusicComponent = MusicComponent;

View File

@@ -1,9 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var MusicComponent_1 = require("./MusicComponent");
describe("MusicComponent", function () {
it('should create a new MusicComponent', function (done) {
var musicComponent = new MusicComponent_1.MusicComponent();
done();
});
});

View File

@@ -3,7 +3,7 @@ module.exports = function(config) {
config.set({
frameworks: ["jasmine", "karma-typescript"],
files: [
{ pattern: "src/**/*.spec.ts" }
{ pattern: "src/**/*.ts" }
],
karmaTypescriptConfig: {
compilerOptions: {

4485
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -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;
}
}

View File

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

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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();
});

View File

@@ -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);
}
}

View File

@@ -7,8 +7,8 @@
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
"declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./dist", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */