forked from b00tc4mp/isdi-parttime-202406
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modified users and events services and models to add relational data b…
- Loading branch information
Showing
4 changed files
with
88 additions
and
34 deletions.
There are no files selected for viewing
52 changes: 41 additions & 11 deletions
52
staff/borja-garcia/project/appProjectApi/controllers/eventController.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 40 additions & 22 deletions
62
staff/borja-garcia/project/appProjectApi/services/eventService.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
const mongoose = require('mongoose'); | ||
const Event = require('./models/event'); // Ruta al modelo | ||
|
||
mongoose.connect('mongodb://localhost:27017/eventsDB', { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
useCreateIndex: true, | ||
}) | ||
.then(() => console.log('Conectado a MongoDB')) | ||
.catch(err => console.error('Error al conectar a MongoDB:', err)); | ||
|
||
// Crear un evento de ejemplo | ||
const newEvent = new Event({ | ||
eventName: 'Reunión de equipo', | ||
startDateTime: new Date('2024-12-20T10:00:00'), // Fecha y hora de inicio | ||
duration: 120, // Duración en minutos | ||
color: '#FF5733', // Color en formato HEX | ||
}); | ||
|
||
newEvent.save() | ||
.then(event => console.log('Evento creado:', event)) | ||
.catch(err => console.error('Error al crear el evento:', err)); | ||
import Event from "../models/events.js"; | ||
import User from "../models/users.js"; | ||
// Crear un evento | ||
export const createEvent = async (eventObject) => { | ||
const { userId, ...eventData } = eventObject; | ||
|
||
// Verificar si el usuario existe | ||
const user = await User.findById(userId); | ||
if (!user) { | ||
throw new Error("Usuario no encontrado"); | ||
} | ||
|
||
// Crear y guardar el evento | ||
const event = new Event({ ...eventData, user: userId }); | ||
const savedEvent = await event.save(); | ||
|
||
// Asociar el evento al usuario | ||
user.events = user.events || []; | ||
user.events.push(savedEvent._id); // Asegúrate de que el esquema del usuario tenga el campo `events` | ||
await user.save(); | ||
|
||
return savedEvent; | ||
}; | ||
|
||
// Obtener todos los eventos | ||
export const getEvents = async () => { | ||
return await Event.find(); | ||
}; | ||
|
||
export const deleteEventById = async (eventId) => { | ||
return await Event.findByIdAndDelete(eventId); | ||
} | ||
|
||
export const updateEventById = async (eventId, updatedEvent) => { | ||
return await Event.findByIdAndUpdate(eventId, updatedEvent, { new: true }); | ||
} | ||
|
||
export const getEventById = async (eventId) => { | ||
return await Event.findById(eventId); | ||
} |