Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore project refinement #39

Merged
merged 6 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ dist-ssr
*.njsproj
*.sln
*.sw?
LogArtApp/backend/.env
LogArtApp/backend/.env
LogArtApp/backend/public/images/
LogArtApp/backend/ssl/server.key
LogArtApp/backend/ssl/server.cert
8 changes: 0 additions & 8 deletions LogArtApp/backend/.env

This file was deleted.

52 changes: 52 additions & 0 deletions LogArtApp/backend/Seeders/seedAdmins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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',
isVerified: true,
verificationToken: null,
},
{
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');
} catch (error) {
console.error('Error seeding admins:', error);
process.exit(1);
}
};

module.exports = seedAdmins;
71 changes: 71 additions & 0 deletions LogArtApp/backend/Seeders/seedComments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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' });

if (objects.length === 0 || users.length === 0 || admins.length === 0) {
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,
user: users[0]._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,
user: users[0]._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,
user: admins[1]._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,
user: admins[0]._id,
},
];

for (const commentData of comments) {
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('Comments seeded successfully');
} catch (error) {
console.error('Error seeding comments:', error);
process.exit(1);
}
};

module.exports = seedComments;
23 changes: 23 additions & 0 deletions LogArtApp/backend/Seeders/seedDB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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 seedDB = async () => {
try {
await seedDisciplines();
await seedUsers();
await seedAdmins();
await seedObjects();
await seedComments();

} catch (error) {
console.error('Error seeding the database:', error);
process.exit(1);
}
};

module.exports = seedDB;
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const Discipline = require('../models/discipline.model');
const seedDisciplines = async () => {
try {
const disciplines = [
{ name: 'Libros', description: 'Libros que has leido' },
{ 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 @@ -19,10 +19,10 @@ const seedDisciplines = async () => {
}
}
console.log('Disciplines seeded successfully');

} catch (error) {
console.error('Error seeding disciplines:', error);
process.exit(1);
}
};
module.exports = seedDisciplines;

module.exports = seedDisciplines;
56 changes: 56 additions & 0 deletions LogArtApp/backend/Seeders/seedObjects.js
Original file line number Diff line number Diff line change
@@ -0,0 +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 seedObjects = async () => {
try {
const disciplines = await Discipline.find({});
const users = await User.find({ role: 'user' });

if (disciplines.length === 0 || users.length === 0) {
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,
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,
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,
createdBy: users[0]._id,
},
];

for (const objectData of objects) {
const exist = await ObjectModel.findOne({ name: objectData.name });
if (!exist) {
await ObjectModel.create(objectData);
console.log(`Object created: ${objectData.name}`);
} else {
console.log(`Object ${objectData.name} already exists`);
}
}
console.log('Objects seeded successfully');
} catch (error) {
console.error('Error seeding objects:', error);
process.exit(1);
}
};

module.exports = seedObjects;
52 changes: 52 additions & 0 deletions LogArtApp/backend/Seeders/seedUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const mongoose = require('mongoose');
const User = require('../models/user.model');
const bcrypt = require('bcrypt');

const seedUsers = async () => {
try {
const users = [
{
username: 'john_doe',
password: 'hola123',
email: '[email protected]',
firstName: 'John',
lastName: 'Doe',
bio: 'Apasionado por los libros y la música.',
role: 'user',
isVerified: true,
verificationToken: null,
},
{
username: 'jane_smith',
password: 'hola123',
email: '[email protected]',
firstName: 'Jane',
lastName: 'Smith',
bio: 'Amante de los videojuegos y la tecnología.',
role: 'user',
isVerified: true,
verificationToken: null,
},
];

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

await User.create(userData);
console.log(`User created: ${userData.email}`);
} else {
console.log(`User ${userData.email} already exists`);
}
}
console.log('Users seeded successfully');
} catch (error) {
console.error('Error seeding users:', error);
process.exit(1);
}
};

module.exports = seedUsers;
Loading