Skip to content

Commit

Permalink
ajustes
Browse files Browse the repository at this point in the history
  • Loading branch information
rojasadrian012 committed Nov 13, 2023
1 parent 1030199 commit 792276e
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 55 deletions.
15 changes: 15 additions & 0 deletions backend/src/controller/themes/themes.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,24 @@ const eliminar = async function (req, res) {
}
};

const actualizarOrden = async function (req, res) {
try {
await themesService.actualizarOrden(req.body);
res.json({
success: true,
});
} catch (error) {
res.json({
success: false,
error: error.message,
});
}
};

module.exports = {
listar,
busquedaPorCodigo: consultarPorCodigo,
actualizar,
eliminar,
actualizarOrden,
};
12 changes: 8 additions & 4 deletions backend/src/controller/topics/topics.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,15 @@ const listarSharedMeController = async function (req, res) {

const actualizarOrden = async function (req, res) {
try {
const topicos = req.body;
await topicsService.actualizarOrden(topicos);
res.json({ success: true, message: 'Orden actualizado correctamente.' });
await topicsService.actualizarOrden(req.body);
res.json({
success: true,
});
} catch (error) {
res.json({ success: false, error: error.message });
res.json({
success: false,
error: error.message,
});
}
};

Expand Down
5 changes: 5 additions & 0 deletions backend/src/model/themes.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const ThemesModel = sequelize.define("Themes", {
type: DataTypes.INTEGER,
allowNull:true
},
order_index: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
},{
tableName: 'themes',
timestamps: false
Expand Down
41 changes: 23 additions & 18 deletions backend/src/model/topics.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,44 @@ const TopicsModel = sequelize.define("Topics", {
primaryKey: true,
autoIncrement: true,
},
create_date:{
create_date: {
type: DataTypes.TIME,
allowNull:false
allowNull: false
},
name:{
name: {
type: DataTypes.STRING,
allowNull:true
allowNull: true
},
topic_id:{
topic_id: {
type: DataTypes.STRING,
allowNull:true
allowNull: true
},
order:{
order: {
type: DataTypes.INTEGER,
allowNull:true
allowNull: true
},
priority:{
priority: {
type: DataTypes.INTEGER,
allowNull:true
allowNull: true
},
color:{
color: {
type: DataTypes.STRING,
allowNull:true
allowNull: true
},
owner_user_id:{
owner_user_id: {
type: DataTypes.INTEGER,
allowNull:true
allowNull: true
},
},{
tableName: 'topics',
timestamps: false
order_index: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
}, {
tableName: 'topics',
timestamps: false
});

module.exports = {
TopicsModel
TopicsModel
};
5 changes: 5 additions & 0 deletions backend/src/route/themes/themes.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ module.exports = function (app) {
authMiddleware.auth,
themesController.eliminar
);
app.post("/themes/update-order",
authMiddleware.auth,
themesController.actualizarOrden
);

};
4 changes: 4 additions & 0 deletions backend/src/route/topics/topics.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,9 @@ module.exports = function (app) {
authMiddleware.auth,
topicsController.actualizarOrden
);
app.post("/topics/update-order",
authMiddleware.auth,
topicsController.actualizarOrden
);

};
19 changes: 18 additions & 1 deletion backend/src/service/themes.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const listar = async function (textoBuscar) {
FROM themes
WHERE 1=1
AND UPPER(name) LIKE UPPER('%${textoBuscar}%')
ORDER BY id`);
ORDER BY order_index ASC`);
if (themes && themes[0]) {
return themes[0];
} else {
Expand Down Expand Up @@ -77,9 +77,26 @@ const eliminar = async function (codigo) {
}
};

const actualizarOrden = async function (orderData) {
const transaction = await sequelize.transaction();
try {
for (const item of orderData) {
await ThemesModel.update({ order_index: item.order_index }, {
where: { id: item.id },
transaction
});
}
await transaction.commit();
} catch (error) {
await transaction.rollback();
throw error;
}
};

module.exports = {
listar,
busquedaPorCodigo: consultarPorCodigo,
actualizar,
eliminar,
actualizarOrden,
};
17 changes: 12 additions & 5 deletions backend/src/service/topics.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const listar = async function (textoBuscar) {
FROM topics
WHERE 1=1
AND UPPER(name) LIKE UPPER('%${textoBuscar}%')
ORDER BY "order"`);
ORDER BY order_index ASC`);
if (topics && topics[0]) {
return topics[0];
} else {
Expand Down Expand Up @@ -171,13 +171,20 @@ const listarSharedMeService = async function (userId) {
}
};

const actualizarOrden = async function (topicos) {
const actualizarOrden = async function (orderData) {
const transaction = await sequelize.transaction();
try {
for (const topico of topicos) {
await TopicsModel.update({ order: topico.order }, { where: { id: topico.id } });
console.log("qweeeqwe", orderData);
for (const item of orderData) {
await TopicsModel.update({ order_index: item.order_index }, {
where: { id: item.id },
transaction
});
}
await transaction.commit();
} catch (error) {
throw new Error("Error al actualizar el orden en la base de datos");
await transaction.rollback();
throw error;
}
};

Expand Down
11 changes: 9 additions & 2 deletions frontend/src/app/theme-list/theme-list.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<ion-button color="dark" fill="outline" (click)="sortIdDesc()">ID Desc</ion-button>
</div>
</ion-card-content>

<ion-list>
<ion-reorder-group [disabled]="false" (ionItemReorder)="reorder($event)">
<!-- NOMBRE Y DESCRIPCION DEL TEMA-->
Expand All @@ -39,7 +39,14 @@
</ion-reorder-group>

</ion-list>
</ion-card>
<div class="ion-text-center" style="padding-bottom: 5%;">
<ion-button (click)="saveOrder()" shape="round">
Guardar
<ion-icon slot="end" name="save-outline"></ion-icon>
   </ion-button>
</div>

</ion-card>

<!-- AGREGAR TEMA-->
<ion-fab slot="fixed" vertical="bottom" horizontal="end">
Expand Down
28 changes: 24 additions & 4 deletions frontend/src/app/theme-list/theme-list.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ThemeListPage implements OnInit {
private toastController: ToastController,
private alertController: AlertController,
private router: Router
) {}
) { }

ionViewWillEnter(): void {
//verificar si el usuario no esta logueado
Expand All @@ -29,7 +29,7 @@ export class ThemeListPage implements OnInit {
this.getThemes();
}

ngOnInit() {}
ngOnInit() { }

async confirmDelete(id: string) {
const alert = await this.alertController.create({
Expand Down Expand Up @@ -109,14 +109,34 @@ export class ThemeListPage implements OnInit {
await toast.present();
}


//Ordenar visualmente
// Agrega una función para guardar el nuevo orden en el backend
saveOrder() {
let token = localStorage.getItem('token');
let config = {
headers: {
Authorization: token,
},
};
const orderData = this.temas.map((tema: any, index: any) => ({ id: tema.id, order_index: index }));
axios.post('http://localhost:3000/themes/update-order', orderData, config)
.then((result) => {
if (result.data.success) {
this.presentToast('Orden guardado con éxito');
}
})
.catch((error) => {
this.presentToast('Error al guardar el orden: ' + error.message);
});
}

// Modifica la función de reordenamiento para que llame a saveOrder
reorder(event: any) {
const moverItem = this.temas.splice(event.detail.from, 1)[0];
this.temas.splice(event.detail.to, 0, moverItem);
event.detail.complete();
}


//ordenamientos
sortAZ() {
this.temas.sort((a: any, b: any) => {
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/app/topic-list/topic-list.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
<ion-list>
<ion-reorder-group [disabled]="false" (ionItemReorder)="reorder($event)">
<ion-item *ngFor="let topico of topicos">
<ion-label [style.color]="topico.color">{{topico.id}} - {{topico.name}}</ion-label>
<ion-label [style.color]="topico.color">{{topico.id}} - {{topico.name}}</ion-label>
<ion-buttons slot="end">
<input type="color" [(ngModel)]="topico.color" />

<!-- Visualizar -->
<ion-button [routerLink]="'/topic-details/' + topico.id">
<ion-icon slot="icon-only" name="eye-outline" style="font-size: 30px;"></ion-icon>
Expand All @@ -43,7 +45,12 @@
</ion-reorder-group>
</ion-list>

<ion-button color="primary" (click)="saveOrder()">Guardar Orden</ion-button>
<div class="ion-text-center" style="padding-bottom: 5%;">
<ion-button (click)="saveOrder()" shape="round">
Guardar
<ion-icon slot="end" name="save-outline"></ion-icon>
   </ion-button>
</div>

</ion-card>

Expand All @@ -54,7 +61,7 @@
<ion-card-content>
<ion-list>
<ion-item *ngFor="let topico of topicosCompartidosConmigo">
<ion-label [style.color]="topico.color">{{topico.id}} - {{topico.name}}</ion-label>
<ion-label [style.color]="topico.color">{{topico.id}} - {{topico.name}}</ion-label>
<ion-buttons slot="end">
<!-- Visualizar -->
<ion-button [routerLink]="'/topic-details/' + topico.id">
Expand Down
36 changes: 18 additions & 18 deletions frontend/src/app/topic-list/topic-list.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class TopicListPage implements OnInit {
if (result.data.success == true) {
this.topicos = result.data.topicos;
console.log(this.topicos);

} else {
console.log(result.data.error);
}
Expand Down Expand Up @@ -128,21 +128,6 @@ export class TopicListPage implements OnInit {

}

updateOrderInBackend() {
// Aquí, envía los tópicos con el nuevo orden al backend
let token = localStorage.getItem('token');
let config = {
headers: {
Authorization: token,
},
};

axios.post('http://localhost:3000/topics/updateOrder', this.topicos, config)
.then(response => console.log(response))
.catch(error => console.error(error));
}


sortAZ() {
this.topicos.sort((a: any, b: any) => {
const nameA = a.name.toUpperCase();
Expand Down Expand Up @@ -206,7 +191,22 @@ export class TopicListPage implements OnInit {
}

saveOrder() {
this.updateOrderInBackend();
let token = localStorage.getItem('token');
let config = {
headers: {
Authorization: token,
},
};
const orderData = this.topicos.map((tema: any, index: any) => ({ id: tema.id, order_index: index }));
axios.post('http://localhost:3000/topics/update-order', orderData, config)
.then((result) => {
if (result.data.success) {
this.presentToast('Orden guardado con éxito');
}
})
.catch((error) => {
this.presentToast('Error al guardar el orden: ' + error.message);
});
}

}

0 comments on commit 792276e

Please sign in to comment.