added endpoints end refactor
This commit is contained in:
1 parent
22dc118616
commit
c4ea7a4293
15 files changed
+333
-87
No files matched your search
@@ -0,0 +1,55 @@
|
||||
#include "user.h"
|
||||
|
||||
void register_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, optional>, <is_superuser, optional>"));
|
||||
return;
|
||||
}
|
||||
cJSON *jsonbody = cJSON_Parse(hm->body.buf);
|
||||
cJSON *username = cJSON_GetObjectItem(jsonbody, "username");
|
||||
cJSON *password = cJSON_GetObjectItem(jsonbody, "password");
|
||||
cJSON *email = cJSON_GetObjectItem(jsonbody, "email");
|
||||
cJSON *group = cJSON_GetObjectItem(jsonbody, "group");
|
||||
cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser");
|
||||
if (username == nullptr || password == nullptr || email == nullptr) {
|
||||
mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"),
|
||||
MG_ESC("must have <username>, <password>, <email>, <group, optional>, <is_superuser, optional>"));
|
||||
return;
|
||||
}
|
||||
if (group == nullptr) {
|
||||
group = cJSON_CreateString("users");
|
||||
}
|
||||
if (is_superuser == nullptr) {
|
||||
is_superuser = cJSON_CreateBool(false);
|
||||
}
|
||||
const User *new_user_data = new_user(
|
||||
username->valuestring,
|
||||
password->valuestring,
|
||||
email->valuestring,
|
||||
group->valuestring,
|
||||
is_superuser->valueint
|
||||
);
|
||||
if (new_user_data == nullptr) {
|
||||
mg_http_reply(c, 500, "", "{%m:%m}", MG_ESC("error"), MG_ESC("failed to create user"));
|
||||
return;
|
||||
}
|
||||
char responsedata[2048] = {0};
|
||||
snprintf(
|
||||
responsedata,
|
||||
sizeof(responsedata),
|
||||
"{\"id\": %d, \"username\": \"%s\", \"email\": \"%s\", \"group\": \"%s\", \"is_superuser\": %d}",
|
||||
new_user_data->id,
|
||||
new_user_data->username,
|
||||
new_user_data->email,
|
||||
new_user_data->group,
|
||||
new_user_data->is_superuser
|
||||
);
|
||||
|
||||
mg_http_reply(
|
||||
c,
|
||||
200,
|
||||
"Content-Type: application/json\r\n",
|
||||
responsedata
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user