/
DataAccess.ts
66 lines (58 loc) · 1.8 KB
/
DataAccess.ts
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { ApiResponse, StApi } from '../Api';
import { favoritesCache } from '../Cache';
import { Location } from '../Geo';
export async function getFavorites(): Promise<string[]> {
const fromCache: string[] = await favoritesCache.getAll();
if (fromCache.length > 0) {
return fromCache;
}
return getFromApi();
}
export async function addPlaceToFavorites(id: string): Promise<void> {
await StApi.post('favorites', {
place_id: id
});
favoritesCache.set(id, id);
}
export async function addCustomPlaceToFavorites(
name: string,
location: Location,
address: string
): Promise<string> {
const apiResponse = await StApi.post('favorites/custom', {
name,
location,
address
});
return apiResponse.data.favorite.place_id;
}
export async function removePlaceFromFavorites(id: string): Promise<ApiResponse> {
const apiResponse: ApiResponse = await StApi.delete_('favorites', {
place_id: id
});
await removeFavoriteFromCache(id);
return apiResponse;
}
export async function shouldNotifyOnFavoritesUpdate(id: string): Promise<boolean> {
const favoriteFromCache: string = await favoritesCache.get(id);
if (favoriteFromCache) {
return false;
}
await getFromApi();
return true;
}
export async function isFavoriteInCache(id: string): Promise<boolean> {
return !!await favoritesCache.get(id);
}
export async function removeFavoriteFromCache(id: string): Promise<void> {
await favoritesCache.remove(id);
}
async function getFromApi(): Promise<string[]> {
const apiResponse: ApiResponse = await StApi.get('favorites');
if (!apiResponse.data.favorites) {
throw new Error('Wrong API response');
}
const favoriteIds = apiResponse.data.favorites.map((favoriteData) => (favoriteData.place_id));
await Promise.all(favoriteIds.map((favoriteId: string) => favoritesCache.set(favoriteId, favoriteId)));
return favoriteIds;
}