|
| 1 | +import Match from '../models/Match.js'; |
| 2 | + |
| 3 | +// Liste des matchs (par championnat) |
| 4 | +export const getMatches = async (req, res) => { |
| 5 | + try { |
| 6 | + const filter = {}; |
| 7 | + |
| 8 | + if (req.query.championshipId) { |
| 9 | + filter.championshipId = req.query.championshipId; |
| 10 | + } |
| 11 | + |
| 12 | + const matches = await Match.find(filter).populate('championshipId').sort({ date: 1 }); |
| 13 | + |
| 14 | + res.json(matches); |
| 15 | + } catch (error) { |
| 16 | + res.status(500).json({ message: error.message }); |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +// Match par ID |
| 21 | +export const getMatchById = async (req, res) => { |
| 22 | + try { |
| 23 | + const match = await Match.findById(req.params.id).populate('championshipId'); |
| 24 | + |
| 25 | + if (!match) { |
| 26 | + return res.status(404).json({ message: 'Match non trouvé' }); |
| 27 | + } |
| 28 | + |
| 29 | + res.json(match); |
| 30 | + } catch (error) { |
| 31 | + res.status(500).json({ message: error.message }); |
| 32 | + } |
| 33 | +}; |
| 34 | + |
| 35 | +// Création (import FFVB ou manuel) |
| 36 | +export const createMatch = async (req, res) => { |
| 37 | + try { |
| 38 | + const match = await Match.create(req.body); |
| 39 | + res.status(201).json(match); |
| 40 | + } catch (error) { |
| 41 | + res.status(400).json({ message: error.message }); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +// Mise à jour (score, statut, etc.) |
| 46 | +export const updateMatch = async (req, res) => { |
| 47 | + try { |
| 48 | + const match = await Match.findByIdAndUpdate(req.params.id, req.body, { |
| 49 | + new: true, |
| 50 | + runValidators: true, |
| 51 | + }); |
| 52 | + |
| 53 | + if (!match) { |
| 54 | + return res.status(404).json({ message: 'Match non trouvé' }); |
| 55 | + } |
| 56 | + |
| 57 | + res.json(match); |
| 58 | + } catch (error) { |
| 59 | + res.status(400).json({ message: error.message }); |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +// Suppression |
| 64 | +export const deleteMatch = async (req, res) => { |
| 65 | + try { |
| 66 | + const match = await Match.findByIdAndDelete(req.params.id); |
| 67 | + |
| 68 | + if (!match) { |
| 69 | + return res.status(404).json({ message: 'Match non trouvé' }); |
| 70 | + } |
| 71 | + |
| 72 | + res.json({ message: 'Match supprimé' }); |
| 73 | + } catch (error) { |
| 74 | + res.status(500).json({ message: error.message }); |
| 75 | + } |
| 76 | +}; |
0 commit comments