Skip to content

Commit

Permalink
fix DTO for multiple data
Browse files Browse the repository at this point in the history
  • Loading branch information
skript023 committed Apr 18, 2024
1 parent ad7e2f6 commit 3d2d281
Show file tree
Hide file tree
Showing 22 changed files with 505 additions and 326 deletions.
4 changes: 3 additions & 1 deletion src/auth/dto/auth.dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace gaboot
{
struct AuthResponse
{
std::string success;
std::string message;
std::string token;

Json::Value to_json()
Expand All @@ -17,6 +19,6 @@ namespace gaboot
return data;
}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(AuthResponse, token)
NLOHMANN_DEFINE_TYPE_INTRUSIVE(AuthResponse, success, message, token)
};
}
12 changes: 6 additions & 6 deletions src/auth/generate_token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ namespace gaboot
return m_token;
}

Json::Value result()
AuthResponse result()
{
Json::Value token;
token["message"] = "Token successfully generated";
token["token"] = m_token;
token["success"] = true;
AuthResponse response;
response.message = "Token successfully generated";
response.token = m_token;
response.success = true;

return token;
return response;
}

static void log_token()
Expand Down
6 changes: 4 additions & 2 deletions src/auth/services/auth_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ namespace gaboot
std::string salt = sha256::encode(fmt::format("{}:{}", username, password) + SECRET + std::to_string(rand()));
auto token = sha256::encode(salt);

m_response.m_data["token"] = token;
m_response.m_data.success = true;
m_response.m_data.message = "Token successfully generated";
m_response.m_data.token = token;

user.updateByJson(m_response.m_data);
user.updateByJson(m_response.m_data.token);

db().update(user);

Expand Down
3 changes: 2 additions & 1 deletion src/interfaces/fwddec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace gaboot
{
struct response_data;
template<class T>
class response_data;
struct customer_detail;
struct item_detail;
struct shipping_address;
Expand Down
30 changes: 8 additions & 22 deletions src/interfaces/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

namespace gaboot
{
template<class T>
struct response_data
template<typename T>
class response_data
{
Json::Value m_data;
public:
std::string m_message;
bool m_success;
int m_last_page;
int m_total_data;
T m_data;

Json::Value to_json()
{
Json::Value json;
m_json = m_data.to_json();
if (!m_message.empty()) json["message"] = m_message;
if (!m_data.empty()) json["data"] = m_data;
if (!m_json.empty()) json["data"] = m_json;
if (m_last_page != 0) json["lastPage"] = m_last_page;
if (m_total_data != 0) json["totalData"] = m_total_data;
json["success"] = m_success;
Expand All @@ -31,28 +33,12 @@ namespace gaboot
void clear()
{
m_message.clear();
m_data.clear();
m_json.clear();
m_success = false;
m_last_page = 0;
m_last_page = 0;
}

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, T>::value, void>::type
operator=(const U& args) {
m_response = args; m_data = m_response.to_json();
}

private:
T m_response;
Json::Value m_json;
};
}
84 changes: 57 additions & 27 deletions src/module/cart/dto/cart.dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,80 @@ namespace gaboot
{
struct CartsResponse
{
CartsResponse() = default;

CartsResponse(Carts const& res):
id(res.getValueOfId()),
customerId(res.getValueOfCustomerId()),
productId(res.getValueOfProductId()),
price(res.getValueOfPrice()),
quantity(res.getValueOfQuantity()),
createdAt(res.getValueOfCreatedAt().toDbStringLocal()),
updatedAt(res.getValueOfUpdatedAt().toDbStringLocal())
{

}

CartsResponse(Carts* res) :
id(res->getValueOfId()),
customerId(res->getValueOfCustomerId()),
productId(res->getValueOfProductId()),
price(res->getValueOfPrice()),
quantity(res->getValueOfQuantity()),
createdAt(res->getValueOfCreatedAt().toDbStringLocal()),
updatedAt(res->getValueOfUpdatedAt().toDbStringLocal())
{
//if (!res) throw NotFoundException("Cart not found");
}

std::string id;
std::string customerId;
std::string productId;
double price;
int32_t quantity;
std::string createdAt;
std::string updatedAt;
std::vector<CartsResponse> m_vector;

Json::Value to_json()
{
nlohmann::json json = *this;
nlohmann::json json;

if (m_vector.empty())
{
json = *this;
}
else
{
for (auto& var : m_vector)
{
json.emplace_back(var);
}
}

Json::Value data;
Json::Reader reader;

reader.parse(json.dump(), data);

return data;
}

template<typename U>
std::enable_if<std::is_same<U, std::vector<Carts>>::value, void>::type operator=(const U& args)
{
for (const auto& res : args)
{
id = res.getValueOfId();
customerId = res.getValueOfCustomerId();
productId = res.getValueOfProductId();
price = res.getValueOfPrice();
quantity = res.getValueOfQuantity();
createdAt = res.getValueOfCreatedAt().toDbStringLocal();
updatedAt = res.getValueOfUpdatedAt().toDbStringLocal();

m_vector.emplace_back(*this);
}
}

template<typename U>
std::enable_if<std::is_same<U, Carts>::value, void>::type operator=(const U& args)
{
id = args.getValueOfId();
customerId = args.getValueOfCustomerId();
productId = args.getValueOfProductId();
price = args.getValueOfPrice();
quantity = args.getValueOfQuantity();
createdAt = args.getValueOfCreatedAt().toDbStringLocal();
updatedAt = args.getValueOfUpdatedAt().toDbStringLocal();
}

template<typename U>
std::enable_if<std::is_same<U, Carts*>::value, void>::type operator=(U args)
{
id = args->getValueOfId();
customerId = args->getValueOfCustomerId();
productId = args->getValueOfProductId();
price = args->getValueOfPrice();
quantity = args->getValueOfQuantity();
createdAt = args->getValueOfCreatedAt().toDbStringLocal();
updatedAt = args->getValueOfUpdatedAt().toDbStringLocal();
}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(CartsResponse, id, customerId, productId, price, quantity, createdAt, updatedAt)
};
}
8 changes: 4 additions & 4 deletions src/module/cart/services/cart_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ namespace gaboot
return HttpResponse::newHttpJsonResponse(m_response.to_json());
}

m_response = carts;

m_response.m_message = "Success retreive carts data";
m_response.m_data = carts;
m_response.m_success = true;

return HttpResponse::newHttpJsonResponse(m_response.to_json());
Expand All @@ -94,13 +94,13 @@ namespace gaboot

this->load_cache();

CartsResponse cart = m_cache_cart.find(id);
auto cart = m_cache_cart.find(id);

if (!&cart) throw NotFoundException("Cart data is not found");
if (!cart) throw NotFoundException("Cart data is not found");

m_response.m_message = "Success retrieve cart data";
m_response.m_success = true;
m_response.m_data = cart.to_json();
m_response.m_data = cart;

return HttpResponse::newHttpJsonResponse(m_response.to_json());
} EXCEPT_CLAUSE
Expand Down
83 changes: 56 additions & 27 deletions src/module/category/dto/category.dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,31 @@ namespace gaboot
{
struct CategoryResponse
{
CategoryResponse() = default;

CategoryResponse(Categories* res) :
id(res->getValueOfId()),
name(res->getValueOfName()),
description(res->getValueOfDescription()),
imagePath(res->getValueOfImagePath()),
thumbnailPath(res->getValueOfThumbnailPath()),
createdAt(res->getValueOfCreatedAt().toDbStringLocal()),
updatedAt(res->getValueOfUpdatedAt().toDbStringLocal())
{

}

CategoryResponse(Categories const& res) :
id(res.getValueOfId()),
name(res.getValueOfName()),
description(res.getValueOfDescription()),
imagePath(res.getValueOfImagePath()),
thumbnailPath(res.getValueOfThumbnailPath()),
createdAt(res.getValueOfCreatedAt().toDbStringLocal()),
updatedAt(res.getValueOfUpdatedAt().toDbStringLocal())
{

}

std::string id;
std::string name;
std::string description;
std::string imagePath;
std::string thumbnailPath;
std::string createdAt;
std::string updatedAt;
std::vector<CategoryResponse> m_vector;

Json::Value to_json()
{
nlohmann::json json = *this;
nlohmann::json json;

if (m_vector.empty())
{
json = *this;
}
else
{
for (auto& var : m_vector)
{
json.emplace_back(var);
}
}

Json::Value data;
Json::Reader reader;

Expand All @@ -55,6 +43,47 @@ namespace gaboot
return data;
}

template<typename U>
std::enable_if<std::is_same<U, std::vector<Categories>>::value, void>::type operator=(const U& args)
{
for (const auto& res : args)
{
id = res.getValueOfId();
name = res.getValueOfName();
description = res.getValueOfDescription();
imagePath = res.getValueOfImagePath();
thumbnailPath = res.getValueOfThumbnailPath();
createdAt = res.getValueOfCreatedAt().toDbStringLocal();
updatedAt = res.getValueOfUpdatedAt().toDbStringLocal();

m_vector.emplace_back(*this);
}
}

template<typename U>
std::enable_if<std::is_same<U, Categories>::value, void>::type operator=(const U& args)
{
id = args.getValueOfId();
name = args.getValueOfName();
description = args.getValueOfDescription();
imagePath = args.getValueOfImagePath();
thumbnailPath = args.getValueOfThumbnailPath();
createdAt = args.getValueOfCreatedAt().toDbStringLocal();
updatedAt = args.getValueOfUpdatedAt().toDbStringLocal();
}

template<typename U>
std::enable_if<std::is_same<U, Categories*>::value, void>::type operator=(U args)
{
id = args->getValueOfId();
name = args->getValueOfName();
description = args->getValueOfDescription();
imagePath = args->getValueOfImagePath();
thumbnailPath = args->getValueOfThumbnailPath();
createdAt = args->getValueOfCreatedAt().toDbStringLocal();
updatedAt = args->getValueOfUpdatedAt().toDbStringLocal();
}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(CategoryResponse, id, name, description, imagePath, thumbnailPath, createdAt, updatedAt)
};
}
8 changes: 4 additions & 4 deletions src/module/category/services/category_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ namespace gaboot

Json::Value data(Json::arrayValue);

m_response = categories;

const size_t lastPage = (categories.size() / (limit + (categories.size() % limit))) == 0 ? 0 : 1;

m_response.m_message = "Success retreive categories data";
m_response.m_data = categories;
m_response.m_success = true;
m_response.m_last_page = lastPage;

Expand All @@ -108,13 +108,13 @@ namespace gaboot

this->load_cache();

CategoryResponse category = m_cache_category.find(id);
auto category = m_cache_category.find(id);

if (!&category) throw NotFoundException("Unable retrieve category detail");
if (!category) throw NotFoundException("Unable retrieve category detail");

m_response.m_message = "Success retrieve category data";
m_response.m_success = true;
m_response.m_data = category.to_json();
m_response.m_data = category;

return HttpResponse::newHttpJsonResponse(m_response.to_json());
} EXCEPT_CLAUSE
Expand Down
Loading

0 comments on commit 3d2d281

Please sign in to comment.