flight-planner
Version:
Plan and route VFR flights
55 lines (54 loc) • 1.88 kB
JavaScript
/**
* In-memory implementation of the AircraftRepository.
* This is a simple implementation for testing or when you don't need persistence.
*/
export class InMemoryAircraftRepository {
aircraft = new Map();
async findByRegistration(registration) {
return this.aircraft.get(registration.toUpperCase()) || null;
}
async findByRegistrations(registrations) {
const results = [];
for (const registration of registrations) {
const aircraft = await this.findByRegistration(registration);
if (aircraft) {
results.push(aircraft);
}
}
return results;
}
async findAll() {
return Array.from(this.aircraft.values());
}
async create(aircraft) {
const key = aircraft.registration.toUpperCase();
if (this.aircraft.has(key)) {
throw new Error(`Aircraft with registration ${aircraft.registration} already exists`);
}
const normalizedAircraft = { ...aircraft, registration: key };
this.aircraft.set(key, normalizedAircraft);
return normalizedAircraft;
}
async update(aircraft) {
const key = aircraft.registration.toUpperCase();
if (!this.aircraft.has(key)) {
throw new Error(`Aircraft with registration ${aircraft.registration} not found`);
}
const normalizedAircraft = { ...aircraft, registration: key };
this.aircraft.set(key, normalizedAircraft);
return normalizedAircraft;
}
async delete(registration) {
return this.aircraft.delete(registration.toUpperCase());
}
async exists(registration) {
return this.aircraft.has(registration.toUpperCase());
}
/**
* Clear all aircraft from the repository.
* Useful for testing.
*/
clear() {
this.aircraft.clear();
}
}