mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Implement BaseRedisETARepository based on past implementation
This commit is contained in:
@@ -3,16 +3,66 @@ import { BaseRedisRepository } from "../../BaseRedisRepository";
|
||||
import { ETAGetterRepository, ETARepositoryEventListener, ETARepositoryEventName } from "./ETAGetterRepository";
|
||||
|
||||
export abstract class BaseRedisETARepository extends BaseRedisRepository implements ETAGetterRepository {
|
||||
getEtasForShuttleId(shuttleId: string): Promise<IEta[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
private static readonly ETA_KEY_PREFIX = 'shuttle:eta:';
|
||||
|
||||
// Helper methods
|
||||
private createEtaKey = (shuttleId: string, stopId: string) =>
|
||||
`${BaseRedisETARepository.ETA_KEY_PREFIX}${shuttleId}:${stopId}`;
|
||||
|
||||
createRedisHashFromEta = (eta: IEta): Record<string, string> => ({
|
||||
secondsRemaining: eta.secondsRemaining.toString(),
|
||||
shuttleId: eta.shuttleId,
|
||||
stopId: eta.stopId,
|
||||
systemId: eta.systemId,
|
||||
updatedTime: eta.updatedTime.toISOString(),
|
||||
});
|
||||
|
||||
createEtaFromRedisData = (data: Record<string, string>): IEta => ({
|
||||
secondsRemaining: parseFloat(data.secondsRemaining),
|
||||
shuttleId: data.shuttleId,
|
||||
stopId: data.stopId,
|
||||
systemId: data.systemId,
|
||||
updatedTime: new Date(data.updatedTime),
|
||||
});
|
||||
|
||||
// Getter implementations
|
||||
async getEtasForShuttleId(shuttleId: string): Promise<IEta[]> {
|
||||
const keys = await this.redisClient.keys(`${BaseRedisETARepository.ETA_KEY_PREFIX}${shuttleId}:*`);
|
||||
const etas: IEta[] = [];
|
||||
|
||||
for (const key of keys) {
|
||||
const data = await this.redisClient.hGetAll(key);
|
||||
if (Object.keys(data).length > 0) {
|
||||
etas.push(this.createEtaFromRedisData(data));
|
||||
}
|
||||
}
|
||||
|
||||
return etas;
|
||||
}
|
||||
|
||||
getEtasForStopId(stopId: string): Promise<IEta[]> {
|
||||
throw new Error("Method not implemented.");
|
||||
async getEtasForStopId(stopId: string): Promise<IEta[]> {
|
||||
const keys = await this.redisClient.keys(`${BaseRedisETARepository.ETA_KEY_PREFIX}*`);
|
||||
const etas: IEta[] = [];
|
||||
|
||||
for (const key of keys) {
|
||||
const data = await this.redisClient.hGetAll(key);
|
||||
if (Object.keys(data).length > 0 && data.stopId === stopId) {
|
||||
etas.push(this.createEtaFromRedisData(data));
|
||||
}
|
||||
}
|
||||
|
||||
return etas;
|
||||
}
|
||||
|
||||
getEtaForShuttleAndStopId(shuttleId: string, stopId: string): Promise<IEta | null> {
|
||||
throw new Error("Method not implemented.");
|
||||
async getEtaForShuttleAndStopId(shuttleId: string, stopId: string): Promise<IEta | null> {
|
||||
const key = this.createEtaKey(shuttleId, stopId);
|
||||
const data = await this.redisClient.hGetAll(key);
|
||||
|
||||
if (Object.keys(data).length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.createEtaFromRedisData(data);
|
||||
}
|
||||
|
||||
// EventEmitter override methods for type safety
|
||||
|
||||
Reference in New Issue
Block a user