-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ Closes: #16
- Loading branch information
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |