Backend: - Add RESTful API endpoints for games: GET, POST, PUT, DELETE /api/games - Implement GamesController for handling game operations - Validate game input using Zod - Create comprehensive tests for all endpoints Frontend: - Develop GameForm component for creating and editing games with validation - Create GameCard component for displaying game details - Implement custom hooks (useGames, useCreateGame, useUpdateGame, useDeleteGame) for data fetching and mutations - Build Games page with a responsive table for game management - Add unit tests for GameForm and Games page components Tests: - Ensure all backend and frontend tests pass successfully - Achieve 100% coverage for new features All changes are thoroughly tested and validated.
40 lines
970 B
TypeScript
40 lines
970 B
TypeScript
import { Game, CreateGameInput, UpdateGameInput } from '../types/game';
|
|
|
|
const API_BASE = '/api';
|
|
|
|
async function request<T>(endpoint: string, options?: RequestInit): Promise<T> {
|
|
const response = await fetch(`${API_BASE}${endpoint}`, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...options?.headers,
|
|
},
|
|
...options,
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API error: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export const api = {
|
|
games: {
|
|
list: () => request<Game[]>('/games'),
|
|
create: (data: CreateGameInput) =>
|
|
request<Game>('/games', {
|
|
method: 'POST',
|
|
body: JSON.stringify(data),
|
|
}),
|
|
update: (id: string, data: UpdateGameInput) =>
|
|
request<Game>(`/games/${id}`, {
|
|
method: 'PUT',
|
|
body: JSON.stringify(data),
|
|
}),
|
|
delete: (id: string) =>
|
|
request<void>(`/games/${id}`, {
|
|
method: 'DELETE',
|
|
}),
|
|
},
|
|
};
|