Skip to content

Commit

Permalink
add customer dto
Browse files Browse the repository at this point in the history
  • Loading branch information
skript023 committed Apr 17, 2024
1 parent 2851dc8 commit 5f4c4e9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 78 deletions.
8 changes: 0 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@ include(CheckIncludeFileCXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if (WIN32)
message("Windows detected, add windows toolchain from ${PROJECT_SOURCE_DIR}/conan/Windows/build/Release/generators/conan_toolchain.cmake")
include("${PROJECT_SOURCE_DIR}/conan/Windows/build/Release/generators/conan_toolchain.cmake")
elseif (UNIX)
message("Linux detected, add linux toolchain from ${PROJECT_SOURCE_DIR}/conan/Linux/build/Release/generators/conan_toolchain.cmake")
include("${PROJECT_SOURCE_DIR}/conan/Linux/build/Release/generators/conan_toolchain.cmake")
endif()

#include vcpkg dependencies
message("\nFetching packages")
find_package(Drogon CONFIG REQUIRED)
Expand Down
3 changes: 2 additions & 1 deletion CMakeSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"value": "x64-linux",
"type": "STRING"
}
]
],
"cmakeToolchain": "${projectDir}\\conan\\Linux\\build\\Release\\generators\\conan_toolchain.cmake"
}
]
}
92 changes: 42 additions & 50 deletions src/module/customer/dto/customer.dto.hpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
#pragma once
#include <pch.h>

using namespace drogon;
using namespace orm;
using namespace drogon_model::gaboot;

namespace gaboot
{
struct ActualCategoryRespose
{
std::string id;
std::string firstname;
std::string lastname;
std::string username;
std::string email;
std::string phone_number;
std::string address_detail;
double latitude;
double longitude;
std::string password;
std::string token;
bool is_active;
std::string image_path;
std::string thumbnail_path;
std::string created_at;
std::string updated_at;

NLOHMANN_DEFINE_TYPE_INTRUSIVE(ActualCustomerResponse, id, firstname, lastname, username, email, phone_number, address_detail, latitude, longitude, password, token, is_active, image_path, thumbnail_path, created_at, updated_at)
};

struct CustomerResponse
{
CustomerResponse() = default;

CustomerResponse(std::unique_ptr<ActualCustomerResponse> const& res):
id(res->id),
firstname(res->firstname),
lastname(res->lastname),
username(res->username),
email(res->email),
phoneNumber(res->phone_number),
addressDetail(res->address_detail),
latitude(res->latitude),
longitude(res->longitude),
password(res->password),
token(res->token),
isactive(res->is_active),
imagepath(res->image_path),
thumbnailPath(res->thumbnail_path),
createdAt(res->created_at),
updatedAt(res->updated_at)
CustomerResponse(MasterCustomers* res):
id(res->getValueOfId()),
firstname(res->getValueOfFirstname()),
lastname(res->getValueOfLastname()),
username(res->getValueOfUsername()),
email(res->getValueOfEmail()),
phoneNumber(res->getValueOfPhoneNumber()),
addressDetail(res->getValueOfAddressDetail()),
latitude(res->getValueOfLatitude()),
longitude(res->getValueOfLongitude()),
password(res->getValueOfPassword()),
token(res->getValueOfToken()),
isActive(res->getValueOfIsActive()),
imagePath(res->getValueOfImagePath()),
thumbnailPath(res->getValueOfThumbnailPath()),
createdAt(res->getValueOfCreatedAt().toDbStringLocal()),
updatedAt(res->getValueOfUpdatedAt().toDbStringLocal())
{

}

CustomerResponse(MasterCustomers const& res):
id(res.getValueOfId()),
firstname(res.getValueOfFirstname()),
lastname(res.getValueOfLastname()),
username(res.getValueOfUsername()),
email(res.getValueOfEmail()),
phoneNumber(res.getValueOfPhoneNumber()),
addressDetail(res.getValueOfAddressDetail()),
latitude(res.getValueOfLatitude()),
longitude(res.getValueOfLongitude()),
password(res.getValueOfPassword()),
token(res.getValueOfToken()),
isActive(res.getValueOfIsActive()),
imagePath(res.getValueOfImagePath()),
thumbnailPath(res.getValueOfThumbnailPath()),
createdAt(res.getValueOfCreatedAt().toDbStringLocal()),
updatedAt(res.getValueOfUpdatedAt().toDbStringLocal())
{

}
Expand All @@ -67,17 +70,6 @@ namespace gaboot
std::string createdAt;
std::string updatedAt;

CustomerResponse from_json(Json::Value const& json)
{
auto njson = nlohmann::json::parse(json.toStyledString());

const auto response = std::make_unique<ActualCustomerResponse>(njson.get<ActualCustomerResponse>());

*this = response;

return *this;
}

Json::Value to_json()
{
nlohmann::json json = *this;
Expand Down
13 changes: 4 additions & 9 deletions src/module/customer/services/customer_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ namespace gaboot
return HttpResponse::newHttpJsonResponse(m_response.to_json());
}

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

std::ranges::for_each(customers.begin(), customers.end(), [this](MasterCustomers const& customer) {
m_response.m_data.append(customer.toJson());
});
m_response = customers;

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

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

return HttpResponse::newHttpJsonResponse(m_response.to_json());
Expand Down Expand Up @@ -109,13 +104,13 @@ namespace gaboot
throw BadRequestException("Requirement doesn't match");
}

const auto user = db().findByPrimaryKey(id);
CustomerResponse user = db().findByPrimaryKey(id);

if (!user.getId()) throw NotFoundException("Unable retrieve customer detail");
//if (!user.getId()) throw NotFoundException("Unable retrieve customer detail");

m_response.m_message = "Success retrieve customers data";
m_response.m_success = true;
m_response.m_data = user.toJson();
m_response = user;

return HttpResponse::newHttpJsonResponse(m_response.to_json());
} EXCEPT_CLAUSE
Expand Down
3 changes: 2 additions & 1 deletion src/module/customer/services/customer_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "validator/validator.hpp"
#include "interfaces/response.hpp"
#include "module/customer/models/MasterCustomers.h"
#include <dto/customer.dto.hpp>

using namespace drogon;
using namespace orm;
Expand Down Expand Up @@ -38,7 +39,7 @@ namespace gaboot
HttpResponsePtr getImage(HttpRequestPtr const&, std::string&&);
HttpResponsePtr getThumbnail(HttpRequestPtr const&, std::string&&);
private:
response_data m_response;
response_data<CustomerResponse> m_response;
std::string m_error;
Json::Value m_data;
};
Expand Down
6 changes: 3 additions & 3 deletions src/module/order/services/order_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ namespace gaboot

this->load_cache();

const auto order = m_cache_order.find(id);
OrderResponse order = m_cache_order.find(id);

if (!order) throw NotFoundException("Order data is not found");
//if (!order) throw NotFoundException("Order data is not found");

m_response.m_message = "Success retrieve order data";
m_response.m_success = true;
m_response.m_data = order->toJson();
m_response = order;

return HttpResponse::newHttpJsonResponse(m_response.to_json());
} EXCEPT_CLAUSE
Expand Down
2 changes: 1 addition & 1 deletion src/module/wishlist/dto/wishlist.dto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ namespace gaboot
return data;
}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(WishlistResponse, id, name, description, imagePath, thumbnailPath, createdAt, updatedAt)
NLOHMANN_DEFINE_TYPE_INTRUSIVE(WishlistResponse, id, productId, category, createdAt, updatedAt)
};
}
5 changes: 1 addition & 4 deletions src/module/wishlist/services/wishlist_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ namespace gaboot
return HttpResponse::newHttpJsonResponse(m_response.to_json());
}

std::ranges::for_each(wishlists.begin(), wishlists.end(), [this](Wishlists const& wishlist) {
m_response.m_data.append(wishlist.toJson());
});

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

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


using namespace drogon;
Expand Down Expand Up @@ -47,7 +48,7 @@ namespace gaboot
}
private:
cache_handler<Wishlists> m_cache_wishlist;
response_data m_response;
response_data<WishlistResponse> m_response;
std::string m_error;
};
}

0 comments on commit 5f4c4e9

Please sign in to comment.