Skip to content

Commit

Permalink
Endpoints eventos
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlotaBergel committed Mar 17, 2024
1 parent e03ad69 commit a7591ea
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 64 deletions.
239 changes: 205 additions & 34 deletions src/controller/event.controller.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,229 @@
const { pool } = require('../database');
//const { connection } = require('../database');
const {pool} = require("../database");
const Evento = require("../models/evento");
const User = require("../models/user");
const Response = require("../models/response");


// Devuelve todos los eventos
const getAllEvents = async (req, res, next) => {

console.log("SE LLAMA AL GET_ALL_EVENTS");

let sql_AllEvents = "SELECT e.*, u_creador.*"+
"FROM evento e " +
"JOIN userEvent eu ON e.id_event = eu.id_event "+
"JOIN user u_creador ON eu.id_user = u_creador.id_user";

// SQL -->SELECT e.*, u_participante.*, u_creador.id_user
// FROM evento e
// JOIN userEvent eu ON e.id_event = eu.id_event
// JOIN user u_participante ON eu.id_user = u_participante.id_user //Hasta esta linea optiene datos del eventos + datos de los participantes
// JOIN user u_creador ON eu.id_user = u_creador.id_user WHERE participation = 1 //Quitando la de arriba y poniendo esta se recupera evento y su creador.

let respuesta;
let eventos=[];

try {
console.log('get decks try');
} catch {
console.log('get decks catch');
let [result] = await pool.query(sql_AllEvents);
console.log(result);

//Generamos el objeto que devemos devolver
for(let i=0; i<result.length; i++){

//console.log(typeof(result[i].nameEvent));
let event = {"id":result[i].id_event,"title":result[i].nameEvent,"date":result[i].date, "hour":result[i].hour, "place":result[i].place,"descriptionEvent":result[i].descriptionEvent, "direction":result[i].direction, "creator":{"id_user":result[i].id_user, "nameUser":result[i].nameUser}};
console.log(event);
// let event = new Event(result[i].id_event, result[i].nameEvent, result[i].date, result[i].hour, result[i].place, result[i].descriptionEvent, result[i].direction, new User(null,result[i].nameUser, null, null,null,null,null));
console.log(event);
eventos.push(event);

}
console.log(eventos);
respuesta = new Response(false, 200, null, eventos);
res.send(respuesta);

} catch(err) {
console.log(err);
respuesta = new Response(true, 400, null, err);
res.send(respuesta);
}
}

// Devuelve solo mis eventos
const getMyEvents = async (req, res, next) => {
try {
console.log('get decks try');
} catch {
console.log('get decks catch');
}
}

const getOthersEvents = async (req, res, next) => {
console.log("SE LLAMA AL GET_MYEVENTS");

console.log(req.params.id_user);

let sql_AllEvents = "SELECT e.*, u_creador.*"+
"FROM evento e " +
"JOIN userEvent eu ON e.id_event = eu.id_event "+
"JOIN user u_creador ON eu.id_user = u_creador.id_user WHERE eu.id_user = ? AND eu.creator = 1";

let respuesta;
let eventos=[];

try {
console.log('get decks try');
} catch {
console.log('get decks catch');
let [result] = await pool.query(sql_AllEvents,[req.params.id_user]);
console.log(result);

//Generamos el objeto que devemos devolver
for(let i=0; i<result.length; i++){

let event = {"id":result[i].id_event,"title":result[i].nameEvent,"date":result[i].date, "hour":result[i].hour, "place":result[i].place,"descriptionEvent":result[i].descriptionEvent, "direction":result[i].direction, "creator":{"id_user":result[i].id_user, "nameUser":result[i].nameUser}};
console.log(event);
//let event = new Event(result[i].id_event, result[i].nameEvent, result[i].date, result[i].hour, result[i].place, result[i].descriptionEvent, result[i].direction, new User(null,result[i].nameUser, null, null,null,null,null));
console.log(event);
eventos.push(event);

}
console.log(eventos);

respuesta = new Response(false, 200, null, eventos);
res.send(respuesta);

} catch(err) {
console.log(err);
respuesta = new Response(true, 400, null, err);
res.send(respuesta);
}
}


//CREAR EVENTO
//Tenemos todos los datos del evento y del usuario logueado que es quien pulsa en crear evento
const addMyEvent = async (req, res, next) => {

//Hay que cambiar cosas en el modelo de evento
let evento = new Evento(null, req.body.title, req.body.date, req.body.hour, req.body.place, req.body.description, req.body.direction, new User(req.body.creator.id_user,null, null, null,null,null,null));
let params = [evento.title, evento.descriptionEvent, evento.date, evento.hour, evento.place, evento.direction];

let respuesta;

console.log(evento);

let sql_InsertEvent = "INSERT INTO evento(nameEvent, descriptionEvent, date, hour, place, direction) VALUES (?,?,?,?,?,?)";
let sql_InsertUserEvent = "INSERT INTO userEvent (id_event, id_user, creator) VALUES (?, ?, 1);";

//Tenemos que realizar una inserción en eventos, recoger ese evento para insertar en users_events
try {
console.log('get decks try');
} catch {
console.log('get decks catch');
let [result] = await pool.query(sql_InsertEvent,params);
let idEventoInsertado = result.insertId;
console.log(result);
console.log(idEventoInsertado);

let [result2] = await pool.query(sql_InsertUserEvent,[idEventoInsertado, evento.creator.id_user]);
console.log(result2);

evento.id = idEventoInsertado;
respuesta = new Response(false, 200, "El evento se inserta correctamente", evento);

res.send(respuesta);
} catch(err){
console.log(err);
respuesta = new Response(true, 400, null, err);
res.send(respuesta);
}
}

// EDITA UN EVENTO
// Este método se llama al pulsar en la pluma de un evento, por lo que tenemos todos los datos asociados al evento, recibimos esos datos a traves del body
const editMyEvent = async (req, res, next) => {

console.log("SE LLAMA AL EDITAR EVENTO");

console.log(req.body.date);
console.log(req.body.hour);

let evento = new Evento(req.body.id, req.body.title, req.body.date, req.body.hour, req.body.place, req.body.description, req.body.direction, null);
let params = [evento.nameEvent, evento.descriptionEvent, evento.date, evento.hour, evento.place, evento.direction, evento.id];

let sql_UpdateEvent = "UPDATE evento SET nameEvent = COALESCE(?, nameEvent), descriptionEvent = COALESCE (?, descriptionEvent), " +
"date = COALESCE(?, date), hour = COALESCE(?, hour), place = COALESCE(?, place), direction = COALESCE(?, direction) WHERE id_event = ? ";

let respuesta;

try {
console.log('get decks try');
} catch {
console.log('get decks catch');
let[result] = await pool.query(sql_UpdateEvent, params);
console.log(result);

respuesta = new Response(false, 200, "El evento se actualiza correctamente", null);

res.send(respuesta);
} catch (err) {
console.log(err);
respuesta = new Response(true, 400, null, err);
res.send(respuesta);
}
}

// BUSCA POR ID_EVENT E ID_USER EN LA TABLA DE ASOCIACION EVENT_USER SI EXISTE, LO ELIMINA, SI NO, TE AÑADE.
// Se llama desde el bóton participar del evento, por lo que tenemos el id_evento y el usuario logueado (id_user)
const editParticipation = async (req, res, next) => {

console.log("SE LLAMA AL EDITAR PARTICIPACIÓN");
console.log(req.query.id_event);
console.log(req.query.id_user);

let respuesta;
let sqlFind_UserEvent = "SELECT id_user, id_event FROM userEvent WHERE id_event = ? AND id_user = ?";
let sqlDelete_UserEvent = "DELETE FROM userEvent WHERE id_event = ? AND id_user = ?";
let sqlCreate_UserEvent = "INSERT INTO userEvent (id_event, id_user, creator) VALUES (?, ?, 0);";

try {
console.log('get decks try');
} catch {
console.log('get decks catch');
let [resultFind] = await pool.query(sqlFind_UserEvent,[req.query.id_event, req.query.id_user]);
console.log(resultFind);

if(resultFind.length > 0)
{
let [resultDelete] = await pool.query(sqlDelete_UserEvent,[req.query.id_event, req.query.id_user]);
console.log(resultDelete);

respuesta = new Response(false, 200, "Dejas de participar en el evento", null);
}else{
let [resultCreate] = await pool.query(sqlCreate_UserEvent,[req.query.id_event, req.query.id_user]);
console.log(resultCreate);
respuesta = new Response(false, 200, "Participas en el evento", null);
}

res.send(respuesta);

} catch(err) {
console.log(err);
respuesta = new Response(true, 400, null, err);
res.send(respuesta);
}
}

const deleteMyEvent = async (req, res, next) => {
// ELIMINA UN EVENTO
// Este método se llama al pulsar en la calavera de un evento, por lo que tenemos el id del evento, recibimos ese id por query param
const deleteMyEvent = async (req, res, next) => {

console.log("SE LLAMA AL DELETE EVENT")
console.log(req.query.id_event);

let respuesta;

let sqlDelete_UserEvent = "DELETE FROM userEvent WHERE id_event = ?";
let sqlDelete_Event = "DELETE FROM evento WHERE id_event = ?";

try {
console.log('get decks try');
} catch {
console.log('get decks catch');
//Como se llama desde el propio evento no hace falta comprobar si existe
//No falla si no existe el evento

let [result] = await pool.query(sqlDelete_UserEvent,[req.query.id_event]);
let [result2] = await pool.query(sqlDelete_Event,[req.query.id_event]);

respuesta = new Response(false, 200, "Se elimina el evento correctamente", null);

res.send(respuesta);

} catch(err) {
console.log(err);
respuesta = new Response(true, 400, null, err);
res.send(respuesta);
}
}

Expand All @@ -68,11 +239,11 @@ const deleteMyEvent = async (req, res, next) => {


module.exports = {
getAllEvents,
getMyEvents,
getOthersEvents,
addMyEvent,
editMyEvent,
editParticipation,
deleteMyEvent
getAllEvents, //todo Ok
getMyEvents, //todo Ok
// getOthersEvents,
addMyEvent, //todo Ok
editMyEvent, //todo Ok
editParticipation, //todo Ok
deleteMyEvent //todo Ok
};
13 changes: 13 additions & 0 deletions src/models/evento.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Evento {
constructor(id, title, date, hour, place, descriptionEvent, direction, creator) {
this.id = id;
this.title = title;
this.date = date;
this.hour = hour;
this.place = place;
this.descriptionEvent = descriptionEvent;
this.direction = direction;
this.creator = creator;
}
}
module.exports = Evento;
16 changes: 0 additions & 16 deletions src/models/evento.ts

This file was deleted.

9 changes: 9 additions & 0 deletions src/models/response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Response {
constructor( err, code, message, data){
this.err = err;
this.code = code;
this.message = message;
this.data = data;
}
}
module.exports = Response;
12 changes: 12 additions & 0 deletions src/models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class User {
constructor(id_user, nameUser, emailUser, description, passwordUser, avatar, icon) {
this.id_user = id_user;
this.nameUser = nameUser;
this.emailUser = emailUser;
this.description = description;
this.passwordUser = passwordUser;
this.avatar = avatar;
this.icon = icon;
}
}
module.exports = User;
13 changes: 0 additions & 13 deletions src/models/user.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/routers/event.routers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ router.get('/eventos/?:id_user', eventCtrl.getMyEvents);

router.get('/eventos/?:id_event', eventCtrl.getMyEvents);

router.get('/eventos/', eventCtrl.getOthersEvents); // hay que cambiar endpoint
// router.get('/eventos/', eventCtrl.getOthersEvents); // hay que cambiar endpoint

router.post('/eventos', eventCtrl.addMyEvent);

Expand Down

0 comments on commit a7591ea

Please sign in to comment.