added ability to revert (#21)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user