BikePointDao.ts 1.18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import bikePointsData from '../../shared/data/bike-point-data.json'
import { IBikePoint } from '@entities/BikePoint'

const bikePoints = bikePointsData as IBikePoint[]

export interface IBikePointDao {
    getById: (bikePointId: string) => Promise<IBikePoint | null>
    getAll: (bikePointId: string) => Promise<IBikePoint[]>
}

class BikePointDao implements IBikePointDao {

    /**
     * @param bikePointId (without Prefix 'BikePoints_', only the numbers behind)
     */
    public getById(bikePointId: string): Promise<IBikePoint | null> {
        return Promise.resolve(getBikePointById(bikePoints, bikePointId))
    }

    public getAll(): Promise<IBikePoint[]> {
        return Promise.resolve(bikePoints.map(bikePoint => removeBikePointIdPrefix(bikePoint)))
    }
}

export const getBikePointById = (bikePoints: IBikePoint[], bikePointId: string) => {
    for (const bikePoint of bikePoints) {
        if (bikePoint.id.replace('BikePoints_', '') === bikePointId) {
            return removeBikePointIdPrefix(bikePoint)
        }
    }
    return null
}

const removeBikePointIdPrefix = (bikePoint: IBikePoint) => ({...bikePoint, id: bikePoint.id.replace('BikePoints_', '')})

export default BikePointDao