Skip to content

Commit

Permalink
Added endpoints for tournament
Browse files Browse the repository at this point in the history
  • Loading branch information
JarandJR authored and lille-morille committed Apr 15, 2024
1 parent 1d3b70c commit a5af8d5
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 6,832 deletions.
91 changes: 27 additions & 64 deletions app/api/tournament.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,41 @@
import { IFetch } from '@/app/api/fetch';
import { URLSearchParams } from 'url';
import queryString from 'query-string';
import { PongTournament, PongMatch, PongTeam, PongResult, AnonymousUser, RequestResponse, User } from '@/app/types/tournament';

const TOURNAMENT_ENDPOINT = 'tournament';
const MATCH_ENDPOINT = 'match';
const TEAM_ENDPOINT = 'team';
const RESULT_ENDPOINT = 'result';
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';

interface Response {
detail?: string;
}

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

interface User {
user_id?: string;
}

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

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

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

interface PongTournament {
id?: number;
name?: string;
matches?: PongMatch[];
}

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 ? `?${new URLSearchParams(filters)}` : ''}`, { method: 'GET' }),
createTournamentItem: async (name: string): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/`, { method: 'POST', data: {name: name}}),
updateTournamentItem: async (id: number, name: string): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/${String(id)}/`, { method: 'PUT', data: {name: name}}),
generateTournament: async (id: number): Promise<PongTournament> => IFetch<PongTournament>(`${TOURNAMENT_ENDPOINT}/${String(id)}/generate/`, { method: 'POST'}),
deleteTournamentItem: async (id: number): Promise<Response> => IFetch<Response>(`${TOURNAMENT_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),
};
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'}),

export const PongMatch = {
/* 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'}),

export const PongTeam = {
/* 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 ? `?${new URLSearchParams(filters)}` : ''}`, { method: 'GET' }),
createTeamItem: async (team_name: string, tournament: number, members: string[], anonymous_members: AnonymousUser[]): Promise<PongTeam> => IFetch<PongTeam>(`${TEAM_ENDPOINT}/`, { method: 'POST', data: {team_name, members, anonymous_members, tournament}}),
updateTeamItem: async (id: number, team_name: string, members: string[], anonymous_members: AnonymousUser[], tournament: number): Promise<PongTeam> => IFetch<PongTeam>(`${TEAM_ENDPOINT}/${String(id)}/`, { method: 'PUT', data: {team_name, members, anonymous_members, tournament}}),
deleteTeamItem: async (id: number): Promise<Response> => IFetch<Response>(`${TEAM_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),
};
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'}),

export const PongResult = {
/* 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 (id: number, match: number, result: string): Promise<PongResult> => IFetch<PongResult>(`${RESULT_ENDPOINT}/${String(id)}/`, { method: 'PUT', data: {match, result}}),
deleteResultItem: async (id: number): Promise<Response> => IFetch<Response>(`${RESULT_ENDPOINT}/${String(id)}/`, { method: 'DELETE'}),
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'}),
};
190 changes: 170 additions & 20 deletions app/hooks/useTournament.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,171 @@
import { useState, useCallback } from 'react';
import { authenticate } from '@/app/api/user';
import { setClientCookie } from '@/app/utils/cookieStore';

export const useUser = () => {
const [user, setUser] = useState<any | null>(null);

const login = useCallback(async (username: string, password: string) => {
try {
const data = await authenticate(username, password);
setClientCookie('tokenDrinking', data.token, { expires: 365 });
} catch (error) {
console.error('Failed to login:', error);
}
}, []);

return {
user,
login,
};
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);
}
};
42 changes: 42 additions & 0 deletions app/types/tournament.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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[];
}
Loading

0 comments on commit a5af8d5

Please sign in to comment.