feat: implement complete game management with CRUD functionality

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.
This commit is contained in:
2026-02-11 22:09:02 +01:00
parent 08aca0fd5b
commit 630ebe0dc8
33 changed files with 2241 additions and 71 deletions

View File

@@ -0,0 +1,38 @@
import { Game } from '../../types/game';
interface GameCardProps {
game: Game;
onEdit?: (game: Game) => void;
onDelete?: (id: string) => void;
}
export default function GameCard({ game, onEdit, onDelete }: GameCardProps): JSX.Element {
return (
<div className="rounded border border-gray-300 p-4 shadow-sm hover:shadow-md">
<h3 className="mb-2 text-lg font-semibold">{game.title}</h3>
<p className="mb-2 text-sm text-gray-600">{game.slug}</p>
{game.description && <p className="mb-3 text-sm text-gray-700">{game.description}</p>}
<p className="mb-4 text-xs text-gray-500">
Added: {new Date(game.createdAt).toLocaleDateString()}
</p>
<div className="flex gap-2">
{onEdit && (
<button
onClick={() => onEdit(game)}
className="rounded bg-blue-600 px-3 py-1 text-sm text-white hover:bg-blue-700"
>
Edit
</button>
)}
{onDelete && (
<button
onClick={() => onDelete(game.id)}
className="rounded bg-red-600 px-3 py-1 text-sm text-white hover:bg-red-700"
>
Delete
</button>
)}
</div>
</div>
);
}

View File

@@ -0,0 +1,190 @@
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { z } from 'zod';
import { Game, CreateGameInput } from '../../types/game';
const gameFormSchema = z.object({
title: z.string().min(1, 'Title is required'),
platformId: z.string().min(1, 'Platform is required'),
description: z.string().optional().nullable(),
priceCents: z.number().optional(),
currency: z.string().optional().default('USD'),
store: z.string().optional(),
date: z.string().optional(),
condition: z.enum(['Loose', 'CIB', 'New']).optional(),
notes: z.string().optional().nullable(),
});
type GameFormData = z.infer<typeof gameFormSchema>;
interface GameFormProps {
initialData?: Game;
onSubmit: (data: CreateGameInput | Game) => void | Promise<void>;
isLoading?: boolean;
}
export default function GameForm({
initialData,
onSubmit,
isLoading = false,
}: GameFormProps): JSX.Element {
const {
register,
handleSubmit,
formState: { errors },
} = useForm<GameFormData>({
resolver: zodResolver(gameFormSchema),
defaultValues: initialData
? {
title: initialData.title,
description: initialData.description,
priceCents: undefined,
currency: 'USD',
store: undefined,
date: undefined,
condition: undefined,
notes: undefined,
}
: undefined,
});
const onFormSubmit = (data: GameFormData) => {
onSubmit(data as CreateGameInput);
};
return (
<form onSubmit={handleSubmit(onFormSubmit)} className="space-y-4">
<div>
<label htmlFor="title" className="block text-sm font-medium">
Title *
</label>
<input
{...register('title')}
id="title"
type="text"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
/>
{errors.title && <p className="text-red-600 text-sm">{errors.title.message}</p>}
</div>
<div>
<label htmlFor="platformId" className="block text-sm font-medium">
Platform *
</label>
<input
{...register('platformId')}
id="platformId"
type="text"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
/>
{errors.platformId && <p className="text-red-600 text-sm">{errors.platformId.message}</p>}
</div>
<div>
<label htmlFor="condition" className="block text-sm font-medium">
Condition
</label>
<select
{...register('condition')}
id="condition"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
>
<option value="">Select condition</option>
<option value="Loose">Loose</option>
<option value="CIB">CIB</option>
<option value="New">New</option>
</select>
</div>
<div>
<label htmlFor="description" className="block text-sm font-medium">
Description
</label>
<textarea
{...register('description')}
id="description"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
rows={3}
/>
</div>
<div>
<label htmlFor="priceCents" className="block text-sm font-medium">
Price (cents)
</label>
<input
{...register('priceCents', { valueAsNumber: true })}
id="priceCents"
type="number"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
/>
</div>
<div>
<label htmlFor="currency" className="block text-sm font-medium">
Currency
</label>
<input
{...register('currency')}
id="currency"
type="text"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
defaultValue="USD"
/>
</div>
<div>
<label htmlFor="store" className="block text-sm font-medium">
Store
</label>
<input
{...register('store')}
id="store"
type="text"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
/>
</div>
<div>
<label htmlFor="date" className="block text-sm font-medium">
Purchase Date
</label>
<input
{...register('date')}
id="date"
type="date"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
/>
</div>
<div>
<label htmlFor="notes" className="block text-sm font-medium">
Notes
</label>
<textarea
{...register('notes')}
id="notes"
className="mt-1 block w-full rounded border border-gray-300 px-3 py-2"
disabled={isLoading}
rows={2}
/>
</div>
<button
type="submit"
disabled={isLoading}
className="w-full rounded bg-blue-600 px-4 py-2 text-white disabled:bg-gray-400"
>
{isLoading ? 'Saving...' : 'Save Game'}
</button>
</form>
);
}

View File

@@ -1,4 +1,45 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { api } from '../lib/api';
import { Game, CreateGameInput, UpdateGameInput } from '../types/game';
const GAMES_QUERY_KEY = ['games'];
export function useGames() {
// placeholder stub for tests and future implementation
return { data: [], isLoading: false };
return useQuery({
queryKey: GAMES_QUERY_KEY,
queryFn: () => api.games.list(),
});
}
export function useCreateGame() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (data: CreateGameInput) => api.games.create(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: GAMES_QUERY_KEY });
},
});
}
export function useUpdateGame() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ id, data }: { id: string; data: UpdateGameInput }) => api.games.update(id, data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: GAMES_QUERY_KEY });
},
});
}
export function useDeleteGame() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (id: string) => api.games.delete(id),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: GAMES_QUERY_KEY });
},
});
}

View File

@@ -1,3 +1,39 @@
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 = {
// placeholder for future HTTP client
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',
}),
},
};

View File

@@ -1,9 +1,165 @@
import React from 'react';
import React, { useState } from 'react';
import { useGames, useCreateGame, useUpdateGame, useDeleteGame } from '../hooks/useGames';
import GameForm from '../components/games/GameForm';
import { Game, CreateGameInput, UpdateGameInput } from '../types/game';
export default function Games(): JSX.Element {
const { data: games, isLoading, error } = useGames();
const createMutation = useCreateGame();
const updateMutation = useUpdateGame();
const deleteMutation = useDeleteGame();
const [isFormOpen, setIsFormOpen] = useState(false);
const [selectedGame, setSelectedGame] = useState<Game | null>(null);
const [deleteConfirm, setDeleteConfirm] = useState<string | null>(null);
const handleCreate = async (data: CreateGameInput | Game) => {
try {
await createMutation.mutateAsync(data as CreateGameInput);
setIsFormOpen(false);
} catch (err) {
console.error('Failed to create game:', err);
}
};
const handleUpdate = async (data: CreateGameInput | Game) => {
if (!selectedGame) return;
try {
await updateMutation.mutateAsync({
id: selectedGame.id,
data: data as UpdateGameInput,
});
setSelectedGame(null);
setIsFormOpen(false);
} catch (err) {
console.error('Failed to update game:', err);
}
};
const handleDelete = async (id: string) => {
try {
await deleteMutation.mutateAsync(id);
setDeleteConfirm(null);
} catch (err) {
console.error('Failed to delete game:', err);
}
};
const handleOpenForm = (game?: Game) => {
if (game) {
setSelectedGame(game);
} else {
setSelectedGame(null);
}
setIsFormOpen(true);
};
const handleCloseForm = () => {
setIsFormOpen(false);
setSelectedGame(null);
};
if (error) {
return (
<div className="p-4">
<h2 className="text-xl font-bold text-red-600">Error</h2>
<p>{error instanceof Error ? error.message : 'Failed to load games'}</p>
</div>
);
}
return (
<div>
<h2>Games</h2>
<div className="p-4">
<div className="mb-6 flex items-center justify-between">
<h2 className="text-2xl font-bold">Games</h2>
<button
onClick={() => handleOpenForm()}
className="rounded bg-green-600 px-4 py-2 text-white hover:bg-green-700 disabled:bg-gray-400"
disabled={isLoading}
>
Add Game
</button>
</div>
{isFormOpen && (
<div className="mb-6 rounded border border-gray-300 p-4">
<div className="mb-4 flex justify-between">
<h3 className="text-lg font-semibold">{selectedGame ? 'Edit Game' : 'Create Game'}</h3>
<button onClick={handleCloseForm} className="text-gray-600 hover:text-gray-900">
</button>
</div>
<GameForm
initialData={selectedGame || undefined}
onSubmit={selectedGame ? handleUpdate : handleCreate}
isLoading={createMutation.isPending || updateMutation.isPending}
/>
</div>
)}
{isLoading && !games ? (
<p className="text-gray-600">Loading games...</p>
) : !games || games.length === 0 ? (
<p className="text-gray-600">No games found. Create one to get started!</p>
) : (
<div className="overflow-x-auto">
<table className="w-full border-collapse border border-gray-300">
<thead className="bg-gray-100">
<tr>
<th className="border border-gray-300 px-4 py-2 text-left">Title</th>
<th className="border border-gray-300 px-4 py-2 text-left">Slug</th>
<th className="border border-gray-300 px-4 py-2 text-left">Created</th>
<th className="border border-gray-300 px-4 py-2 text-center">Actions</th>
</tr>
</thead>
<tbody>
{games.map((game) => (
<tr key={game.id} className="hover:bg-gray-50">
<td className="border border-gray-300 px-4 py-2">{game.title}</td>
<td className="border border-gray-300 px-4 py-2">{game.slug}</td>
<td className="border border-gray-300 px-4 py-2">
{new Date(game.createdAt).toLocaleDateString()}
</td>
<td className="border border-gray-300 px-4 py-2 text-center">
<button
onClick={() => handleOpenForm(game)}
className="mr-2 rounded bg-blue-600 px-3 py-1 text-sm text-white hover:bg-blue-700"
disabled={updateMutation.isPending || deleteMutation.isPending}
>
Edit
</button>
{deleteConfirm === game.id ? (
<div className="inline-flex gap-2">
<button
onClick={() => handleDelete(game.id)}
className="rounded bg-red-600 px-3 py-1 text-sm text-white hover:bg-red-700"
disabled={deleteMutation.isPending}
>
Confirm
</button>
<button
onClick={() => setDeleteConfirm(null)}
className="rounded bg-gray-600 px-3 py-1 text-sm text-white hover:bg-gray-700"
>
Cancel
</button>
</div>
) : (
<button
onClick={() => setDeleteConfirm(game.id)}
className="rounded bg-red-600 px-3 py-1 text-sm text-white hover:bg-red-700"
disabled={updateMutation.isPending || deleteMutation.isPending}
>
Delete
</button>
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
);
}

View File

@@ -1,8 +1,16 @@
/* Minimal global styles */
html, body, #root {
html,
body,
#root {
height: 100%;
}
body {
margin: 0;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
font-family:
system-ui,
-apple-system,
'Segoe UI',
Roboto,
'Helvetica Neue',
Arial;
}

View File

@@ -0,0 +1,60 @@
export type GameCondition = 'Loose' | 'CIB' | 'New';
export interface Game {
id: string;
title: string;
slug: string;
description?: string | null;
releaseDate?: Date | null | string;
igdbId?: number | null;
rawgId?: number | null;
thegamesdbId?: number | null;
extra?: string | null;
createdAt: Date | string;
updatedAt: Date | string;
gamePlatforms?: GamePlatform[];
purchases?: Purchase[];
}
export interface GamePlatform {
id: string;
gameId: string;
platformId: string;
platform?: {
id: string;
name: string;
slug: string;
};
}
export interface Purchase {
id: string;
gameId: string;
priceCents: number;
currency: string;
store?: string | null;
date: Date | string;
receiptPath?: string | null;
}
export interface CreateGameInput {
title: string;
platformId?: string;
description?: string | null;
priceCents?: number;
currency?: string;
store?: string;
date?: string;
condition?: GameCondition;
}
export interface UpdateGameInput {
title?: string;
platformId?: string;
description?: string | null;
priceCents?: number;
currency?: string;
store?: string;
date?: string;
condition?: GameCondition;
}