forked from Arquisoft/wichat_0
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'Back-end' into Back-Ana
- Loading branch information
Showing
21 changed files
with
895 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
name: Build | ||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- master | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"postman.settings.dotenv-detection-notification-visibility": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import {connect, disconnect} from './Connection.js'; | ||
import User from './user.js'; | ||
// Y los demás imports necesarios | ||
|
||
// Clase que contiene los métodos para realizar operaciones CRUD sobre cualquier modelo de la base de datos | ||
class Crud { | ||
|
||
static async createUser(data) { | ||
try { | ||
const newUser = new User(data); | ||
const savedUser = await newUser.save(); | ||
return savedUser; | ||
} catch (error) { | ||
error.message = 'Error al crear el usuario: ' + error.message; | ||
throw error; | ||
} | ||
} | ||
|
||
static async getAllUsers() { | ||
try { | ||
const users = await User.find(); | ||
return users; | ||
} catch (error) { | ||
error.message = 'Error al obtener los usuarios: ' + error.message; | ||
throw error; | ||
} | ||
} | ||
|
||
static async getUserById(userId) { | ||
try { | ||
const user = await User.findById(userId); | ||
return user; | ||
} catch (error) { | ||
error.message = 'Error al obtener el usuario: ' + error.message; | ||
throw error; | ||
} | ||
} | ||
|
||
static async updateUser(userId, updateData) { | ||
try { | ||
const updatedUser = await User.findByIdAndUpdate( | ||
userId, | ||
updateData, | ||
{ new: true } | ||
); | ||
return updatedUser; // Devuelve el usuario actualizado | ||
} catch (error) { | ||
error.message = 'Error al actualizar el usuario: ' + error.message; | ||
throw error; | ||
} | ||
} | ||
|
||
static async deleteUser(userId) { | ||
try { | ||
const deletedUser = await User.findByIdAndDelete(userId); | ||
return deletedUser; // Devuelve el usuario eliminado | ||
} catch (error) { | ||
error.message = 'Error al eliminar el usuario: ' + error.message; | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
connect() | ||
.then(() => { | ||
console.log("Conexión establecida"); | ||
// Se crea el usuario | ||
return Crud.createUser({ | ||
username: 'user1', | ||
password: '123456' | ||
}); | ||
}) | ||
.then(createdUser => { | ||
console.log("Usuario creado:", createdUser); | ||
// Se busca el usuario recién creado usando su _id | ||
return Crud.getUserById(createdUser._id); | ||
}) | ||
.then(foundUser => { | ||
console.log("Usuario encontrado:", foundUser); | ||
console.log(`ID: ${foundUser._id}`); | ||
console.log(`Username: ${foundUser.username}`); | ||
console.log(`Friend Code: ${foundUser.friendCode}`); | ||
console.log(`Password: ${foundUser.password}`); | ||
}) | ||
.catch(error => { | ||
console.error("Error:", error); | ||
}) | ||
.finally(() => { | ||
disconnect(); | ||
}); | ||
|
||
export default Crud; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Schema, model } from "mongoose"; | ||
|
||
// Definir el esquema de usuario | ||
const userSchema = new Schema({ | ||
username: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
friendCode: { | ||
type: String, | ||
unique: true, | ||
required: false | ||
}, | ||
password: { | ||
type: String, | ||
required: true | ||
} | ||
}); | ||
|
||
// Función que genera un número entre 100000 y 999999 en formato String. | ||
function generarCodigoAmigo() { | ||
return Math.floor(100000 + Math.random() * 900000).toString(); | ||
} | ||
|
||
// Antes de guardar, se genera y asigna un friendCode único. | ||
// Usamos `this.constructor` para buscar dentro del mismo modelo, ya que "User" | ||
// aún no está definido en el momento de crear el hook. | ||
userSchema.pre("save", async function (next) { | ||
const user = this; | ||
let codigoValido = false; | ||
let codigoAleatorio; | ||
|
||
while (!codigoValido) { | ||
codigoAleatorio = generarCodigoAmigo(); | ||
|
||
// Verifica si ya existe otro usuario con este friendCode | ||
const existe = await this.constructor.findOne({ friendCode: codigoAleatorio }); | ||
if (!existe) { | ||
codigoValido = true; | ||
} | ||
} | ||
|
||
user.friendCode = codigoAleatorio; | ||
next(); | ||
}); | ||
|
||
// Crear el modelo a partir del esquema | ||
const User = model("User", userSchema); | ||
export default User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
coverage | ||
coverage | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.