Skip to content

Commit

Permalink
Created responses for contacts, events and users. Handled multiple ob…
Browse files Browse the repository at this point in the history
…jects. b00tc4mp#252
  • Loading branch information
itsmePo committed Jan 11, 2025
1 parent 242fbb3 commit 93aa73d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
updateContactById,
} from "../services/contactService.js";
import { getUserById, saveUserContact } from "../services/userService.js";
import { contactResponse } from "../responses/contactResponse.js";
const router = express.Router();

// Ruta para agregar un contacto de emergencia vinculado a un usuario
Expand All @@ -24,50 +25,50 @@ router.post("/users/:userId", async (req, res, next) => {
const contact = await createContact(user, contactData);
await saveUserContact(user, contact);

res.status(201).json({ message: "Contacto de emergencia creado" });
res.status(200).json({ message: "Contacto de emergencia creado" });
} catch (error) {
next(error)
next(error);
}
});

// Ruta para obtener un teléfono de emergencia por ID
router.get("/:id", async (req, res, next) => {
try {
const contact = await getContactById(req.params.id); // Busca por ID
res.status(200).json(contact);
res.status(200).json(contactResponse(contact));
} catch (error) {
next(error)
next(error);
}
});

// Ruta para obtener todos los teléfonos de emergencia
router.get("/", async (req, res, next) => {
try {
const contacts = await getContacts(); // Recupera todos los teléfonos
res.status(200).json(contacts);
res.status(200).json(contactResponse(contacts));
} catch (error) {
next(error)
next(error);
}
});
//Contactos de emergencia por userId
router.get("/users/:userId", async (req, res, next) => {
try {
const user = await getUserById(req.params.userId); // Busca por ID
await getUserById(req.params.userId); // Busca por ID

const contacts = await getContactsByUserId(req.params.userId);
res.status(200).json(contacts);
res.status(200).json(contactResponse(contacts));
} catch (error) {
next(error)
next(error);
}
});

router.delete("/:id", async (req, res, next) => {
try {
const deletedContact = await deleteContactById(req.params.id); // Busca y elimina el contacto por su ID
await deleteContactById(req.params.id); // Busca y elimina el contacto por su ID

res.status(200).json({ message: "Contacto eliminado correctamente" });
} catch (error) {
next(error)
next(error);
}
});
// Ruta para actualizar un contacto de emergencia
Expand All @@ -78,11 +79,11 @@ router.put("/:id", async (req, res, next) => {
phone: req.body.phone,
relationship: req.body.relationship,
};
const updatedContact = await updateContactById(req.params.id, contactData);
await updateContactById(req.params.id, contactData);

res.status(200).json({ message: "Contacto de emergencia actualizado" });
} catch (error) {
next(error)
next(error);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
getEventsByUserId,
} from "../services/eventService.js"; // Importa funciones del controlador
import { getUserById, saveUserEvent } from "../services/userService.js";
import { eventResponse } from "../responses/eventResponse.js";

const router = express.Router();

// Crear un evento
Expand All @@ -19,7 +21,7 @@ router.post("/users/:userId", async (req, res, next) => {
duration: req.body.duration,
color: req.body.color,
category: req.body.category,
userId: req.params.userId
userId: req.params.userId,
};

// Verifica que el usuario exista
Expand All @@ -29,34 +31,36 @@ router.post("/users/:userId", async (req, res, next) => {

res.status(200).json({ message: "Evento creado correctamente" });
} catch (error) {
next(error)
next(error);
}
});

router.get("/:id", async (req, res, next) => {
try {
const event = await getEventById(req.params.id);
res.status(200).json(event);
res.status(200).json(eventResponse(event));
} catch (error) {
next(error) }
next(error);
}
});

// Obtener todos los eventos
router.get("/", async (req, res, next) => {
try {
const events = await getEvents();
res.status(200).json(events);
res.status(200).json(eventResponse(events));
} catch (error) {
next(error) }
next(error);
}
});

router.get("/users/:userId", async (req, res, next) => {
try {
await getUserById(req.params.userId); // Busca por ID
const events = await getEventsByUserId(req.params.userId);
res.status(200).json(events);
res.status(200).json(eventResponse(events));
} catch (error) {
next(error)
next(error);
}
});

Expand All @@ -66,7 +70,8 @@ router.delete("/:id", async (req, res, next) => {

res.status(200).json({ message: "Evento eliminado correctamente" });
} catch (error) {
next(error) }
next(error);
}
});

router.put("/:id", async (req, res, next) => {
Expand All @@ -76,13 +81,14 @@ router.put("/:id", async (req, res, next) => {
startDateTime: req.body.startDateTime,
duration: req.body.duration,
color: req.body.color,
category: req.body.category
category: req.body.category,
};
await updateEventById(req.params.id, updateEvent); // Busca y modifica el usuario por su ID

res.status(200).json({ message: "Evento modificado correctamente" });
} catch (error) {
next(error) }
next(error);
}
});

export default router;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const contactResponse = (...contacts) => {
// Verifica si se ha pasado más de un usuario (es decir, un arreglo)
if (Array.isArray(contacts[0])) {
// Si es un arreglo, formatea cada usuario
return contacts[0].map((contact) => ({
contactName: contact.contactName,
phone: contact.phone,
relationship: contact.relationship,
contactId: contact.id
}));
}

// Si no es un arreglo, formatea solo un usuario
const [contact] = contacts;
return {
contactName: contact.contactName,
phone: contact.phone,
relationship: contact.relationship,
contactId: contact.id
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export const eventResponse = (...events) => {
// Verifica si se ha pasado más de un usuario (es decir, un arreglo)
if (Array.isArray(events[0])) {
// Si es un arreglo, formatea cada usuario
return events[0].map((event) => ({
eventName: event.eventName,
startDateTime: event.startDateTime,
duration: event.duration,
color: event.color,
category: event.category,
eventId: event.id
}));
}

// Si no es un arreglo, formatea solo un usuario
const [event] = events;
return {
eventName: event.eventName,
startDateTime: event.startDateTime,
duration: event.duration,
color: event.color,
category: event.category,
eventId: event.id
};
};
29 changes: 15 additions & 14 deletions staff/borja-garcia/project/appProjectApi/responses/userResponse.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
export const userResponse = (...users) => {
// Verifica si se ha pasado más de un usuario (es decir, un arreglo)
if (Array.isArray(users[0])) {
// Si es un arreglo, formatea cada usuario
return users[0].map(user => ({
email: user.email,
username: user.username,
}));
}

// Si no es un arreglo, formatea solo un usuario
const [user] = users;
return {
// Verifica si se ha pasado más de un usuario (es decir, un arreglo)
if (Array.isArray(users[0])) {
// Si es un arreglo, formatea cada usuario
return users[0].map((user) => ({
email: user.email,
username: user.username,
};
userId: user.id
}));
}

// Si no es un arreglo, formatea solo un usuario
const [user] = users;
return {
email: user.email,
username: user.username,
userId: user.id
};
};

0 comments on commit 93aa73d

Please sign in to comment.