Files
auth_service_c/src/db_get_users_from_file.c
T

30 lines
1.0 KiB
C

#include <cJSON.h>
#include <sqlite3.h>
#include "file.h"
#include "user.h"
void db_get_users_from_file(const char *path) {
char *users_string = read_file(path);
cJSON *json_array = cJSON_Parse(users_string);
for (int i = 0; i < cJSON_GetArraySize(json_array); i++) {
cJSON *json_object = cJSON_GetArrayItem(json_array, i);
const char *username =
cJSON_GetObjectItem(json_object, "username")->valuestring;
const char *email = cJSON_GetObjectItem(json_object, "email")->valuestring;
const char *password =
cJSON_GetObjectItem(json_object, "password")->valuestring;
const char *group = cJSON_GetObjectItem(json_object, "group")->valuestring;
const bool is_superuser =
cJSON_GetObjectItem(json_object, "is_superuser")->valueint;
User *new_db_user =
db_insert_user(username, email, password, group, &is_superuser);
if (!new_db_user) {
fprintf(stderr, "Cannot insert user: %s\n", username);
}
free_user(new_db_user);
}
cJSON_Delete(json_array);
free(users_string);
}