Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/repositories/settlementRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,28 @@ describe("SettlementRepository", () => {
expect(repo.byAnchor("anchorA")).toHaveLength(2);
expect(repo.count()).toBe(3);
});

describe("remove", () => {
it("removes an existing settlement and returns true", () => {
const repo = new SettlementRepository();
const s = repo.create(draft("anchorA", 100));
expect(repo.count()).toBe(1);

const result = repo.remove(s.id);
expect(result).toBe(true);
expect(repo.get(s.id)).toBeUndefined();
expect(repo.count()).toBe(0);
expect(repo.all()).toHaveLength(0);
expect(repo.byAnchor("anchorA")).toHaveLength(0);
});

it("returns false when removing a non-existent id", () => {
const repo = new SettlementRepository();
repo.create(draft("anchorA", 100));

const result = repo.remove(999);
expect(result).toBe(false);
expect(repo.count()).toBe(1);
});
});
});
16 changes: 16 additions & 0 deletions src/repositories/settlementRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ export class SettlementRepository extends InMemoryRepository<number, Settlement>
return this.getByKey(id);
}

/** Removes a settlement, returning `true` if one existed. */
remove(id: number): boolean {
const existing = this.getByKey(id);
const existed = this.removeByKey(id);
if (existed && existing) {
const anchorSet = this.anchorIndex.get(existing.anchor);
if (anchorSet) {
anchorSet.delete(id);
if (anchorSet.size === 0) {
this.anchorIndex.delete(existing.anchor);
}
}
}
return existed;
}

/** Returns every settlement, most recent first. */
all(): Settlement[] {
return this.listAll().sort((a, b) => b.id - a.id);
Expand Down