Skip to content

Commit

Permalink
hkuserver continue
Browse files Browse the repository at this point in the history
  • Loading branch information
fasiondog committed Apr 12, 2021
1 parent ca764aa commit 84857ea
Showing 11 changed files with 170 additions and 29 deletions.
61 changes: 61 additions & 0 deletions hikyuu_cpp/hikyuu/utilities/db_connect/TableMacro.h

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions hikyuu_cpp/hikyuu_server/common/uuid.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-12
* Author: fasiondog
*/

#pragma once

#include <string>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

namespace hku {

inline std::string UUID() {
static auto rgen = boost::uuids::random_generator();
return boost::uuids::to_string(rgen());
}

} // namespace hku
10 changes: 5 additions & 5 deletions hikyuu_cpp/hikyuu_server/http/HttpError.h
Original file line number Diff line number Diff line change
@@ -30,11 +30,11 @@ enum HttpErrorCode {
WRONG_PARAMETER_TYPE // 参数类型错误(各个业务接口返回各个接口的参数)
};

#define HTTP_VALID_CHECK(expr, errcode, errmsg) \
{ \
if (!(expr)) { \
throw HttpError(errcode, errmsg); \
} \
#define HTTP_VALID_CHECK(expr, errcode, ...) \
{ \
if (!(expr)) { \
throw HttpError(errcode, fmt::format(__VA_ARGS__)); \
} \
}

class HttpError : public HttpException {
14 changes: 14 additions & 0 deletions hikyuu_cpp/hikyuu_server/service/RestErrorCode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-13
* Author: fasiondog
*/

#pragma once

namespace hku {

enum TradeErrorCode { TD_ACCOUNT_REPETITION = 20000 };

}
1 change: 1 addition & 0 deletions hikyuu_cpp/hikyuu_server/service/RestHandle.h
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
#pragma once

#include "http/HttpHandle.h"
#include "RestErrorCode.h"

namespace hku {

43 changes: 43 additions & 0 deletions hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright(C) 2021 hikyuu.org
*
* Create on: 2021-04-12
* Author: fasiondog
*/

#include "common/uuid.h"
#include "TradeAccountHandle.h"
#include "TradeService.h"

namespace hku {

void AddTradeAccountHandle::run() {
json req = getReqJson();
HTTP_VALID_CHECK(req.contains("name"), HttpErrorCode::MISS_PARAMETER,
R"(Missing param "name")");
HTTP_VALID_CHECK(req.contains("type"), HttpErrorCode::MISS_PARAMETER,
R"(Missing param "type")");
TradeAccountModel account;
std::string name = req["name"].get<std::string>();
account.setName(name);
account.setType(req["type"].get<std::string>());
account.setAccount(UUID());
auto con = TradeService::getDBConnect();
{
TransAction trans(con);
HTTP_VALID_CHECK(!TradeAccountModel::isExistName(con, name),
TradeErrorCode::TD_ACCOUNT_REPETITION, "Name repetition");
con->save(account, false);
}
json res;
to_json(res, account);
setResData(res);
}

void GetTradeAccountHandle::run() {}

void ModTradeAccountHandle::run() {}

void DelTradeAccountHandle::run() {}

} // namespace hku
24 changes: 5 additions & 19 deletions hikyuu_cpp/hikyuu_server/service/trade/TradeAccountHandle.h
Original file line number Diff line number Diff line change
@@ -7,44 +7,30 @@

#pragma once

#include <iostream>
#include "../RestHandle.h"
#include "model/TradeAccountModel.h"

namespace hku {

class AddTradeAccountHandle : public RestHandle {
REST_HANDLE_IMP(AddTradeAccountHandle)

virtual void run() override {
json req = getReqJson();
HTTP_VALID_CHECK(req.contains("name"), HttpErrorCode::MISS_PARAMETER,
R"(Missing param "name")");
HTTP_VALID_CHECK(req.contains("type"), HttpErrorCode::MISS_PARAMETER,
R"(Missing param "type")");
TradeAccountModel account;
account.setName(req["name"].get<std::string>());
account.setType(req["type"].get<std::string>());
LOG_INFO("{}", req);
setResData(R"([{"result": true, "account": "12345"}])");
}
virtual void run() override;
};

class GetTradeAccountHandle : public RestHandle {
REST_HANDLE_IMP(GetTradeAccountHandle)

virtual void run() override {}
virtual void run() override;
};

class ModTradeAccountHandle : public RestHandle {
REST_HANDLE_IMP(ModTradeAccountHandle)

virtual void run() override {}
virtual void run() override;
};

class DelTradeAccountHandle : public RestHandle {
REST_HANDLE_IMP(DelTradeAccountHandle)

virtual void run() override {}
virtual void run() override;
};

} // namespace hku
2 changes: 1 addition & 1 deletion hikyuu_cpp/hikyuu_server/service/trade/TradeService.h
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@ class TradeService : public HttpService {
}

public:
DBConnectPtr getDBConnect();
static DBConnectPtr getDBConnect();

private:
static void initTradeServiceSqlite(const Parameter &param);
5 changes: 4 additions & 1 deletion hikyuu_cpp/hikyuu_server/service/trade/db/sqlite/create.cpp
Original file line number Diff line number Diff line change
@@ -14,13 +14,16 @@ const char *g_sqlite_create_db{
CREATE TABLE "td_account" (
"id" INTEGER NOT NULL UNIQUE,
"account" TEXT NOT NULL UNIQUE,
"name" TEXT NOT NULL,
"name" TEXT NOT NULL UNIQUE,
"type" TEXT NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE UNIQUE INDEX "ix_td_account_on_account" ON "td_account" (
"account" ASC
);
CREATE UNIQUE INDEX "ix_td_account_on_name" ON "td_account" (
"name" ASC
);
CREATE TABLE "td_funds" (
"id" INTEGER NOT NULL UNIQUE,
"td_id" INTEGER NOT NULL,
17 changes: 14 additions & 3 deletions hikyuu_cpp/hikyuu_server/service/trade/model/TradeAccountModel.h
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@
#pragma once

#include <nlohmann/json.hpp>
#include <hikyuu/utilities/db_connect/TableMacro.h>
#include <hikyuu/utilities/db_connect/DBConnect.h>

using nlohmann::json;

@@ -17,6 +17,18 @@ namespace hku {
class TradeAccountModel {
TABLE_BIND3(td_account, account, name, type)

public:
static bool isExistName(DBConnectPtr con, const std::string& name) {
SQLStatementPtr st = con->getStatement(
fmt::format(R"(select count(id) from {} where name="{}")", getTableName(), name));
st->exec();
st->moveNext();
int result = 0;
st->getColumn(0, result);
return result != 0;
}

public:
std::string getAccount() const {
return account;
}
@@ -51,11 +63,10 @@ class TradeAccountModel {
};

inline void to_json(json& j, const TradeAccountModel& p) {
j = json{{"id", p.m_id}, {"account", p.account}, {"name", p.name}, {"type", p.type}};
j = json{{"account", p.account}, {"name", p.name}, {"type", p.type}};
}

inline void from_json(const json& j, TradeAccountModel& p) {
j.at("id").get_to(p.m_id);
j.at("account").get_to(p.account);
j.at("name").get_to(p.name);
j.at("type").get_to(p.type);
Binary file modified test_data/trader.db
Binary file not shown.

0 comments on commit 84857ea

Please sign in to comment.