added ability to revert (#21)

This commit is contained in:
undergroundwires
2020-07-15 19:04:56 +01:00
parent 57028987f1
commit 9c063d59de
58 changed files with 1448 additions and 265 deletions

View File

@@ -18,14 +18,24 @@ export class InMemoryRepository<TKey, TEntity extends IEntity<TKey>> implements
public addItem(item: TEntity): void {
if (!item) {
throw new Error('Item is null');
throw new Error('item is null or undefined');
}
if (this.exists(item)) {
if (this.exists(item.id)) {
throw new Error(`Cannot add (id: ${item.id}) as it is already exists`);
}
this.items.push(item);
}
public addOrUpdateItem(item: TEntity): void {
if (!item) {
throw new Error('item is null or undefined');
}
if (this.exists(item.id)) {
this.removeItem(item.id);
}
this.items.push(item);
}
public removeItem(id: TKey): void {
const index = this.items.findIndex((item) => item.id === id);
if (index === -1) {
@@ -34,8 +44,8 @@ export class InMemoryRepository<TKey, TEntity extends IEntity<TKey>> implements
this.items.splice(index, 1);
}
public exists(entity: TEntity): boolean {
const index = this.items.findIndex((item) => item.id === entity.id);
public exists(id: TKey): boolean {
const index = this.items.findIndex((item) => item.id === id);
return index !== -1;
}
}