mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Merge pull request #40 from brendan-ch/feat/parking-data-repository
[INT-62] feat/parking-data-repository
This commit is contained in:
36
src/repositories/InMemoryParkingRepository.ts
Normal file
36
src/repositories/InMemoryParkingRepository.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { ParkingGetterSetterRepository } from "./ParkingGetterSetterRepository";
|
||||
import { IParkingStructure } from "../entities/ParkingRepositoryEntities";
|
||||
|
||||
export class InMemoryParkingRepository implements ParkingGetterSetterRepository {
|
||||
private structures: Map<string, IParkingStructure>;
|
||||
|
||||
constructor() {
|
||||
this.structures = new Map<string, IParkingStructure>();
|
||||
}
|
||||
|
||||
async addOrUpdateParkingStructure(structure: IParkingStructure): Promise<void> {
|
||||
this.structures.set(structure.id, { ...structure });
|
||||
}
|
||||
|
||||
async clearParkingStructureData(): Promise<void> {
|
||||
this.structures.clear();
|
||||
}
|
||||
|
||||
async getParkingStructureById(id: string): Promise<IParkingStructure | null> {
|
||||
const structure = this.structures.get(id);
|
||||
return structure ? { ...structure } : null;
|
||||
}
|
||||
|
||||
async getParkingStructures(): Promise<IParkingStructure[]> {
|
||||
return Array.from(this.structures.values()).map(structure => ({ ...structure }));
|
||||
}
|
||||
|
||||
async removeParkingStructureIfExists(id: string): Promise<IParkingStructure | null> {
|
||||
const structure = this.structures.get(id);
|
||||
if (structure) {
|
||||
this.structures.delete(id);
|
||||
return { ...structure };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
6
src/repositories/ParkingGetterRepository.ts
Normal file
6
src/repositories/ParkingGetterRepository.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { IParkingStructure } from "../entities/ParkingRepositoryEntities";
|
||||
|
||||
export interface ParkingGetterRepository {
|
||||
getParkingStructures(): Promise<IParkingStructure[]>;
|
||||
getParkingStructureById(id: string): Promise<IParkingStructure | null>;
|
||||
}
|
||||
10
src/repositories/ParkingGetterSetterRepository.ts
Normal file
10
src/repositories/ParkingGetterSetterRepository.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { IParkingStructure } from "../entities/ParkingRepositoryEntities";
|
||||
import { ParkingGetterRepository } from "./ParkingGetterRepository";
|
||||
|
||||
export interface ParkingGetterSetterRepository extends ParkingGetterRepository {
|
||||
addOrUpdateParkingStructure(structure: IParkingStructure): Promise<void>;
|
||||
|
||||
removeParkingStructureIfExists(id: string): Promise<IParkingStructure | null>;
|
||||
|
||||
clearParkingStructureData(): Promise<void>;
|
||||
}
|
||||
105
test/repositories/InMemoryParkingRepositoryTests.test.ts
Normal file
105
test/repositories/InMemoryParkingRepositoryTests.test.ts
Normal file
@@ -0,0 +1,105 @@
|
||||
import { beforeEach, describe, expect, it } from "@jest/globals";
|
||||
import { InMemoryParkingRepository } from "../../src/repositories/InMemoryParkingRepository";
|
||||
import { IParkingStructure } from "../../src/entities/ParkingRepositoryEntities";
|
||||
|
||||
describe("InMemoryParkingRepository", () => {
|
||||
let repository: InMemoryParkingRepository;
|
||||
const testStructure: IParkingStructure = {
|
||||
coordinates: {
|
||||
latitude: 33.794795,
|
||||
longitude: -117.850807,
|
||||
},
|
||||
spotsAvailable: 0,
|
||||
id: "1",
|
||||
name: "Anderson Parking Structure",
|
||||
capacity: 100,
|
||||
address: "300 E Walnut Ave, Orange, CA 92867"
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
repository = new InMemoryParkingRepository();
|
||||
});
|
||||
|
||||
describe("addOrUpdateParkingStructure", () => {
|
||||
it("should add a new parking structure", async () => {
|
||||
await repository.addOrUpdateParkingStructure(testStructure);
|
||||
const result = await repository.getParkingStructureById(testStructure.id);
|
||||
expect(result).toEqual(testStructure);
|
||||
});
|
||||
|
||||
it("should update existing parking structure", async () => {
|
||||
await repository.addOrUpdateParkingStructure(testStructure);
|
||||
const updatedStructure = { ...testStructure, name: "Updated Garage" };
|
||||
await repository.addOrUpdateParkingStructure(updatedStructure);
|
||||
const result = await repository.getParkingStructureById(testStructure.id);
|
||||
expect(result).toEqual(updatedStructure);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removeParkingStructureIfExists", () => {
|
||||
it("should remove existing parking structure and return it", async () => {
|
||||
await repository.addOrUpdateParkingStructure(testStructure);
|
||||
const removed = await repository.removeParkingStructureIfExists(testStructure.id);
|
||||
expect(removed).toEqual(testStructure);
|
||||
const result = await repository.getParkingStructureById(testStructure.id);
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return null when removing non-existent structure", async () => {
|
||||
const result = await repository.removeParkingStructureIfExists("non-existent");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("clearParkingStructureData", () => {
|
||||
it("should remove all parking structures", async () => {
|
||||
const structures = [
|
||||
testStructure,
|
||||
{ ...testStructure, id: "test-id-2", name: "Second Garage" }
|
||||
];
|
||||
|
||||
for (const structure of structures) {
|
||||
await repository.addOrUpdateParkingStructure(structure);
|
||||
}
|
||||
|
||||
await repository.clearParkingStructureData();
|
||||
const result = await repository.getParkingStructures();
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getParkingStructures", () => {
|
||||
it("should return empty array when no structures exist", async () => {
|
||||
const result = await repository.getParkingStructures();
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it("should return all added structures", async () => {
|
||||
const structures = [
|
||||
testStructure,
|
||||
{ ...testStructure, id: "test-id-2", name: "Second Garage" }
|
||||
];
|
||||
|
||||
for (const structure of structures) {
|
||||
await repository.addOrUpdateParkingStructure(structure);
|
||||
}
|
||||
|
||||
const result = await repository.getParkingStructures();
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result).toEqual(expect.arrayContaining(structures));
|
||||
});
|
||||
});
|
||||
|
||||
describe("getParkingStructureById", () => {
|
||||
it("should return null for non-existent structure", async () => {
|
||||
const result = await repository.getParkingStructureById("non-existent");
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return structure by id", async () => {
|
||||
await repository.addOrUpdateParkingStructure(testStructure);
|
||||
const result = await repository.getParkingStructureById(testStructure.id);
|
||||
expect(result).toEqual(testStructure);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user