From c0f8e4c4e692fe833e8d975de0628b1a0594b016 Mon Sep 17 00:00:00 2001 From: keqingmoe Date: Mon, 30 Dec 2024 22:21:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9B=86=E5=90=88=E5=A4=84?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97=EF=BC=8C=E6=94=AF=E6=8C=81=E9=9B=86?= =?UTF-8?q?=E5=90=88=E7=9A=84=E5=A2=9E=E5=88=A0=E6=9F=A5=E6=94=B9=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E4=BC=98=E5=8C=96=E9=9B=86=E5=90=88=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/db/sets.h | 27 +++++ include/server/response.h | 5 + src/db/sets.cpp | 136 +++++++++++++++++++++ src/server/response.c | 40 ++++++ src/server/study/sets.c | 248 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 456 insertions(+) create mode 100644 include/db/sets.h create mode 100644 src/db/sets.cpp create mode 100644 src/server/study/sets.c diff --git a/include/db/sets.h b/include/db/sets.h new file mode 100644 index 0000000..e0fe7be --- /dev/null +++ b/include/db/sets.h @@ -0,0 +1,27 @@ +#ifndef DB_SETS_H +#define DB_SETS_H + +#ifdef __cplusplus +extern "C" +{ +#endif + + int open_sets_db(); + + void close_sets_db(); + + int add_set(const char* name, const char* problems, const char* data, int* result); + + int delete_set(int id); + + int get_set(int id, char** result); + + int modify_set(int id, const char* name, const char* problems, const char* data); + + int all_sets(char** result); + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/include/server/response.h b/include/server/response.h index 389386d..2428e89 100644 --- a/include/server/response.h +++ b/include/server/response.h @@ -59,4 +59,9 @@ void res_all_records_by(mg_connection* conn, const char* records); void res_all_records_of(mg_connection* conn, const char* records); void res_records_count(mg_connection* conn, int count); +void res_add_set(mg_connection* conn, int id); +void res_get_set(mg_connection* conn, const char* set); +void res_all_sets(mg_connection* conn, const char* sets); +void res_sets_count(mg_connection* conn, int count); + #endif \ No newline at end of file diff --git a/src/db/sets.cpp b/src/db/sets.cpp new file mode 100644 index 0000000..8ff2bf5 --- /dev/null +++ b/src/db/sets.cpp @@ -0,0 +1,136 @@ +#include "sets.h" +#include "leveldb/options.h" + +#include +#include + + +#include + +#include + + +using leveldb_ptr = leveldb::DB*; + +auto sets_db = leveldb_ptr{nullptr}; +auto max_sets_id = 0; + +extern "C" +{ + int open_sets_db() + { + auto opts = leveldb::Options{}; + + opts.create_if_missing = true; + + auto status = leveldb::DB::Open(opts, "db/sets", &sets_db); + + if (!status.ok()) { + std::println(stderr, "Failed to open sets database: {}", status.ToString()); + return 0; + } + + auto it = sets_db->NewIterator(leveldb::ReadOptions()); + for (it->SeekToFirst(); it->Valid(); it->Next()) { + auto id = std::stoi(it->key().ToString()); + if (id > max_sets_id) max_sets_id = id; + } + + if (!it->status().ok()) { + std::println(stderr, "Failed to get max set id: {}", it->status().ToString()); + delete it; + delete sets_db; + return 0; + } + + delete it; + return 1; + } + + void close_sets_db() + { + delete sets_db; + } + + int add_set(const char* name, const char* problems, const char* data, int* result) + { + auto json = nlohmann::json{ + {"name", name }, + {"problems", nlohmann::json::parse(problems)}, + {"data", nlohmann::json::parse(data) } + }; + auto status = sets_db->Put(leveldb::WriteOptions{}, std::to_string(max_sets_id + 1), json.dump()); + if (!status.ok()) { + std::println(stderr, "Failed to add a set: {}", status.ToString()); + return 0; + } + ++max_sets_id; + *result = max_sets_id; + return 1; + } + + int delete_set(int id) + { + auto status = sets_db->Delete(leveldb::WriteOptions{}, std::to_string(id)); + if (status.IsNotFound()) { + return -1; + } else if (!status.ok()) { + std::println(stderr, "Failed to delete a set: {}", status.ToString()); + return 0; + } + return 1; + } + + int get_set(int id, char** result) + { + auto value = std::string{}; + + auto status = sets_db->Get(leveldb::ReadOptions{}, std::to_string(id), &value); + if (status.IsNotFound()) { + *result = NULL; + } else if (!status.ok()) { + std::println(stderr, "Failed to get a set: {}", status.ToString()); + return 0; + } else { + *result = strdup(value.c_str()); + } + return 1; + } + + int modify_set(int id, const char* name, const char* problems, const char* data) + { + auto json = nlohmann::json{ + {"name", name }, + {"problems", nlohmann::json(problems) }, + {"data", nlohmann::json::parse(data)} + }; + auto status = sets_db->Put(leveldb::WriteOptions{}, std::to_string(id), json.dump()); + if (!status.ok()) { + std::println(stderr, "Failed to add a set: {}", status.ToString()); + return 0; + } + return 1; + } + + int all_sets(char** result) + { + auto problems = std::vector{}; + auto buf = 0; + + auto it = sets_db->NewIterator(leveldb::ReadOptions()); + for (it->SeekToFirst(); it->Valid(); it->Next()) { + problems.emplace_back(std::stoi(it->key().ToString())); + } + + if (!it->status().ok()) { + std::println(stderr, "Failed to get all sets of the problem: {}", it->status().ToString()); + delete it; + return 0; + } + delete it; + auto json = nlohmann::json(problems); + + *result = strdup(json.dump().c_str()); + return 1; + } +} \ No newline at end of file diff --git a/src/server/response.c b/src/server/response.c index 239e82b..b0120af 100644 --- a/src/server/response.c +++ b/src/server/response.c @@ -395,3 +395,43 @@ void res_records_count(mg_connection* conn, int count) "{\"success\":\"success to get the records count\", \"result\":%d}", count); } + +void res_add_set(mg_connection* conn, int id) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"success to add a new set\", \"id\":%d}", + id); +} + +void res_get_set(mg_connection* conn, const char* set) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"success to get the set\", \"set\":%s}", + set); +} + +void res_all_sets(mg_connection* conn, const char* sets) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"success to get all sets\", \"sets\":%s}", + sets); +} + +void res_sets_count(mg_connection* conn, int count) +{ + mg_printf(conn, + "HTTP/1.1 200 OK\r\n" + "Content-Type: application/json\r\n" + "Access-Control-Allow-Origin: *\r\n\r\n" + "{\"success\":\"success to get the sets count\", \"result\":%d}", + count); +} diff --git a/src/server/study/sets.c b/src/server/study/sets.c new file mode 100644 index 0000000..408bbe9 --- /dev/null +++ b/src/server/study/sets.c @@ -0,0 +1,248 @@ +#include "db/sets.h" +#include "jwt/jwt.h" +#include "server/response.h" +#include "server/study.h" +#include "server/types.h" +#include "server/util.h" + + +#include "db/auth.h" + +#include "hash/hash.h" + +#include + +#include + +#include +#include +#include + +typedef struct +{ + char* action; + char* token; + int id; + int has_id; + char* name; + char* problems; + char* data; +} set_form_t; + +static void set_form_dtor(set_form_t* form) +{ + if (form->action) free(form->action); + if (form->token) free(form->token); + if (form->name) free(form->name); + if (form->problems) free(form->problems); + if (form->data) free(form->data); +} + +static int field_found(const char* key, const char* filename, char* path, size_t pathlen, void* user_data) +{ + return MG_FORM_FIELD_HANDLE_GET; +} + +static int field_get(const char* key, const char* value, size_t valuelen, void* user_data) +{ + set_form_t* form = (set_form_t*)user_data; + if (strcmp(key, "action") == 0) { + form->action = kqm_strndup(value, valuelen); + } else if (strcmp(key, "token") == 0) { + form->token = kqm_strndup(value, valuelen); + } else if (strcmp(key, "id") == 0) { + char* id_str = kqm_strndup(value, valuelen); + + form->id = atoi(id_str); + form->has_id = 1; + free(id_str); + } else if (strcmp(key, "name") == 0) { + form->name = kqm_strndup(value, valuelen); + } else if (strcmp(key, "problems") == 0) { + form->problems = kqm_strndup(value, valuelen); + } else if (strcmp(key, "data") == 0) { + form->data = kqm_strndup(value, valuelen); + } + return MG_FORM_FIELD_HANDLE_GET; +} + +static void impl_add(mg_connection* conn, set_form_t* form) +{ + if (!form->name) { + res_need_xxx(conn, "name"); + return; + } + if (!form->problems) { + res_need_xxx(conn, "problem ids"); + return; + } + if (!form->data) { + res_need_xxx(conn, "other data"); + return; + } + + int result; + int flag = add_set(form->name, form->problems, form->data, &result); + if (!flag) { + res_500(conn, "failed to get a set"); + return; + } + + res_add_set(conn, result); +} + +static void impl_delete(mg_connection* conn, set_form_t* form) +{ + if (!form->has_id) { + res_need_xxx(conn, "set id"); + return; + } + + int flag = delete_set(form->id); + if (flag == -1) { + res_404(conn, "the set does not exist"); + } else if (!flag) { + res_500(conn, "failed to delete a set"); + return; + } + + res_200(conn, "successed to delete the set"); +} + +static void impl_query(mg_connection* conn, set_form_t* form) +{ + if (!form->has_id) { + res_need_xxx(conn, "set id"); + return; + } + + char* result = NULL; + int flag = get_set(form->id, &result); + if (!flag) { + res_500(conn, "failed to get a set"); + return; + } + if (!result) { + res_404(conn, "the set does not exist"); + return; + } + + res_get_set(conn, result); + free(result); +} + +static void impl_modify(mg_connection* conn, set_form_t* form) +{ + if (!form->has_id) { + res_need_xxx(conn, "set id"); + return; + } + if (!form->name) { + res_need_xxx(conn, "name"); + return; + } + if (!form->problems) { + res_need_xxx(conn, "problem ids"); + return; + } + if (!form->data) { + res_need_xxx(conn, "other data"); + return; + } + + int flag = modify_set(form->id, form->name, form->problems, form->data); + if (!flag) { + res_500(conn, "failed to modify the set"); + return; + } + + res_200(conn, "successed to modify the set"); +} + +static void impl_all(mg_connection* conn, set_form_t* form) +{ + char* result = NULL; + int flag = all_sets(&result); + if (!flag) { + res_500(conn, "failed to get all sets"); + return; + } + + res_all_sets(conn, result); + free(result); +} + +int sets_handler(mg_connection* conn, void* cbdata) +{ + const mg_request_info* post_body = mg_get_request_info(conn); + + if (post_body == NULL) { + res_null_req(conn); + return 1; + } + + if (strcmp(post_body->request_method, "POST")) { + res_must_post(conn); + return 1; + } + + set_form_t form = {NULL, NULL, 0, 0, NULL, NULL, NULL}; + + mg_form_data_handler set_callback = { + .field_found = field_found, + .field_get = field_get, + .field_store = NULL, + .user_data = &form, + }; + + mg_handle_form_request(conn, &set_callback); + + + if (!form.action) { + res_need_action(conn); + } else if (!form.token) { + res_need_token(conn); + } else { + char* user_id = get_payload(form.token); + int result; + int flag = get_user_permission(user_id, &result); + if (!flag) { + res_check_permission_fail(conn); + } else if (result != 1 && result != 2) { + res_permission_denied(conn); + } else { + if (!strcmp(form.action, "add")) { + if (result == 2) { + res_permission_denied(conn); + } else { + impl_add(conn, &form); + } + } else if (!strcmp(form.action, "delete")) { + if (result == 2) { + res_permission_denied(conn); + } else { + impl_delete(conn, &form); + } + } else if (!strcmp(form.action, "query")) { + if (result == 2) { + res_permission_denied(conn); + } else { + impl_query(conn, &form); + } + } else if (!strcmp(form.action, "modify")) { + if (result == 2) { + res_permission_denied(conn); + } else { + impl_modify(conn, &form); + } + } else if (!strcmp(form.action, "all")) { + impl_all(conn, &form); + } else { + res_bad_action(conn); + } + } + } + + set_form_dtor(&form); + return 1; +}