Skip to content

Commit

Permalink
feat: 🎸 stubs connectors
Browse files Browse the repository at this point in the history
✅ Closes: #16
  • Loading branch information
mfw78 authored and kostysh committed Jun 6, 2022
1 parent 6692c2c commit 1708e8c
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/repositories/SpaceStubRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import DBService, {
FormattedDate,
FacilityItemValues,
LevelDefaultTyping,
DBLevel,
FacilityValues,
SpaceStubKey,
SpaceStubValues
} from '../services/DBService';
import { AbstractSublevel } from 'abstract-level';

export class SpaceStubRepository {
private dbService: DBService;
private db: AbstractSublevel<
AbstractSublevel<
AbstractSublevel<DBLevel, LevelDefaultTyping, string, FacilityValues>,
LevelDefaultTyping,
string,
FacilityItemValues
>,
LevelDefaultTyping,
SpaceStubKey,
SpaceStubValues
>;

constructor(facilityId: string, spaceId: string) {
this.dbService = DBService.getInstance();
this.db = this.dbService.getSpaceStubsDB(facilityId, spaceId);
}

// --- daily index management

public async getIndex(idx: FormattedDate): Promise<string[]> {
try {
return await this.db.get<FormattedDate, string[]>(idx, {
valueEncoding: 'json'
});
} catch (e) {
if (e.status !== 404) {
throw e;
}
}
return [];
}

public async addToIndex(idx: FormattedDate, stubId: string): Promise<void> {
const stubIds = await this.getIndex(idx);

if (stubIds.length > 0) {
const ids = new Set<string>(stubIds);
ids.add(stubId);
await this.db.put(idx, Array.from(ids));
} else {
await this.db.put(idx, [stubId]);
}
}

public async delFromIndex(idx: FormattedDate, itemId: string): Promise<void> {
const stubIds = await this.getIndex(idx);

if (stubIds.length > 0) {
const ids = new Set<string>(stubIds);
if (ids.delete(itemId)) {
await this.db.put(idx, Array.from(ids));
}
}
}

// --- num_booked getter / setter

public async getNumBookedByDate(key: SpaceStubKey): Promise<number> {
try {
return (await this.db.get(key)) as number;
} catch (e) {
if (e.status !== 404) {
throw e;
}
}

return 0;
}

public async setNumBookedByDate(
key: SpaceStubKey,
value: number
): Promise<void> {
await this.db.put(key, value);
}
}
80 changes: 80 additions & 0 deletions src/repositories/StubRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import DBService, {
FormattedDate,
LevelDefaultTyping,
DBLevel,
FacilityStubValues,
FacilityValues,
FacilityStubKey
} from '../services/DBService';
import { AbstractSublevel } from 'abstract-level';
import { StubStorage } from '../proto/lpms';

export class StubRepository {
private dbService: DBService;
private db: AbstractSublevel<
AbstractSublevel<DBLevel, LevelDefaultTyping, string, FacilityValues>,
LevelDefaultTyping,
string,
FacilityStubValues
>;

constructor(facilityId: string) {
this.dbService = DBService.getInstance();
this.db = this.dbService.getFacilityStubsDB(facilityId);
}

// --- availability getters / setters

public async getStub(key: FacilityStubKey): Promise<FacilityStubValues> {
try {
return await this.db.get(key);
} catch (e) {
if (e.status === 404) {
throw new Error(`Unable to get "${key}" of stub level"`);
}
throw e;
}
}

public async setStub(key: FacilityStubKey, stub: StubStorage): Promise<void> {
await this.db.put(key, stub);
}

// --- daily index management

public async getIndex(idx: FormattedDate): Promise<string[]> {
try {
return await this.db.get<FormattedDate, string[]>(idx, {
valueEncoding: 'json'
});
} catch (e) {
if (e.status !== 404) {
throw e;
}
}
return [];
}

public async addToIndex(idx: FormattedDate, stubId: string): Promise<void> {
const stubIds = await this.getIndex(idx);

if (stubIds.length > 0) {
const ids = new Set<string>(stubIds);
ids.add(stubId);
await this.db.put(idx, Array.from(ids));
} else {
await this.db.put(idx, [stubId]);
}
}

public async delFromIndex(idx: FormattedDate, itemId: string): Promise<void> {
const stubIds = await this.getIndex(idx);

if (stubIds.length > 0) {
const ids = new Set<string>(stubIds);
if (ids.delete(itemId)) {
await this.db.put(idx, Array.from(ids));
}
}
}
}

0 comments on commit 1708e8c

Please sign in to comment.