forked from HadesD/eCommerceWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "UserCtrl.h" | ||
|
||
// Add definition of your processing function here | ||
|
||
#include "models/Users.h" | ||
|
||
using User = drogon_model::web_rinphone::Users; | ||
|
||
void UserCtrl::get(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) | ||
{ | ||
auto dbClient = app().getDbClient(); | ||
orm::Mapper<User> usrMap(dbClient); | ||
|
||
Json::Value resJson; | ||
auto& resMsg = resJson["message"]; | ||
HttpStatusCode httpRetCode = HttpStatusCode::k200OK; | ||
|
||
orm::Criteria cnd(User::Cols::_deleted_at, orm::CompareOperator::IsNull); | ||
|
||
try | ||
{ | ||
size_t page; | ||
size_t limit = 24; | ||
|
||
try | ||
{ | ||
page = std::stoul(req->getParameter("page")); | ||
page = (page < 1) ? 1 : page; | ||
} | ||
catch (...) | ||
{ | ||
page = 1; | ||
} | ||
|
||
const auto &usrs = usrMap | ||
.orderBy(User::Cols::_created_at, orm::SortOrder::DESC) | ||
.paginate(page, limit) | ||
.findBy(cnd); | ||
auto &retData = resJson["data"]; | ||
retData = Json::Value(Json::arrayValue); | ||
for (const auto &usr : usrs) | ||
{ | ||
// app_helpers::stockJsonRow(dbClient, stk, retData.append(stk.toJson())); | ||
retData.append(usr.toJson()); | ||
} | ||
|
||
resJson["total"] = static_cast<uint>(usrMap.count(cnd)); | ||
resJson["current_page"] = static_cast<uint>(page); | ||
resJson["per_page"] = static_cast<uint>(limit); | ||
} | ||
catch (const std::exception &e) | ||
{ | ||
LOG_ERROR << e.what(); | ||
|
||
httpRetCode = HttpStatusCode::k500InternalServerError; | ||
} | ||
|
||
auto res = HttpResponse::newHttpJsonResponse(resJson); | ||
res->setStatusCode(httpRetCode); | ||
callback(res); | ||
} | ||
|
||
void UserCtrl::getOne(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, uint64_t id) | ||
{ | ||
|
||
} | ||
|
||
void UserCtrl::create(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback) | ||
{ | ||
|
||
} | ||
|
||
void UserCtrl::updateOne(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, uint64_t id) | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <drogon/HttpController.h> | ||
|
||
using namespace drogon; | ||
|
||
class UserCtrl : public drogon::HttpController<UserCtrl> | ||
{ | ||
public: | ||
METHOD_LIST_BEGIN | ||
// use METHOD_ADD to add your custom processing function here; | ||
// METHOD_ADD(UserCtrl::get, "/{2}/{1}", Get); // path is /UserCtrl/{arg2}/{arg1} | ||
// METHOD_ADD(UserCtrl::your_method_name, "/{1}/{2}/list", Get); // path is /UserCtrl/{arg1}/{arg2}/list | ||
// ADD_METHOD_TO(UserCtrl::your_method_name, "/absolute/path/{1}/{2}/list", Get); // path is /absolute/path/{arg1}/{arg2}/list | ||
|
||
ADD_METHOD_TO(UserCtrl::get, "/users", Get); | ||
ADD_METHOD_TO(UserCtrl::getOne, "/users/{1}", Get); | ||
ADD_METHOD_TO(UserCtrl::create, "/users", Post); | ||
ADD_METHOD_TO(UserCtrl::updateOne, "/users/{1}", Post); | ||
|
||
METHOD_LIST_END | ||
// your declaration of processing function maybe like this: | ||
// void get(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, int p1, std::string p2); | ||
// void your_method_name(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, double p1, int p2) const; | ||
|
||
void get(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback); | ||
void getOne(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, uint64_t id); | ||
void create(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback); | ||
void updateOne(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, uint64_t id); | ||
}; |