Skip to content

Commit

Permalink
add type response
Browse files Browse the repository at this point in the history
  • Loading branch information
skript023 committed Apr 16, 2024
1 parent 0cdbb1f commit 6ba7de0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
22 changes: 18 additions & 4 deletions src/interfaces/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace gaboot
{
template<typename T>
struct response_data
{
Json::Value m_data;
Expand Down Expand Up @@ -36,9 +37,22 @@ namespace gaboot
m_last_page = 0;
}

template<typename T>
inline void operator=(T&& args) { m_data = args.to_json(); }
template<typename T>
inline void operator<<(T&& args) { m_data.append(args.to_json()); }
template<typename U>
typename std::enable_if<std::is_same<U, std::vector<typename U::value_type>>::value, void>::type
operator=(const U& args) {
for (const auto& item : args)
{
m_response = item; m_data.append(m_response.to_json());
}
}

template<typename U>
typename std::enable_if<!std::is_same<U, std::vector<typename U::value_type>>::value, void>::type
operator=(const U& args) {
m_response = args; m_data = m_response.to_json();
}

private:
T m_response;
};
}
16 changes: 8 additions & 8 deletions src/module/order/dto/order.dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ namespace gaboot
id(res->getValueOfId()),
name(res->getValueOfName()),
customerId(res->getValueOfCustomerId()),
totalPrice(res->getTotalPrice()),
totalPrice(res->getValueOfTotalPrice()),
discount(res->getValueOfDiscount()),
grandTotal(res->getValueOfGrandTotal()),
totalItem(res->getTotalItem()),
totalItem(res->getValueOfTotalItem()),
status(res->getValueOfStatus()),
expired(res->getValueOfExpired()),
createdAt(res->getValueOfCreatedAt().toDbStringLocal()),
Expand All @@ -47,10 +47,10 @@ namespace gaboot
id(res.getValueOfId()),
name(res.getValueOfName()),
customerId(res.getValueOfCustomerId()),
totalPrice(res.getTotalPrice()),
totalPrice(res.getValueOfTotalPrice()),
discount(res.getValueOfDiscount()),
grandTotal(res.getValueOfGrandTotal()),
totalItem(res.getTotalItem()),
totalItem(res.getValueOfTotalItem()),
status(res.getValueOfStatus()),
expired(res.getValueOfExpired()),
createdAt(res.getValueOfCreatedAt().toDbStringLocal()),
Expand All @@ -62,10 +62,10 @@ namespace gaboot
std::string id;
std::string name;
std::string customerId;
std::string totalPrice;
std::string discount;
std::string grandTotal;
std::string totalItem;
double totalPrice;
double discount;
double grandTotal;
int totalItem;
std::string status;
std::string expired;
std::string createdAt;
Expand Down
4 changes: 1 addition & 3 deletions src/module/order/services/order_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ namespace gaboot
return HttpResponse::newHttpJsonResponse(m_response.to_json());
}

std::ranges::for_each(orders.begin(), orders.end(), [this](Orders const& order) {
m_response.m_data.append(order.toJson());
});
m_response = orders;

m_response.m_message = "Success retreive orders data";
m_response.m_success = true;
Expand Down
3 changes: 2 additions & 1 deletion src/module/order/services/order_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "interfaces/response.hpp"
#include "order/models/Orders.h"
#include <cache_manager/cache_handler.hpp>
#include <dto/order.dto.hpp>

using namespace drogon;
using namespace orm;
Expand Down Expand Up @@ -46,7 +47,7 @@ namespace gaboot
}
private:
cache_handler<Orders> m_cache_order;
response_data m_response;
response_data<OrderResponse> m_response;
std::string m_error;
};
}

0 comments on commit 6ba7de0

Please sign in to comment.