Update db_delete_user.c
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Failing after 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Failing after 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Failing after 11s

This commit is contained in:
wt committed 2025-10-23 13:21:12 +07:00
1 parent 1af7497460
commit 5fda74ab1d
1 file changed
+31 -1
+31 -1
View File
@@ -1,8 +1,13 @@
#ifdef USE_SQLITE3
#include "database.h"
#ifdef USE_SQLITE3
#include "sql.h"
#endif
#ifndef USE_SQLITE3
#include "file.h"
#endif
#include "user.h"
bool db_delete_user(const int id) {
@@ -47,6 +52,31 @@ bool db_delete_user(const int id) {
pthread_mutex_unlock(&db_mutex);
return true;
#else
pthread_mutex_lock(&users_file_mutex);
char *data = read_file("users_db.json");
cJSON *users = cJSON_Parse(data);
cJSON *user = nullptr;
int index = 0;
bool found = false;
cJSON_ArrayForEach(user, users) {
cJSON *user_id = cJSON_GetObjectItem(user, "id");
if (user_id->valueint == id) {
cJSON_DeleteItemFromArray(users, index);
found = true;
break;
}
index++;
}
if (!found) {
pthread_mutex_unlock(&users_file_mutex);
return false;
}
char *new_data = cJSON_Print(users);
write_file("users_db.json", new_data);
free(new_data);
free(data);
cJSON_Delete(users);
pthread_mutex_unlock(&users_file_mutex);
return true;
#endif
}