Files
game-marathon/frontend/src/api/games.ts
2025-12-18 23:47:11 +07:00

124 lines
4.2 KiB
TypeScript

import client from './client'
import type { Game, GameStatus, Challenge, ChallengePreview, ChallengesPreviewResponse } from '@/types'
export interface CreateGameData {
title: string
download_url: string
genre?: string
cover_url?: string
}
export interface CreateChallengeData {
title: string
description: string
type: string
difficulty: string
points: number
estimated_time?: number
proof_type: string
proof_hint?: string
}
export const gamesApi = {
list: async (marathonId: number, status?: GameStatus): Promise<Game[]> => {
const params = status ? { status } : {}
const response = await client.get<Game[]>(`/marathons/${marathonId}/games`, { params })
return response.data
},
listPending: async (marathonId: number): Promise<Game[]> => {
const response = await client.get<Game[]>(`/marathons/${marathonId}/games/pending`)
return response.data
},
get: async (id: number): Promise<Game> => {
const response = await client.get<Game>(`/games/${id}`)
return response.data
},
create: async (marathonId: number, data: CreateGameData): Promise<Game> => {
const response = await client.post<Game>(`/marathons/${marathonId}/games`, data)
return response.data
},
delete: async (id: number): Promise<void> => {
await client.delete(`/games/${id}`)
},
approve: async (id: number): Promise<Game> => {
const response = await client.post<Game>(`/games/${id}/approve`)
return response.data
},
reject: async (id: number): Promise<Game> => {
const response = await client.post<Game>(`/games/${id}/reject`)
return response.data
},
uploadCover: async (id: number, file: File): Promise<Game> => {
const formData = new FormData()
formData.append('file', file)
const response = await client.post<Game>(`/games/${id}/cover`, formData, {
headers: { 'Content-Type': 'multipart/form-data' },
})
return response.data
},
// Challenges
getChallenges: async (gameId: number): Promise<Challenge[]> => {
const response = await client.get<Challenge[]>(`/games/${gameId}/challenges`)
return response.data
},
createChallenge: async (gameId: number, data: CreateChallengeData): Promise<Challenge> => {
const response = await client.post<Challenge>(`/games/${gameId}/challenges`, data)
return response.data
},
deleteChallenge: async (id: number): Promise<void> => {
await client.delete(`/challenges/${id}`)
},
updateChallenge: async (id: number, data: Partial<CreateChallengeData>): Promise<Challenge> => {
const response = await client.patch<Challenge>(`/challenges/${id}`, data)
return response.data
},
previewChallenges: async (marathonId: number, gameIds?: number[]): Promise<ChallengesPreviewResponse> => {
const data = gameIds?.length ? { game_ids: gameIds } : undefined
const response = await client.post<ChallengesPreviewResponse>(`/marathons/${marathonId}/preview-challenges`, data)
return response.data
},
saveChallenges: async (marathonId: number, challenges: ChallengePreview[]): Promise<{ message: string }> => {
const response = await client.post<{ message: string }>(`/marathons/${marathonId}/save-challenges`, { challenges })
return response.data
},
// Proposed challenges
proposeChallenge: async (gameId: number, data: CreateChallengeData): Promise<Challenge> => {
const response = await client.post<Challenge>(`/games/${gameId}/propose-challenge`, data)
return response.data
},
getProposedChallenges: async (marathonId: number): Promise<Challenge[]> => {
const response = await client.get<Challenge[]>(`/marathons/${marathonId}/proposed-challenges`)
return response.data
},
getMyProposedChallenges: async (marathonId: number): Promise<Challenge[]> => {
const response = await client.get<Challenge[]>(`/marathons/${marathonId}/my-proposed-challenges`)
return response.data
},
approveChallenge: async (id: number): Promise<Challenge> => {
const response = await client.patch<Challenge>(`/challenges/${id}/approve`)
return response.data
},
rejectChallenge: async (id: number): Promise<Challenge> => {
const response = await client.patch<Challenge>(`/challenges/${id}/reject`)
return response.data
},
}