mirror of
https://github.com/typesense/typesense.git
synced 2025-05-18 04:32:38 +08:00
API - creating a new collection.
This commit is contained in:
parent
a82765277f
commit
1baaa2b344
@ -2,6 +2,8 @@
|
||||
|
||||
#include "http_server.h"
|
||||
|
||||
void post_create_collection(http_req & req, http_res & res);
|
||||
|
||||
void get_search(http_req & req, http_res & res);
|
||||
|
||||
void post_add_document(http_req & req, http_res & res);
|
||||
|
@ -23,7 +23,7 @@ struct field {
|
||||
std::string name;
|
||||
std::string type;
|
||||
|
||||
field(std::string name, std::string type): name(name), type(type) {
|
||||
field(const std::string & name, const std::string & type): name(name), type(type) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -18,23 +18,33 @@ struct http_res {
|
||||
uint32_t status_code;
|
||||
std::string body;
|
||||
|
||||
void send_200(const std::string & res_body) {
|
||||
status_code = 200;
|
||||
body = res_body;
|
||||
}
|
||||
|
||||
void send_201(const std::string & res_body) {
|
||||
status_code = 201;
|
||||
body = res_body;
|
||||
}
|
||||
|
||||
void send_400(const std::string & message) {
|
||||
status_code = 400;
|
||||
body = "{\"message\": \"" + message + "\"}";
|
||||
}
|
||||
|
||||
void send_404() {
|
||||
status_code = 404;
|
||||
body = "{\"message\": \"Not Found\"}";
|
||||
}
|
||||
|
||||
void send_409(const std::string & message) {
|
||||
status_code = 400;
|
||||
body = "{\"message\": \"" + message + "\"}";
|
||||
}
|
||||
|
||||
void send_500(const std::string & res_body) {
|
||||
status_code = 404;
|
||||
body = res_body;
|
||||
}
|
||||
|
||||
void send_200(const std::string & res_body) {
|
||||
status_code = 404;
|
||||
body = res_body;
|
||||
}
|
||||
|
||||
void send_201(const std::string & res_body) {
|
||||
status_code = 404;
|
||||
status_code = 500;
|
||||
body = res_body;
|
||||
}
|
||||
};
|
||||
@ -85,7 +95,7 @@ public:
|
||||
|
||||
void post(const std::string & path, void (*handler)(http_req &, http_res &));
|
||||
|
||||
void put();
|
||||
void put(const std::string & path, void (*handler)(http_req &, http_res &));
|
||||
|
||||
void del();
|
||||
|
||||
|
85
src/api.cpp
85
src/api.cpp
@ -3,6 +3,91 @@
|
||||
#include "collection.h"
|
||||
#include "collection_manager.h"
|
||||
|
||||
void post_create_collection(http_req & req, http_res & res) {
|
||||
nlohmann::json req_json = nlohmann::json::parse(req.body);
|
||||
|
||||
CollectionManager & collectionManager = CollectionManager::get_instance();
|
||||
|
||||
// validate presence of mandatory fields
|
||||
|
||||
if(req_json.count("name") == 0) {
|
||||
return res.send_400("Parameter `name` is required.");
|
||||
}
|
||||
|
||||
if(req_json.count("search_fields") == 0) {
|
||||
return res.send_400("Parameter `search_fields` is required.");
|
||||
}
|
||||
|
||||
if(req_json.count("rank_fields") == 0) {
|
||||
return res.send_400("Parameter `rank_fields` is required.");
|
||||
}
|
||||
|
||||
if(collectionManager.get_collection(req_json["name"]) != nullptr) {
|
||||
return res.send_409("Collection with name `" + req_json["name"].get<std::string>() + "` already exists.");
|
||||
}
|
||||
|
||||
// field specific validation
|
||||
|
||||
std::vector<field> search_fields;
|
||||
|
||||
if(!req_json["search_fields"].is_array() || req_json["search_fields"].size() == 0) {
|
||||
return res.send_400("Wrong format for `search_fields`. It should be an array like: "
|
||||
"[{\"name\": \"<field_name>\", \"type\": \"<field_type>\"}]");
|
||||
}
|
||||
|
||||
for(const nlohmann::json & search_field_json: req_json["search_fields"]) {
|
||||
if(!search_field_json.is_object() ||
|
||||
search_field_json.count(fields::name) == 0 || search_field_json.count(fields::type) == 0 ||
|
||||
!search_field_json.at(fields::name).is_string() || !search_field_json.at(fields::type).is_string()) {
|
||||
|
||||
return res.send_400("Wrong format for `search_fields`. It should be an array like: "
|
||||
"[{\"name\": \"<field_name>\", \"type\": \"<field_type>\"}]");
|
||||
}
|
||||
|
||||
search_fields.push_back(field(search_field_json["name"], search_field_json["type"]));
|
||||
}
|
||||
|
||||
std::vector<field> facet_fields;
|
||||
|
||||
if(req_json.count("facet_fields") != 0) {
|
||||
if(!req_json["facet_fields"].is_array()) {
|
||||
return res.send_400("Wrong format for `facet_fields`. It should be an array like: "
|
||||
"[{\"name\": \"<field_name>\", \"type\": \"<field_type>\"}]");
|
||||
}
|
||||
|
||||
for(const nlohmann::json & facet_field_json: req_json["facet_fields"]) {
|
||||
if(!facet_field_json.is_object() ||
|
||||
facet_field_json.count(fields::name) == 0 || facet_field_json.count(fields::type) == 0 ||
|
||||
!facet_field_json.at(fields::name).is_string() || !facet_field_json.at(fields::type).is_string()) {
|
||||
|
||||
return res.send_400("Wrong format for `facet_fields`. It should be an array like: "
|
||||
"[{\"name\": \"<field_name>\", \"type\": \"<field_type>\"}]");
|
||||
}
|
||||
|
||||
facet_fields.push_back(field(facet_field_json["name"], facet_field_json["type"]));
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::string> rank_fields;
|
||||
|
||||
if(!req_json["rank_fields"].is_array() || req_json["rank_fields"].size() == 0) {
|
||||
return res.send_400("Wrong format for `rank_fields`. It should be an array like: "
|
||||
"[\"<field_name_1>, <field_name_2>\"]");
|
||||
}
|
||||
|
||||
for(const nlohmann::json & rank_field: req_json["rank_fields"]) {
|
||||
if(!rank_field.is_string()) {
|
||||
return res.send_400("Wrong format for `rank_fields`. It should be an array like: "
|
||||
"[\"<field_name_1>, <field_name_2>\"]");
|
||||
}
|
||||
|
||||
rank_fields.push_back(rank_field.get<std::string>());
|
||||
}
|
||||
|
||||
collectionManager.create_collection(req_json["name"], search_fields, facet_fields, rank_fields);
|
||||
res.send_201(req.body);
|
||||
}
|
||||
|
||||
void get_search(http_req & req, http_res & res) {
|
||||
const char *NUM_TYPOS = "num_typos";
|
||||
const char *PREFIX = "prefix";
|
||||
|
@ -82,6 +82,7 @@ const char* HttpServer::get_status_reason(uint32_t status_code) {
|
||||
case 201: return "Created";
|
||||
case 400: return "Bad Request";
|
||||
case 404: return "Not Found";
|
||||
case 409: return "Conflict";
|
||||
case 500: return "Internal Server Error";
|
||||
default: return "";
|
||||
}
|
||||
@ -195,8 +196,11 @@ void HttpServer::post(const std::string & path, void (*handler)(http_req &, http
|
||||
routes.push_back(rpath);
|
||||
}
|
||||
|
||||
void HttpServer::put() {
|
||||
|
||||
void HttpServer::put(const std::string & path, void (*handler)(http_req &, http_res &)) {
|
||||
std::vector<std::string> path_parts;
|
||||
StringUtils::split(path, path_parts, "/");
|
||||
route_path rpath = {"PUT", path_parts, handler};
|
||||
routes.push_back(rpath);
|
||||
}
|
||||
|
||||
void HttpServer::del() {
|
||||
|
@ -15,14 +15,9 @@ int main(int argc, char **argv) {
|
||||
|
||||
HttpServer server;
|
||||
|
||||
server.get("/search/:collection", get_search);
|
||||
server.post("/search/:collection", post_add_document);
|
||||
|
||||
/*server.get("/search/:collection", [](http_req & req, http_res & res) -> int {
|
||||
res.status_code = 200;
|
||||
res.body = "{\"collection\": \"" + req.params["collection"] + "\"}";
|
||||
return 0;
|
||||
});*/
|
||||
server.post("/collection", post_create_collection);
|
||||
server.post("/collection/:collection", post_add_document);
|
||||
server.get("/collection/:collection/search", get_search);
|
||||
|
||||
server.run();
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user