aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/api/Api.tsx
blob: 1f77088a8ad93718f22650b8baf81946366e0275 (plain) (blame)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import axios from 'axios';

import { GameChapter, GamesChapters } from '../types/Chapters';
import { Game } from '../types/Game';
import { Map, MapDiscussion, MapDiscussions, MapLeaderboard, MapSummary } from '../types/Map';
import { MapDiscussionCommentContent, MapDiscussionContent, ModMenuContent } from '../types/Content';
import { Search } from '../types/Search';
import { UserProfile } from '../types/Profile';
import { Ranking } from '../types/Ranking';

// add new api call function entries here
// example usage: API.get_games();
export const API = {
  user_logout: () => user_logout(),

  get_user: (user_id: string) => get_user(user_id),
  get_games: () => get_games(),

  get_chapters: (chapter_id: string) => get_chapters(chapter_id),
  get_games_chapters: (game_id: string) => get_games_chapters(game_id),
  get_game_maps: (game_id: string) => get_game_maps(game_id),
  get_rankings: () => get_rankings(),
  get_search: (q: string) => get_search(q),

  get_map_summary: (map_id: string) => get_map_summary(map_id),
  get_map_leaderboard: (map_id: string) => get_map_leaderboard(map_id),
  get_map_discussions: (map_id: string) => get_map_discussions(map_id),
  get_map_discussion: (map_id: string, discussion_id: number) => get_map_discussion(map_id, discussion_id),

  post_map_summary: (map_id: string, content: ModMenuContent) => post_map_summary(map_id, content),
  post_map_discussion: (map_id: string, content: MapDiscussionContent) => post_map_discussion(map_id, content),
  post_map_discussion_comment: (map_id: string, discussion_id: number, content: MapDiscussionCommentContent) => post_map_discussion_comment(map_id, discussion_id, content),

  put_map_image: (map_id: string, image: string) => put_map_image(map_id, image),
  put_map_summary: (map_id: string, content: ModMenuContent) => put_map_summary(map_id, content),

  delete_map_summary: (map_id: string, route_id: number) => delete_map_summary(map_id, route_id),
  delete_map_discussion: (map_id: string, discussion_id: number) => delete_map_discussion(map_id, discussion_id),
};

const BASE_API_URL: string = "https://lp.ardapektezol.com/api/v1/"

function url(path: string): string {
  return BASE_API_URL + path;
}

// USER

const user_logout = async () => {
  await axios.delete(url("token"));
};

const get_user = async (user_id: string): Promise<UserProfile> => {
  const response = await axios.get(url(`users/${user_id}`))
  return response.data.data;
};


// GAMES

const get_games = async (): Promise<Game[]> => {
  const response = await axios.get(url("games"))
  return response.data.data;
};

const get_chapters = async (chapter_id: string): Promise<GameChapter> => {
  const response = await axios.get(url(`chapters/${chapter_id}`));
  return response.data.data;
}

const get_games_chapters = async (game_id: string): Promise<GamesChapters> => {
  const response = await axios.get(url(`games/${game_id}`));
  return response.data.data;
};

const get_game_maps = async (game_id: string): Promise<Map[]> => {
  const response = await axios.get(url(`games/${game_id}/maps`))
  return response.data.data.maps;
};


// RANKINGS
const get_rankings = async (): Promise<Ranking> => {
  const response = await axios.get(url(`rankings`));
  return response.data.data;
}

// SEARCH

const get_search = async (q: string): Promise<Search> => {
  const response = await axios.get(url(`search?q=${q}`))
  return response.data.data;
};

// MAP SUMMARY

const put_map_image = async (map_id: string, image: string): Promise<boolean> => {
  const response = await axios.put(url(`maps/${map_id}/image`), {
    "image": image,
  });
  return response.data.success;
};

const get_map_summary = async (map_id: string): Promise<MapSummary> => {
  const response = await axios.get(url(`maps/${map_id}/summary`))
  return response.data.data;
};

const post_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => {
  const response = await axios.post(url(`maps/${map_id}/summary`), {
    "user_name": content.name,
    "score_count": content.score,
    "record_date": content.date,
    "showcase": content.showcase,
    "description": content.description,
  });
  return response.data.success;
};

const put_map_summary = async (map_id: string, content: ModMenuContent): Promise<boolean> => {
  const response = await axios.put(url(`maps/${map_id}/summary`), {
    "route_id": content.id,
    "user_name": content.name,
    "score_count": content.score,
    "record_date": content.date,
    "showcase": content.showcase,
    "description": content.description,
  });
  return response.data.success;
};

const delete_map_summary = async (map_id: string, route_id: number): Promise<boolean> => {
  const response = await axios.delete(url(`maps/${map_id}/summary`), {
    data: {
      "route_id": route_id,
    }
  });
  return response.data.success;
};

// MAP LEADERBOARDS

const get_map_leaderboard = async (map_id: string): Promise<MapLeaderboard | undefined> => {
  const response = await axios.get(url(`maps/${map_id}/leaderboards`))
  if (!response.data.success) {
    return undefined;
  }
  return response.data.data;
};

// MAP DISCUSSIONS

const get_map_discussions = async (map_id: string): Promise<MapDiscussions | undefined> => {
  const response = await axios.get(url(`maps/${map_id}/discussions`));
  if (!response.data.data.discussions) {
    return undefined;
  }
  return response.data.data;
};

const get_map_discussion = async (map_id: string, discussion_id: number): Promise<MapDiscussion | undefined> => {
  const response = await axios.get(url(`maps/${map_id}/discussions/${discussion_id}`));
  if (!response.data.data.discussion) {
    return undefined;
  }
  return response.data.data;
};

const post_map_discussion = async (map_id: string, content: MapDiscussionContent): Promise<boolean> => {
  const response = await axios.post(url(`maps/${map_id}/discussions`), {
    "title": content.title,
    "content": content.content,
  });
  return response.data.success;
};

const post_map_discussion_comment = async (map_id: string, discussion_id: number, content: MapDiscussionCommentContent): Promise<boolean> => {
  const response = await axios.post(url(`maps/${map_id}/discussions/${discussion_id}`), {
    "comment": content.comment,
  });
  return response.data.success;
};

const delete_map_discussion = async (map_id: string, discussion_id: number): Promise<boolean> => {
  const response = await axios.delete(url(`maps/${map_id}/discussions/${discussion_id}`));
  return response.data.success;
};