Skip to content

Commit

Permalink
Merge pull request #56 from codeurjc-students/refactor-code-cleanup-a…
Browse files Browse the repository at this point in the history
…nd-format

Refactor code cleanup and format
  • Loading branch information
DavidMorenoo authored Dec 14, 2024
2 parents 39eab6b + 541f500 commit d9964c7
Show file tree
Hide file tree
Showing 76 changed files with 1,660 additions and 2,042 deletions.
2 changes: 1 addition & 1 deletion LogArtApp/backend/Seeders/dropDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ const dropDatabase = async () => {
}
};

module.exports = dropDatabase;
module.exports = dropDatabase;
40 changes: 19 additions & 21 deletions LogArtApp/backend/Seeders/seedAdmins.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
const mongoose = require('mongoose');
const User = require('../models/user.model');
const bcrypt = require('bcrypt');
const mongoose = require("mongoose");
const User = require("../models/user.model");
const bcrypt = require("bcrypt");

const seedAdmins = async () => {
try {
const admins = [
{
username: 'admin_one',
password: 'admin123',
email: '[email protected]',
firstName: 'Admin',
lastName: 'One',
bio: 'Administrador principal.',
role: 'admin',
username: "admin_one",
password: "admin123",
email: "[email protected]",
firstName: "Admin",
lastName: "One",
bio: "Administrador principal.",
role: "admin",
isVerified: true,
verificationToken: null,
},
{
username: 'admin_two',
password: 'admin123',
email: '[email protected]',
firstName: 'Admin',
lastName: 'Two',
bio: 'Administrador secundario.',
role: 'admin',
username: "admin_two",
password: "admin123",
email: "[email protected]",
firstName: "Admin",
lastName: "Two",
bio: "Administrador secundario.",
role: "admin",
isVerified: true,
verificationToken: null,
},
];

for (const adminData of admins) {
const exist = await User.findOne({ email: adminData.email });
if (!exist) {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(adminData.password, salt);
adminData.password = hashedPassword;

await User.create(adminData);
console.log(`Admin created: ${adminData.email}`);
} else {
console.log(`Admin ${adminData.email} already exists`);
}
}
console.log('Admins seeded successfully');
console.log("Admins seeded successfully");
} catch (error) {
console.error('Error seeding admins:', error);
console.error("Error seeding admins:", error);
process.exit(1);
}
};
Expand Down
69 changes: 37 additions & 32 deletions LogArtApp/backend/Seeders/seedComments.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,84 @@
const mongoose = require('mongoose');
const Comment = require('../models/comment.model');
const ObjectModel = require('../models/object.model');
const User = require('../models/user.model');
const mongoose = require("mongoose");
const Comment = require("../models/comment.model");
const ObjectModel = require("../models/object.model");
const User = require("../models/user.model");

const seedComments = async () => {
try {
const objects = await ObjectModel.find({});
const users = await User.find({ role: 'user' });
const admins = await User.find({ role: 'admin' });

const users = await User.find({ role: "user" });
const admins = await User.find({ role: "admin" });
if (objects.length === 0 || users.length === 0 || admins.length === 0) {
console.log('Ensure that objects and users are seeded before seeding comments.');
console.log(
"Ensure that objects and users are seeded before seeding comments."
);
return;
}

const comments = [
{
content: '¡Me encantó este libro!',
object: objects.find(o => o.name === 'Cien Años de Soledad')._id,
content: "¡Me encantó este libro!",
object: objects.find((o) => o.name === "Cien Años de Soledad")._id,
user: users[0]._id,
},
{
content: 'Bohemian Rhapsody es una obra maestra.',
object: objects.find(o => o.name === 'Bohemian Rhapsody')._id,
content: "Bohemian Rhapsody es una obra maestra.",
object: objects.find((o) => o.name === "Bohemian Rhapsody")._id,
user: users[1]._id,
},
{
content: 'The Legend of Zelda siempre me ha fascinado.',
object: objects.find(o => o.name === 'The Legend of Zelda')._id,
content: "The Legend of Zelda siempre me ha fascinado.",
object: objects.find((o) => o.name === "The Legend of Zelda")._id,
user: users[0]._id,
},
{
content: '¡Controla tu lenguage!',
object: objects.find(o => o.name === 'The Legend of Zelda')._id,
content: "¡Controla tu lenguage!",
object: objects.find((o) => o.name === "The Legend of Zelda")._id,
user: admins[0]._id,
},
{
content: 'Intenta respetar las normas de la comunidad.',
object: objects.find(o => o.name === 'Bohemian Rhapsody')._id,
content: "Intenta respetar las normas de la comunidad.",
object: objects.find((o) => o.name === "Bohemian Rhapsody")._id,
user: admins[1]._id,
},
{
content: '¡Perdón!',
object: objects.find(o => o.name === 'The Legend of Zelda')._id,
content: "¡Perdón!",
object: objects.find((o) => o.name === "The Legend of Zelda")._id,
user: users[0]._id,
},
{
content: 'Este es un aviso para que no lo olvides.',
object: objects.find(o => o.name === 'Cien Años de Soledad')._id,
content: "Este es un aviso para que no lo olvides.",
object: objects.find((o) => o.name === "Cien Años de Soledad")._id,
user: admins[0]._id,
},
{
content: '¡Soy admin, y me encanta este juego!',
object: objects.find(o => o.name === 'The Legend of Zelda 2')._id,
content: "¡Soy admin, y me encanta este juego!",
object: objects.find((o) => o.name === "The Legend of Zelda 2")._id,
user: admins[0]._id,
},
{
content: '¡Me encantó este juego!',
object: objects.find(o => o.name === 'The Legend of Zelda 3')._id,
content: "¡Me encantó este juego!",
object: objects.find((o) => o.name === "The Legend of Zelda 3")._id,
user: admins[1]._id,
},
];

for (const commentData of comments) {
const exist = await Comment.findOne({ content: commentData.content, object: commentData.object, user: commentData.user });
const exist = await Comment.findOne({
content: commentData.content,
object: commentData.object,
user: commentData.user,
});
if (!exist) {
await Comment.create(commentData);
console.log(`Comment created for object ID: ${commentData.object}`);
} else {
console.log(`Comment for object ID: ${commentData.object} already exists`);
console.log(
`Comment for object ID: ${commentData.object} already exists`
);
}
}
console.log('Comments seeded successfully');
console.log("Comments seeded successfully");
} catch (error) {
console.error('Error seeding comments:', error);
console.error("Error seeding comments:", error);
process.exit(1);
}
};
Expand Down
18 changes: 8 additions & 10 deletions LogArtApp/backend/Seeders/seedDB.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const mongoose = require('mongoose');

const seedDisciplines = require('./seedDisciplines');
const seedUsers = require('./seedUsers');
const seedAdmins = require('./seedAdmins');
const seedObjects = require('./seedObjects');
const seedComments = require('./seedComments');
const dropDatabase = require('./dropDatabase');
const mongoose = require("mongoose");
const seedDisciplines = require("./seedDisciplines");
const seedUsers = require("./seedUsers");
const seedAdmins = require("./seedAdmins");
const seedObjects = require("./seedObjects");
const seedComments = require("./seedComments");
const dropDatabase = require("./dropDatabase");

const seedDB = async () => {
try {
Expand All @@ -15,9 +14,8 @@ const seedDB = async () => {
await seedAdmins();
await seedObjects();
await seedComments();

} catch (error) {
console.error('Error seeding the database:', error);
console.error("Error seeding the database:", error);
process.exit(1);
}
};
Expand Down
15 changes: 7 additions & 8 deletions LogArtApp/backend/Seeders/seedDisciplines.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const mongoose = require('mongoose');
const Discipline = require('../models/discipline.model');
const mongoose = require("mongoose");
const Discipline = require("../models/discipline.model");

const seedDisciplines = async () => {
try {
const disciplines = [
{ name: 'Libros', description: 'Libros que has leído' },
{ name: 'Canciones', description: 'Canciones que has escuchado' },
{ name: 'Videojuegos', description: 'Videojuegos que has jugado' },
{ name: "Libros", description: "Libros que has leído" },
{ name: "Canciones", description: "Canciones que has escuchado" },
{ name: "Videojuegos", description: "Videojuegos que has jugado" },
];

for (const discipline of disciplines) {
const exist = await Discipline.findOne({ name: discipline.name });
if (!exist) {
Expand All @@ -18,9 +17,9 @@ const seedDisciplines = async () => {
console.log(`Discipline ${discipline.name} already exists`);
}
}
console.log('Disciplines seeded successfully');
console.log("Disciplines seeded successfully");
} catch (error) {
console.error('Error seeding disciplines:', error);
console.error("Error seeding disciplines:", error);
process.exit(1);
}
};
Expand Down
63 changes: 31 additions & 32 deletions LogArtApp/backend/Seeders/seedObjects.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
const mongoose = require('mongoose');
const ObjectModel = require('../models/object.model');
const Discipline = require('../models/discipline.model');
const User = require('../models/user.model');
const mongoose = require("mongoose");
const ObjectModel = require("../models/object.model");
const Discipline = require("../models/discipline.model");
const User = require("../models/user.model");

const seedObjects = async () => {
try {
const disciplines = await Discipline.find({});
const users = await User.find({ role: 'user' });
const admins = await User.find({ role: 'admin' });

const users = await User.find({ role: "user" });
const admins = await User.find({ role: "admin" });
if (disciplines.length === 0 || users.length === 0) {
console.log('Ensure that disciplines and users are seeded before seeding objects.');
console.log(
"Ensure that disciplines and users are seeded before seeding objects."
);
return;
}

const objects = [
{
name: 'Cien Años de Soledad',
description: 'Una novela clásica de Gabriel García Márquez.',
imageUrl: 'public/images/objects/cien_años.jpg',
discipline: disciplines.find(d => d.name === 'Libros')._id,
name: "Cien Años de Soledad",
description: "Una novela clásica de Gabriel García Márquez.",
imageUrl: "public/images/objects/cien_años.jpg",
discipline: disciplines.find((d) => d.name === "Libros")._id,
createdBy: users[0]._id,
},
{
name: 'Bohemian Rhapsody',
description: 'Canción icónica de Queen.',
imageUrl: 'public/images/objects/bohemian_rhapsody.jpg',
discipline: disciplines.find(d => d.name === 'Canciones')._id,
name: "Bohemian Rhapsody",
description: "Canción icónica de Queen.",
imageUrl: "public/images/objects/bohemian_rhapsody.jpg",
discipline: disciplines.find((d) => d.name === "Canciones")._id,
createdBy: users[1]._id,
},
{
name: 'The Legend of Zelda',
description: 'Un famoso videojuego de aventura.',
imageUrl: 'public/images/objects/zelda.jpg',
discipline: disciplines.find(d => d.name === 'Videojuegos')._id,
name: "The Legend of Zelda",
description: "Un famoso videojuego de aventura.",
imageUrl: "public/images/objects/zelda.jpg",
discipline: disciplines.find((d) => d.name === "Videojuegos")._id,
createdBy: users[0]._id,
},
{
name: 'The Legend of Zelda 2',
description: 'Un famoso videojuego de aventura.',
imageUrl: 'public/images/objects/zelda.jpg',
discipline: disciplines.find(d => d.name === 'Videojuegos')._id,
name: "The Legend of Zelda 2",
description: "Un famoso videojuego de aventura.",
imageUrl: "public/images/objects/zelda.jpg",
discipline: disciplines.find((d) => d.name === "Videojuegos")._id,
createdBy: admins[0]._id,
},
{
name: 'The Legend of Zelda 3',
description: 'Un famoso videojuego de aventura.',
imageUrl: 'public/images/objects/zelda.jpg',
discipline: disciplines.find(d => d.name === 'Videojuegos')._id,
name: "The Legend of Zelda 3",
description: "Un famoso videojuego de aventura.",
imageUrl: "public/images/objects/zelda.jpg",
discipline: disciplines.find((d) => d.name === "Videojuegos")._id,
createdBy: admins[1]._id,
},
];

for (const objectData of objects) {
const exist = await ObjectModel.findOne({ name: objectData.name });
if (!exist) {
Expand All @@ -61,9 +60,9 @@ const seedObjects = async () => {
console.log(`Object ${objectData.name} already exists`);
}
}
console.log('Objects seeded successfully');
console.log("Objects seeded successfully");
} catch (error) {
console.error('Error seeding objects:', error);
console.error("Error seeding objects:", error);
process.exit(1);
}
};
Expand Down
Loading

0 comments on commit d9964c7

Please sign in to comment.