aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/api/Api.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/api/Api.tsx')
-rw-r--r--frontend/src/api/Api.tsx205
1 files changed, 30 insertions, 175 deletions
diff --git a/frontend/src/api/Api.tsx b/frontend/src/api/Api.tsx
index 30c0ad6..6731cb3 100644
--- a/frontend/src/api/Api.tsx
+++ b/frontend/src/api/Api.tsx
@@ -1,197 +1,52 @@
1import axios from 'axios';
2
3import { GameChapter, GamesChapters } from '../types/Chapters';
4import { Game } from '../types/Game';
5import { Map, MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map';
6import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content'; 1import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content';
7import { Search } from '../types/Search'; 2import { delete_token, get_token } from './Auth';
8import { UserProfile } from '../types/Profile'; 3import { get_user, get_profile, post_profile } from './User';
9import { Ranking } from '../types/Ranking'; 4import { get_games, get_chapters, get_games_chapters, get_game_maps, get_search } from './Games';
5import { get_official_rankings, get_unofficial_rankings } from './Rankings';
6import { get_map_summary, get_map_leaderboard, get_map_discussions, get_map_discussion, post_map_discussion, post_map_discussion_comment, delete_map_discussion } from './Maps';
7import { delete_map_summary, post_map_summary, put_map_image, put_map_summary } from './Mod';
10 8
11// add new api call function entries here 9// add new api call function entries here
12// example usage: API.get_games(); 10// example usage: API.get_games();
13export const API = { 11export const API = {
14 user_logout: () => user_logout(), 12 // Auth
15 13 get_token: () => get_token(),
14
15 delete_token: () => delete_token(),
16 // User
16 get_user: (user_id: string) => get_user(user_id), 17 get_user: (user_id: string) => get_user(user_id),
18 get_profile: (token: string) => get_profile(token),
19 post_profile: (token: string) => post_profile(token),
20 // Games
17 get_games: () => get_games(), 21 get_games: () => get_games(),
18
19 get_chapters: (chapter_id: string) => get_chapters(chapter_id), 22 get_chapters: (chapter_id: string) => get_chapters(chapter_id),
20 get_games_chapters: (game_id: string) => get_games_chapters(game_id), 23 get_games_chapters: (game_id: string) => get_games_chapters(game_id),
21 get_game_maps: (game_id: string) => get_game_maps(game_id), 24 get_game_maps: (game_id: string) => get_game_maps(game_id),
22 get_rankings: () => get_rankings(),
23 get_search: (q: string) => get_search(q), 25 get_search: (q: string) => get_search(q),
24 26 // Rankings
27 get_official_rankings: () => get_official_rankings(),
28 get_unofficial_rankings: () => get_unofficial_rankings(),
29 // Maps
25 get_map_summary: (map_id: string) => get_map_summary(map_id), 30 get_map_summary: (map_id: string) => get_map_summary(map_id),
26 get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id), 31 get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id),
27 get_map_discussions: (map_id: string) => get_map_discussions(map_id), 32 get_map_discussions: (map_id: string) => get_map_discussions(map_id),
28 get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id), 33 get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id),
29 34
30 post_map_summary: (map_id: string, content: ModMenuContent) => post_map_summary(map_id, content), 35 post_map_discussion: (token: string, map_id: string, content: MapDiscussionContent) => post_map_discussion(token, map_id, content),
31 post_map_discussion: (map_id: string, content: MapDiscussionContent) => post_map_discussion(map_id, content), 36 post_map_discussion_comment: (token: string, map_id: string, discussion_id: number, comment: string) => post_map_discussion_comment(token, map_id, discussion_id, comment),
32 post_map_discussion_comment: (map_id: string, discussion_id: number, content: MapDiscussionCommentContent) => post_map_discussion_comment(map_id, discussion_id, content),
33
34 put_map_image: (map_id: string, image: string) => put_map_image(map_id, image),
35 put_map_summary: (map_id: string, content: ModMenuContent) => put_map_summary(map_id, content),
36 37
37 delete_map_summary: (map_id: string, route_id: number) => delete_map_summary(map_id, route_id), 38 delete_map_discussion: (token: string, map_id: string, discussion_id: number) => delete_map_discussion(token, map_id, discussion_id),
38 delete_map_discussion: (map_id: string, discussion_id: number) => delete_map_discussion(map_id, discussion_id), 39 // Mod
40 post_map_summary: (token: string, map_id: string, content: ModMenuContent) => post_map_summary(token, map_id, content),
41
42 put_map_image: (token: string, map_id: string, image: string) => put_map_image(token, map_id, image),
43 put_map_summary: (token: string, map_id: string, content: ModMenuContent) => put_map_summary(token, map_id, content),
44
45 delete_map_summary: (token: string, map_id: string, route_id: number) => delete_map_summary(token, map_id, route_id),
39}; 46};
40 47
41const BASE_API_URL: string = "https://lp.ardapektezol.com/api/v1/" 48const BASE_API_URL: string = "/api/v1/"
42 49
43function url(path: string): string { 50export function url(path: string): string {
44 return BASE_API_URL + path; 51 return BASE_API_URL + path;
45}
46
47// USER
48
49const user_logout = async () => {
50 await axios.delete(url("token"));
51};
52
53const get_user = async (user_id: string): Promise<UserProfile> => {
54 const response = await axios.get(url(`users/${user_id}`))
55 return response.data.data;
56};
57
58
59// GAMES
60
61const get_games = async (): Promise<Game[]> => {
62 const response = await axios.get(url("games"))
63 return response.data.data;
64};
65
66const get_chapters = async (chapter_id: string): Promise<GameChapter> => {
67 const response = await axios.get(url(`chapters/${chapter_id}`));
68 return response.data.data;
69}
70
71const get_games_chapters = async (game_id: string): Promise<GamesChapters> => {
72 const response = await axios.get(url(`games/${game_id}`));
73 return response.data.data;
74};
75
76const get_game_maps = async (game_id: string): Promise<Map[]> => {
77 const response = await axios.get(url(`games/${game_id}/maps`))
78 return response.data.data.maps;
79};
80
81
82// RANKINGS
83const get_rankings = async (): Promise<Ranking> => {
84 const response = await axios.get(url(`rankings`));
85 return response.data.data;
86}
87
88// SEARCH
89
90const get_search = async (q: string): Promise<Search> => {
91 const response = await axios.get(url(`search?q=${q}`))
92 return response.data.data;
93};
94
95// MAP SUMMARY
96
97const put_map_image = async (map_id: string, image: string): Promise<boolean> => {
98 const response = await axios.put(url(`maps/${map_id}/image`), {
99 "image": image,
100 });
101 return response.data.success;
102};
103
104const get_map_summary = async (map_id: string): Promise<MapSummary> => {
105 const response = await axios.get(url(`maps/${map_id}/summary`))
106 return response.data.data;
107};
108
109const post_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => {
110 const response = await axios.post(url(`maps/${map_id}/summary`), {
111 "user_name": content.name,
112 "score_count": content.score,
113 "record_date": content.date,
114 "showcase": content.showcase,
115 "description": content.description,
116 });
117 return response.data.success;
118};
119
120const put_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => {
121 const response = await axios.put(url(`maps/${map_id}/summary`), {
122 "route_id": content.id,
123 "user_name": content.name,
124 "score_count": content.score,
125 "record_date": content.date,
126 "showcase": content.showcase,
127 "description": content.description,
128 });
129 return response.data.success;
130};
131
132const delete_map_summary = async (map_id: string, route_id: number): Promise<boolean> => {
133 const response = await axios.delete(url(`maps/${map_id}/summary`), {
134 data: {
135 "route_id": route_id,
136 }
137 });
138 return response.data.success;
139};
140
141// MAP LEADERBOARDS
142
143const get_map_leaderboard = async (map_id: string): Promise<MapLeaderboard | undefined> => {
144 const response = await axios.get(url(`maps/${map_id}/leaderboards`))
145 if (!response.data.success) {
146 return undefined;
147 }
148 const data = response.data.data;
149 // map the kind of leaderboard
150 data.records = data.records.map((record: any) => {
151 if (record.host && record.partner) {
152 return { ...record, kind: 'multiplayer' };
153 } else {
154 return { ...record, kind: 'singleplayer' };
155 }
156 });
157 // should be unreachable
158 return undefined;
159};
160
161// MAP DISCUSSIONS
162
163const get_map_discussions = async (map_id: string): Promise<MapDiscussions | undefined> => {
164 const response = await axios.get(url(`maps/${map_id}/discussions`));
165 if (!response.data.data.discussions) {
166 return undefined;
167 }
168 return response.data.data;
169};
170
171const get_map_discussion = async (map_id: string, discussion_id: number): Promise<MapDiscussion | undefined> => {
172 const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`));
173 if (!response.data.data.discussion) {
174 return undefined;
175 }
176 return response.data.data;
177};
178
179const post_map_discussion = async (map_id: string, content: MapDiscussionContent): Promise<boolean> => {
180 const response = await axios.post(url(`maps/${map_id}/discussions`), {
181 "title": content.title,
182 "content": content.content,
183 });
184 return response.data.success;
185};
186
187const post_map_discussion_comment = async (map_id: string, discussion_id: number, content: MapDiscussionCommentContent): Promise<boolean> => {
188 const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), {
189 "comment": content.comment,
190 });
191 return response.data.success;
192};
193
194const delete_map_discussion = async (map_id: string, discussion_id: number): Promise<boolean> => {
195 const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`));
196 return response.data.success;
197}; 52};