Files
game-marathon/frontend/src/api/games.ts

93 lines
3.0 KiB
TypeScript
Raw Normal View History

2025-12-14 02:38:35 +07:00
import client from './client'
2025-12-14 20:21:56 +07:00
import type { Game, GameStatus, Challenge, ChallengePreview, ChallengesPreviewResponse } from '@/types'
2025-12-14 02:38:35 +07:00
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 = {
2025-12-14 20:21:56 +07:00
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`)
2025-12-14 02:38:35 +07:00
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}`)
},
2025-12-14 20:21:56 +07:00
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
},
2025-12-14 02:38:35 +07:00
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}`)
},
2025-12-17 20:19:26 +07:00
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)
2025-12-14 03:23:50 +07:00
return response.data
},
saveChallenges: async (marathonId: number, challenges: ChallengePreview[]): Promise<{ message: string }> => {
const response = await client.post<{ message: string }>(`/marathons/${marathonId}/save-challenges`, { challenges })
2025-12-14 02:38:35 +07:00
return response.data
},
}