Handballbooking/server/controllers/matchController.js
2025-06-04 15:13:40 +02:00

30 lines
1.1 KiB
JavaScript
Executable file

import { query } from '../utils/database.js';
export const getMatches = async (req, res) => {
try {
// Fetch all matches, ordered by date
const matches = await query('SELECT id, name, date, location, totalSeats, availableSeats, price, timeoutDate FROM matches ORDER BY date');
res.status(200).json(matches);
} catch (error) {
console.error('Error fetching matches:', error);
res.status(500).json({ message: 'Failed to fetch matches', error: error.message });
}
};
export const getMatchDetails = async (req, res) => {
try {
const { id } = req.params;
// Fetch details for a specific match
const match = await query('SELECT id, name, date, location, totalSeats, availableSeats, price, timeoutDate FROM matches WHERE id = ?', [id]);
if (match.length === 0) {
return res.status(404).json({ message: 'Match not found' });
}
res.status(200).json(match[0]);
} catch (error) {
console.error('Error fetching match details:', error);
res.status(500).json({ message: 'Failed to fetch match details', error: error.message });
}
};