Files
yourchat/src/core/user_repository.cpp
Torsten Schulz (local) 83d7484006 Füge Clean Code Refactoring für das YourChat-Projekt hinzu
- Erstelle die Datei `CLEAN_CODE_REFACTORING.md`, die die Ziele und Prinzipien des Clean Code Refactorings beschreibt.
- Implementiere neue Klassen wie `UserRepository`, `WebSocketMessageHandler`, `MessageValidator`, `ConfigurationManager` und `ChatUserClean`, um die Lesbarkeit, Wartbarkeit und Testbarkeit des Codes zu verbessern.
- Füge Methoden zur Fehlerbehandlung, zur Verwendung von Konstanten und zur Anwendung des Factory Patterns hinzu.
- Aktualisiere die `CMakeLists.txt`, um die neuen Quellcodedateien einzuschließen.
- Optimiere die `ChatRoom`- und `ChatUser`-Klassen, um die neuen Strukturen und Prinzipien zu integrieren.
- Füge einen Migrationsleitfaden und Metriken zur Bewertung der Codequalität hinzu.
2025-09-06 00:06:58 +02:00

179 lines
7.6 KiB
C++

#include "user_repository.h"
#include <iostream>
namespace Yc {
namespace Lib {
UserRepository::UserRepository(std::shared_ptr<Database> database)
: _database(database) {
}
Json::Value UserRepository::loadUserData(const std::string& userName) {
if (!_database) {
return createDefaultUserJson(userName, "#000000");
}
try {
// Suche Community-User
Json::Value communityUser = loadCommunityUser(userName);
if (communityUser.isNull()) {
return createDefaultUserJson(userName, "#000000");
}
int falukantUserId = communityUser["id"].asInt();
Json::Value chatUser = loadChatUser(falukantUserId, userName);
if (chatUser.isNull()) {
// Chat-User anlegen
chatUser = createChatUser(userName, "#000000");
}
// Rechte laden
int chatUserId = chatUser["id"].asInt();
Json::Value rights = loadUserRights(chatUserId);
chatUser["rights"] = rights;
return chatUser;
} catch (const std::exception& e) {
#ifdef YC_DEBUG
std::cout << "[Debug] UserRepository: Error loading user data for " << userName << ": " << e.what() << std::endl;
#endif
return createDefaultUserJson(userName, "#000000");
}
}
std::string UserRepository::loadUserColor(const std::string& userName) {
if (!_database) {
return "#000000";
}
try {
std::string query = "SELECT color FROM chat.\"user\" WHERE display_name = '" + userName + "' LIMIT 1;";
auto result = _database->exec(query);
if (result.size() > 0) {
std::string color = result[0]["color"].as<std::string>();
if (!color.empty()) {
return color;
}
}
return "#000000";
} catch (const std::exception& e) {
#ifdef YC_DEBUG
std::cout << "[Debug] UserRepository: Error loading color for " << userName << ": " << e.what() << std::endl;
#endif
return "#000000";
}
}
bool UserRepository::saveUserColor(const std::string& userName, const std::string& color) {
if (!_database) {
return false;
}
try {
std::string query = "UPDATE chat.\"user\" SET color = '" + color + "', updated_at = NOW() WHERE display_name = '" + userName + "';";
_database->exec(query);
return true;
} catch (const std::exception& e) {
#ifdef YC_DEBUG
std::cout << "[Debug] UserRepository: Error saving color for " << userName << ": " << e.what() << std::endl;
#endif
return false;
}
}
Json::Value UserRepository::createChatUser(const std::string& userName, const std::string& color) {
if (!_database) {
return createDefaultUserJson(userName, color);
}
try {
std::string insert = "INSERT INTO chat.\"user\" (falukant_user_id, display_name, color, show_gender, show_age, created_at, updated_at) VALUES (0, '" + userName + "', '" + color + "', true, true, NOW(), NOW()) RETURNING *;";
auto result = _database->exec(insert);
if (!result.empty()) {
const auto& row = result[0];
Json::Value userJson;
userJson["id"] = row["id"].as<int>();
userJson["falukant_user_id"] = row["falukant_user_id"].as<int>();
userJson["display_name"] = row["display_name"].as<std::string>();
userJson["color"] = row["color"].as<std::string>();
userJson["show_gender"] = row["show_gender"].as<bool>();
userJson["show_age"] = row["show_age"].as<bool>();
userJson["created_at"] = row["created_at"].as<std::string>();
userJson["updated_at"] = row["updated_at"].as<std::string>();
return userJson;
}
} catch (const std::exception& e) {
#ifdef YC_DEBUG
std::cout << "[Debug] UserRepository: Error creating chat user for " << userName << ": " << e.what() << std::endl;
#endif
}
return createDefaultUserJson(userName, color);
}
Json::Value UserRepository::loadCommunityUser(const std::string& userName) {
std::string query = "SELECT * FROM community.\"user\" WHERE username = '" + userName + "' LIMIT 1;";
auto result = _database->exec(query);
if (result.empty()) {
return Json::Value::null;
}
const auto& row = result[0];
Json::Value userJson;
userJson["id"] = row["id"].as<int>();
userJson["username"] = row["username"].as<std::string>();
return userJson;
}
Json::Value UserRepository::loadChatUser(int falukantUserId, const std::string& userName) {
std::string query = "SELECT * FROM chat.\"user\" WHERE falukant_user_id = " + std::to_string(falukantUserId) + " LIMIT 1;";
auto result = _database->exec(query);
if (result.empty()) {
return Json::Value::null;
}
const auto& row = result[0];
Json::Value userJson;
userJson["id"] = row["id"].as<int>();
userJson["falukant_user_id"] = row["falukant_user_id"].as<int>();
userJson["display_name"] = row["display_name"].as<std::string>();
userJson["color"] = row["color"].as<std::string>();
userJson["show_gender"] = row["show_gender"].as<bool>();
userJson["show_age"] = row["show_age"].as<bool>();
userJson["created_at"] = row["created_at"].as<std::string>();
userJson["updated_at"] = row["updated_at"].as<std::string>();
return userJson;
}
Json::Value UserRepository::loadUserRights(int chatUserId) {
std::string query = "SELECT r.tr FROM chat.user_rights ur JOIN chat.rights r ON ur.chat_right_id = r.id WHERE ur.chat_user_id = " + std::to_string(chatUserId) + ";";
auto result = _database->exec(query);
Json::Value rights(Json::arrayValue);
for (const auto& row : result) {
rights.append(row["tr"].as<std::string>());
}
return rights;
}
Json::Value UserRepository::createDefaultUserJson(const std::string& userName, const std::string& color) {
Json::Value userJson;
userJson["id"] = 0;
userJson["falukant_user_id"] = 0;
userJson["display_name"] = userName;
userJson["color"] = color;
userJson["show_gender"] = true;
userJson["show_age"] = true;
userJson["created_at"] = "";
userJson["updated_at"] = "";
userJson["rights"] = Json::Value(Json::arrayValue);
return userJson;
}
} // namespace Lib
} // namespace Yc