Initial commit

This commit is contained in:
2025-03-13 16:05:09 +01:00
commit 5950d5ae9d
44 changed files with 5505 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
const mySql = require("mysql2");
const UserManager = require("./UserManager/UserManager");
class DataBaseManager {
constructor(host, database) {
this.connection = mySql.createConnection({
host: host,
user: "root",
password:"",
database: database
});
this.connection.connect((err) => {
if (err) {
throw err;
}
});
this.usermanager = new UserManager(this.connection);
}
}
module.exports = DataBaseManager;

View File

@@ -0,0 +1,27 @@
const bcrypt = require("bcrypt");
class User {
constructor(userJSON) {
this.id = userJSON.id;
this.username = userJSON.username;
this.email = userJSON.email;
this.password = userJSON.password;
this.fullName = userJSON.fullName;
this.createdAt = userJSON.createdAt;
}
async doesPassMatch(passwordPlain) {
return await bcrypt.compare(passwordPlain, this.password);
}
toUserJSON(){
return {
id: this.id,
username: this.username,
email: this.email,
fullName: this.fullName,
}
}
}
module.exports = User;

View File

@@ -0,0 +1,102 @@
const mySql = require("mysql2")
const bcrypt = require("bcrypt")
const User = require("./User")
class UserManager {
/**@param {mySql.Connection} connection*/
constructor(connection) {
this.connection = connection;
}
// Gebe zumindest {username: "x", password: "x"} mit!
// 0: Fehler (User Existiert schon oder Fehler) | 1: Funktioniert (User Erstellt!)
async createUser(userJson) {
if (!userJson.username || !userJson.password) return 0;
const user = await this.getUser({username: userJson.username});
if(user) return 0;
try {
const passHash = await bcrypt.hash(userJson.password, 10);
this.connection.query(`INSERT INTO users (username, email, password, fullName) VALUES ("${userJson.username}",` +
` ${userJson.email ? `"${userJson.email}"` : "NULL"}, "${passHash}", ${userJson.fullName ? `"${userJson.fullName}"` : "NULL"})`);
return 1;
} catch (error) {
console.log(error);
}
}
// Gebe nichts mit um alle user zu bekommen oder {id: 1} oder {username: "x"} um darauf einen bestimmten zu bekommen;
async getUser(userData) {
let selectionString = "SELECT * FROM users";
if(userData?.id){
selectionString = `SELECT * FROM users WHERE id=${userData.id}`;
}
else if(userData?.username){
selectionString = `SELECT * FROM users WHERE username="${userData.username}"`;
}
try {
const response = await this.connection.promise().query(selectionString);
if(response[0].length === 0){
return null;
}
else if(response[0].length === 1){
const x = response[0][0];
return new User(x);
}
else{
let userArray = [];
response[0].forEach(x => {
const user = new User(x);
userArray.push(user);
});
return userArray;
}
} catch (error) {
console.log(error);
}
}
// 0: User Existiert nicht | 1: Funktioniert
// id = 1 | newUserData = {email: "x", password: "xx", "fullName": "xxx"} <-- BRaucht alle Argeumente sonst null, username nicht änderbar
async updateUser(id, newUserData){
const user = await this.getUser({id});
if(!user) return 0;
if(!newUserData.password) return 0;
try {
this.connection.query(
`UPDATE users SET
username="${newUserData.username}",
email=${newUserData.email ? `"${newUserData.email}"` : "NULL"},
fullName=${newUserData.fullName ? `"${newUserData.fullName}"` : "NULL"},
password="${newUserData.password}"
WHERE id = ${id}`
)
return 1;
} catch (error) {
console.log(error)
}
}
// 0: Es gibt den User nicht | 1: Funktioniert
async delteUser(id){
const user = await this.getUser({id});
if(!user) return 0;
try {
this.connection.query(`DELETE FROM users where id=${id}`);
return 1;
} catch (error) {
console.log(error)
}
}
}
module.exports = UserManager;