swimmingArt/routes/routines.js

98 lines
2.9 KiB
JavaScript

const express = require('express');
const mongoose = require('mongoose');
const router = express.Router();
const User = require('./user');
const routineSchema = new mongoose.Schema({
title: String,
createdBy: { type: String, default: "coach-id-ejemplo" },
language: { type: String, enum: ['es', 'en', 'fr'], default: 'es' },
duration: Number,
musicUrl: { type: String, default: "" },
nombreCompetencia: String,
tipoCompetencia: { type: String, enum: ['libre', 'técnica'], default: 'libre' },
modalidad: { type: String, enum: ['solo', 'duo', 'equipo'], default: 'solo' },
participantes: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
elements: [],
formaciones: [
{
nombreColoquial: String,
notasTacticas: String,
atletas: [
{
idPersonalizado: String,
x: Number,
y: Number,
rol: String,
grupo: String,
direccion: String
}
]
}
],
createdAt: { type: Date, default: Date.now }
});
const Routine = mongoose.model('Routine', routineSchema);
router.get('/', async (req, res) => {
try {
const routines = await Routine.find().populate('participantes', 'name');
res.json(routines);
} catch (error) {
res.status(500).json({ message: 'Error al obtener rutinas', error });
}
});
router.post('/', async (req, res) => {
try {
const newRoutine = new Routine(req.body);
await newRoutine.save();
res.status(201).json({ message: 'Rutina guardada exitosamente', routine: newRoutine });
} catch (error) {
res.status(500).json({ message: 'Error al guardar la rutina', error });
}
});
router.get('/:id', async (req, res) => {
try {
const routine = await Routine.findById(req.params.id).populate('participantes', 'name');
if (!routine) return res.status(404).json({ error: 'Rutina no encontrada' });
res.json(routine);
} catch (error) {
res.status(500).json({ error: 'Error al cargar rutina', details: error });
}
});
router.post('/:id/formations', async (req, res) => {
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id)) {
return res.status(400).json({ error: 'ID inválido para rutina' });
}
try {
const rutina = await Routine.findById(id);
if (!rutina) return res.status(404).json({ error: 'Rutina no encontrada' });
rutina.formaciones.push(req.body);
await rutina.save();
res.status(200).json({ message: 'Formación agregada exitosamente' });
} catch (error) {
res.status(500).json({ error: 'Error al guardar formación', details: error });
}
});
router.get('/:id/formations', async (req, res) => {
try {
const rutina = await Routine.findById(req.params.id);
if (!rutina) return res.status(404).json({ error: 'Rutina no encontrada' });
res.status(200).json(rutina.formaciones);
} catch (error) {
res.status(500).json({ error: 'Error al obtener formaciones', details: error });
}
});
module.exports = router;