|
| 1 | +import Championship from '../models/Championship.js'; |
| 2 | + |
| 3 | +// Liste (optionnellement filtrée par saison ou équipe) |
| 4 | +export const getChampionships = async (req, res) => { |
| 5 | + try { |
| 6 | + const filter = {}; |
| 7 | + |
| 8 | + if (req.query.seasonId) { |
| 9 | + filter.seasonId = req.query.seasonId; |
| 10 | + } |
| 11 | + |
| 12 | + if (req.query.teamId) { |
| 13 | + filter.teamId = req.query.teamId; |
| 14 | + } |
| 15 | + |
| 16 | + const championships = await Championship.find(filter).populate('teamId').sort({ createdAt: -1 }); |
| 17 | + |
| 18 | + res.json(championships); |
| 19 | + } catch (error) { |
| 20 | + res.status(500).json({ message: error.message }); |
| 21 | + } |
| 22 | +}; |
| 23 | + |
| 24 | +// Par ID |
| 25 | +export const getChampionshipById = async (req, res) => { |
| 26 | + try { |
| 27 | + const championship = await Championship.findById(req.params.id).populate('teamId'); |
| 28 | + |
| 29 | + if (!championship) { |
| 30 | + return res.status(404).json({ message: 'Championship not found' }); |
| 31 | + } |
| 32 | + |
| 33 | + res.json(championship); |
| 34 | + } catch (error) { |
| 35 | + res.status(500).json({ message: error.message }); |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +// Création |
| 40 | +export const createChampionship = async (req, res) => { |
| 41 | + try { |
| 42 | + const championship = await Championship.create(req.body); |
| 43 | + res.status(201).json(championship); |
| 44 | + } catch (error) { |
| 45 | + res.status(400).json({ message: error.message }); |
| 46 | + } |
| 47 | +}; |
| 48 | + |
| 49 | +// Mise à jour |
| 50 | +export const updateChampionship = async (req, res) => { |
| 51 | + try { |
| 52 | + const championship = await Championship.findByIdAndUpdate(req.params.id, req.body, { |
| 53 | + new: true, |
| 54 | + runValidators: true, |
| 55 | + }); |
| 56 | + |
| 57 | + if (!championship) { |
| 58 | + return res.status(404).json({ message: 'Championship not found' }); |
| 59 | + } |
| 60 | + |
| 61 | + res.json(championship); |
| 62 | + } catch (error) { |
| 63 | + res.status(400).json({ message: error.message }); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +// Suppression |
| 68 | +export const deleteChampionship = async (req, res) => { |
| 69 | + try { |
| 70 | + const championship = await Championship.findByIdAndDelete(req.params.id); |
| 71 | + |
| 72 | + if (!championship) { |
| 73 | + return res.status(404).json({ message: 'Championship not found' }); |
| 74 | + } |
| 75 | + |
| 76 | + res.json({ message: 'Championship deleted' }); |
| 77 | + } catch (error) { |
| 78 | + res.status(500).json({ message: error.message }); |
| 79 | + } |
| 80 | +}; |
0 commit comments