添加集合处理模块,支持集合的增删查改功能,优化集合管理流程
This commit is contained in:
parent
2d0dacd495
commit
c0f8e4c4e6
27
include/db/sets.h
Normal file
27
include/db/sets.h
Normal file
@ -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
|
@ -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
|
136
src/db/sets.cpp
Normal file
136
src/db/sets.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include "sets.h"
|
||||
#include "leveldb/options.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <print>
|
||||
|
||||
|
||||
#include <leveldb/db.h>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
|
||||
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<int>{};
|
||||
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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
248
src/server/study/sets.c
Normal file
248
src/server/study/sets.c
Normal file
@ -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 <civetweb.h>
|
||||
|
||||
#include <cjson/cJSON.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user