updated file database added get_all_users get_user_by_id
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s

get_user_by_username get_user_field_by_id
This commit is contained in:
wt committed 2025-10-22 16:46:36 +07:00
1 parent 73a73976fe
commit d14db43e9b
13 files changed
+158 -9

No files matched your search

+30 -2
View File
@@ -1,7 +1,12 @@
#include "cJSON.h"
#include "database.h"
#include "sql.h"
#include "user.h"
#ifndef USE_SQLITE3
#include "file.h"
#endif
User *db_get_user_by_username(const char *username) {
#ifdef USE_SQLITE3
User *user = nullptr;
@@ -43,7 +48,30 @@ User *db_get_user_by_username(const char *username) {
pthread_mutex_unlock(&db_mutex);
return user;
#else
User *asdf = new_user(0, "asdf", "asdf", "asdf", "asdf", false);
return asdf;
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
User *found = nullptr;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
cJSON *user_username = cJSON_GetObjectItem(user, "username");
cJSON *user_password = cJSON_GetObjectItem(user, "password");
cJSON *user_email = cJSON_GetObjectItem(user, "email");
cJSON *user_group = cJSON_GetObjectItem(user, "group");
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
if (!strcmp(user_username->valuestring, username)) {
found = new_user(user_id->valueint, user_username->valuestring,
user_password->valuestring, user_email->valuestring,
user_group->valuestring, user_is_superuser->valueint);
break;
}
}
if (found == nullptr) {
pthread_mutex_unlock(&users_file_mutex);
return nullptr;
}
pthread_mutex_unlock(&users_file_mutex);
return found;
#endif
}