Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat(tournament)/endpoints' into feat(tourna…
Browse files Browse the repository at this point in the history
…ment)/dev

# Conflicts:
#	package-lock.json
  • Loading branch information
lille-morille committed Apr 15, 2024
2 parents 420ee5f + 73ba000 commit b6d5767
Show file tree
Hide file tree
Showing 6 changed files with 4,358 additions and 6,436 deletions.
41 changes: 41 additions & 0 deletions app/api/tournament.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { IFetch } from '@/app/api/fetch';
import queryString from 'query-string';
import { PongTournament, PongMatch, PongTeam, PongResult, AnonymousUser, RequestResponse, User } from '@/app/types/tournament';

const BASE_URL = 'blitzed/';
const TOURNAMENT_ENDPOINT = BASE_URL + 'tournament';
const MATCH_ENDPOINT = BASE_URL + 'match';
const TEAM_ENDPOINT = BASE_URL + 'team';
const RESULT_ENDPOINT = BASE_URL + 'result';


export const Tournament = {
/* Tournament */
getTournamentItem: async (id: number): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/${String(id)}/`, { method: 'GET'}),
getTournamentList: async (filters?: any): Promise<PongTournament[]> => IFetch<PongTournament[]>(`${TOURNAMENT_ENDPOINT}/${filters ? `?${queryString.stringify(filters)}` : ''}`, { method: 'GET' }),
createTournamentItem: async (name: string): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/`, { method: 'POST', data: {name}}),
updateTournamentItem: async (tournament: PongTournament): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/${String(tournament.id)}/`, { method: 'PUT', data: tournament}),
generateTournament: async (id: number): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/${String(id)}/generate/`, { method: 'GET'}),
deleteTournamentItem: async (id: number): Promise<RequestResponse> => IFetch<RequestResponse>(`${TOURNAMENT_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),

/* Match */
getMatchItem: async (id: number): Promise<PongMatch> => IFetch<PongMatch>(`${MATCH_ENDPOINT}/${String(id)}/`, { method: 'GET'}),
getMatchList: async (filters?: any): Promise<PongMatch[]> => IFetch<PongMatch[]>(`${MATCH_ENDPOINT}/${filters ? `?${queryString.stringify(filters)}` : ''}`, { method: 'GET' }),
createMatchItem: async (match: PongMatch): Promise<PongMatch> => IFetch<PongMatch>(`${MATCH_ENDPOINT}/`, { method: 'POST', data: match}),
updateMatchItem: async (match: PongMatch): Promise<PongMatch> => IFetch<PongMatch>(`${MATCH_ENDPOINT}/${String(match.id)}/`, { method: 'PUT', data: match}),
deleteMatchItem: async (id: number): Promise<RequestResponse> => IFetch<RequestResponse>(`${MATCH_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),

/* Team */
getTeamItem: async (id: number): Promise<PongTeam> => IFetch<PongTeam>(`${TEAM_ENDPOINT}/${String(id)}/`, { method: 'GET'}),
getTeamList: async (filters?: any): Promise<PongTeam[]> => IFetch<PongTeam[]>(`${TEAM_ENDPOINT}/${filters ? `?${queryString.stringify(filters)}` : ''}`, { method: 'GET' }),
createTeamItem: async (team: PongTeam): Promise<PongTeam> => IFetch<PongTeam>(`${TEAM_ENDPOINT}/`, { method: 'POST', data: team}),
updateTeamItem: async (team: PongTeam): Promise<PongTeam> => IFetch<PongTeam>(`${TEAM_ENDPOINT}/${String(team.id)}/`, { method: 'PUT', data: team}),
deleteTeamItem: async (id: number): Promise<RequestResponse> => IFetch<RequestResponse>(`${TEAM_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),

/* Result */
getResultItem: async (id: number): Promise<PongResult> => IFetch<PongResult>(`${RESULT_ENDPOINT}/${String(id)}/`, { method: 'GET'}),
getResultList: async (filters?: any): Promise<PongResult[]> => IFetch<PongResult[]>(`${RESULT_ENDPOINT}/${filters ? `?${queryString.stringify(filters)}` : ''}`, { method: 'GET' }),
createResultItem: async (match: number, result: string): Promise<PongResult> => IFetch<PongResult>(`${RESULT_ENDPOINT}/`, { method: 'POST', data: {match, result}}),
updateResultItem: async (res: PongResult): Promise<PongResult> => IFetch<PongResult>(`${RESULT_ENDPOINT}/${String(res.id)}/`, { method: 'PUT', data: res}),
deleteResultItem: async (id: number): Promise<RequestResponse> => IFetch<RequestResponse>(`${RESULT_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),
};
171 changes: 171 additions & 0 deletions app/hooks/useTournament.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { Tournament } from '@/app/api/tournament';
import { PongMatch, PongResult, PongTeam, PongTournament } from '@/app/types/tournament';

export const useGetTournament = async(id: number) => {
try {
return await Tournament.getTournamentItem(id);
} catch (err: any) {
console.error('Failed to get tournament:', err);
}
};

export const useGetTournamentList = async(filters?: any) => {
try {
return await Tournament.getTournamentList(filters);
} catch (err: any) {
console.error('Failed to get tournament list:', err);
}
};

export const useCreateTournament = async(name: string) => {
try {
return await Tournament.createTournamentItem(name);
} catch (err: any) {
console.error('Failed to create tournament:', err);
}
};

export const useUpdateTournament = async(tournament: PongTournament) => {
try {
return await Tournament.updateTournamentItem(tournament);
} catch (err: any) {
console.error('Failed to update tournament:', err);
}
};

export const useGenerateTournament = async(id: number) => {
try {
return await Tournament.generateTournament(id);
} catch (err: any) {
console.error('Failed to generate tournament:', err);
}
};

export const useDeleteTournament = async(id: number) => {
try {
return await Tournament.deleteTournamentItem(id);
} catch (err: any) {
console.error('Failed to delete tournament:', err);
}
};

export const useCreatePongMatch = async(newMatch: PongMatch) => {
try {
return await Tournament.createMatchItem(newMatch);
} catch (err: any) {
console.error('Failed to create match:', err);
}
};

export const useGetPongMatch = async(id: number) => {
try {
return await Tournament.getMatchItem(id);
} catch (err: any) {
console.error('Failed to get match:', err);
}
};

export const useGetPongMatchList = async(filters?: any) => {
try {
return await Tournament.getMatchList(filters);
} catch (err: any) {
console.error('Failed to get match list:', err);
}
};

export const useUpdatePongMatch = async(match: PongMatch) => {
try {
return await Tournament.updateMatchItem(match);
} catch (err: any) {
console.error('Failed to update match:', err);
}
};

export const useDeletePongMatch = async(id: number) => {
try {
return await Tournament.deleteMatchItem(id);
} catch (err: any) {
console.error('Failed to delete match:', err);
}
};

export const useCreatePongResult = async(match: number, result: string) => {
try {
console.log('useCreatePongResult', match, result);
return await Tournament.createResultItem(match, result);
} catch (err: any) {
console.error('Failed to create result:', err);
}
};

export const useGetPongResult = async(id: number) => {
try {
return await Tournament.getResultItem(id);
} catch (err: any) {
console.error('Failed to get result:', err);
}
};

export const useGetPongResultList = async(filters?: any) => {
try {
return await Tournament.getResultList(filters);
} catch (err: any) {
console.error('Failed to get result list:', err);
}
};

export const useUpdatePongResult = async(result: PongResult) => {
try {
return await Tournament.updateResultItem(result);
} catch (err: any) {
console.error('Failed to update result:', err);
}
};

export const useDeletePongResult = async(id: number) => {
try {
return await Tournament.deleteResultItem(id);
} catch (err: any) {
console.error('Failed to delete result:', err);
}
};

export const useCreatePongTeam = async(team: PongTeam) => {
try {
return await Tournament.createTeamItem(team);
} catch (err: any) {
console.error('Failed to create team:', err);
}
};

export const useGetPongTeam = async(id: number) => {
try {
return await Tournament.getTeamItem(id);
} catch (err: any) {
console.error('Failed to get team:', err);
}
};

export const useGetPongTeamList = async(filters?: any) => {
try {
return await Tournament.getTeamList(filters);
} catch (err: any) {
console.error('Failed to get team list:', err);
}
};

export const useUpdatePongTeam = async(team: PongTeam) => {
try {
return await Tournament.updateTeamItem(team);
} catch (err: any) {
console.error('Failed to update team:', err);
}
};

export const useDeletePongTeam = async(id: number) => {
try {
return await Tournament.deleteTeamItem(id);
} catch (err: any) {
console.error('Failed to delete team:', err);
}
};
50 changes: 50 additions & 0 deletions app/types/tournament.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export interface RequestResponse {
detail?: string;
}

export interface AnonymousUser {
id?: number;
name?: string;
}

export interface User {
user_id?: string;
}

export interface PongTeam {
id?: number;
team_name?: string;
members?: User[];
anonymous_members?: AnonymousUser[] | number[];
tournament?: number;
}

export interface PongResult {
id?: number;
match?: number;
winner?: PongTeam | number;
result?: string;
}

export interface PongMatch {
id?: number;
team1?: PongTeam | number;
team2?: PongTeam | number;
result?: PongResult;
future_match?: number;
tournament?: number;
}

export interface PongTournament {
id?: number;
name?: string;
matches?: PongMatch[];
status: TournamentStatus;
pin_code?: string;
creator: User;
access: TournamentAccess;
}

export type TournamentStatus = "PENDING" | "ACTIVE" | "FINISHED"

export type TournamentAccess = "PUBLIC" | "PIN";
Loading

0 comments on commit b6d5767

Please sign in to comment.