Skip to content

Commit

Permalink
Update to the latest oatpp API.
Browse files Browse the repository at this point in the history
  • Loading branch information
lganzzzo committed May 23, 2020
1 parent 2300856 commit a155876
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 54 deletions.
16 changes: 8 additions & 8 deletions book-service/src/book-service/controller/BookController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class BookController : public oatpp::web::server::api::ApiController {

ENDPOINT_INFO(createBook) {
info->summary = "Create new Book";
info->addConsumes<dto::BookDto>("application/json");
info->addResponse<dto::BookDto>(Status::CODE_200, "application/json");
info->addConsumes<Object<dto::BookDto>>("application/json");
info->addResponse<Object<dto::BookDto>>(Status::CODE_200, "application/json");
}
ENDPOINT("POST", "/books", createBook,
BODY_DTO(dto::BookDto, bookDto)) {
BODY_DTO(Object<dto::BookDto>, bookDto)) {
OATPP_ASSERT_HTTP(bookDto->authorId, Status::CODE_400, "'authorId' is require!");
return createDtoResponse(Status::CODE_200, m_database->createBook(bookDto));
}
Expand All @@ -43,15 +43,15 @@ class BookController : public oatpp::web::server::api::ApiController {
ENDPOINT_INFO(putBook) {
// general
info->summary = "Update Book by bookId";
info->addConsumes<dto::BookDto>("application/json");
info->addResponse<dto::BookDto>(Status::CODE_200, "application/json");
info->addConsumes<Object<dto::BookDto>>("application/json");
info->addResponse<Object<dto::BookDto>>(Status::CODE_200, "application/json");
info->addResponse<String>(Status::CODE_404, "text/plain");
// params specific
info->pathParams["bookId"].description = "Book Identifier";
}
ENDPOINT("PUT", "/books/{bookId}", putBook,
PATH(Int64, bookId),
BODY_DTO(dto::BookDto, bookDto)) {
BODY_DTO(Object<dto::BookDto>, bookDto)) {
bookDto->id = bookId;
return createDtoResponse(Status::CODE_200, m_database->updateBook(bookDto));
}
Expand All @@ -60,7 +60,7 @@ class BookController : public oatpp::web::server::api::ApiController {
ENDPOINT_INFO(getBookById) {
// general
info->summary = "Get one Book by bookId";
info->addResponse<dto::BookDto>(Status::CODE_200, "application/json");
info->addResponse<Object<dto::BookDto>>(Status::CODE_200, "application/json");
info->addResponse<String>(Status::CODE_404, "text/plain");
// params specific
info->pathParams["bookId"].description = "Book Identifier";
Expand All @@ -75,7 +75,7 @@ class BookController : public oatpp::web::server::api::ApiController {

ENDPOINT_INFO(getBooks) {
info->summary = "get all stored books";
info->addResponse<List<dto::BookDto>>(Status::CODE_200, "application/json");
info->addResponse<List<Object<dto::BookDto>>>(Status::CODE_200, "application/json");
}
ENDPOINT("GET", "/books", getBooks) {
return createDtoResponse(Status::CODE_200, m_database->getBooks());
Expand Down
14 changes: 7 additions & 7 deletions book-service/src/book-service/db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace example { namespace book { namespace db {

model::Book Database::serializeFromDto(const dto::BookDto::ObjectWrapper &bookDto) {
model::Book Database::serializeFromDto(const oatpp::Object<dto::BookDto> &bookDto) {
model::Book book;
if (bookDto->id) {
book.id = bookDto->id;
Expand All @@ -13,23 +13,23 @@ model::Book Database::serializeFromDto(const dto::BookDto::ObjectWrapper &bookDt
return book;
}

dto::BookDto::ObjectWrapper Database::deserializeToDto(const model::Book &book) {
oatpp::Object<dto::BookDto> Database::deserializeToDto(const model::Book &book) {
auto dto = dto::BookDto::createShared();
dto->id = book.id;
dto->title = book.title;
dto->authorId = book.authorId;
return dto;
}

dto::BookDto::ObjectWrapper Database::createBook(const dto::BookDto::ObjectWrapper &bookDto) {
oatpp::Object<dto::BookDto> Database::createBook(const oatpp::Object<dto::BookDto> &bookDto) {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto book = serializeFromDto(bookDto);
book.id = m_idCounter++;
m_booksById[book.id] = book;
return deserializeToDto(book);
}

dto::BookDto::ObjectWrapper Database::updateBook(const dto::BookDto::ObjectWrapper &bookDto) {
oatpp::Object<dto::BookDto> Database::updateBook(const oatpp::Object<dto::BookDto> &bookDto) {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto book = serializeFromDto(bookDto);
if (book.id < 0) {
Expand All @@ -44,7 +44,7 @@ dto::BookDto::ObjectWrapper Database::updateBook(const dto::BookDto::ObjectWrapp
return deserializeToDto(book);
}

dto::BookDto::ObjectWrapper Database::getBookById(v_int64 id) {
oatpp::Object<dto::BookDto> Database::getBookById(v_int64 id) {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto it = m_booksById.find(id);
if (it == m_booksById.end()) {
Expand All @@ -53,9 +53,9 @@ dto::BookDto::ObjectWrapper Database::getBookById(v_int64 id) {
return deserializeToDto(it->second);
}

oatpp::data::mapping::type::List<dto::BookDto::ObjectWrapper>::ObjectWrapper Database::getBooks() {
oatpp::List<oatpp::Object<dto::BookDto>> Database::getBooks() {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto result = oatpp::data::mapping::type::List<dto::BookDto::ObjectWrapper>::createShared();
auto result = oatpp::List<oatpp::Object<dto::BookDto>>::createShared();
auto it = m_booksById.begin();
while (it != m_booksById.end()) {
result->push_back(deserializeToDto(it->second));
Expand Down
14 changes: 7 additions & 7 deletions book-service/src/book-service/db/Database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Database {
v_int64 m_idCounter; ///< counter to generate bookIds
std::unordered_map<v_int64, model::Book> m_booksById; ///< Map bookId to Book
private:
model::Book serializeFromDto(const dto::BookDto::ObjectWrapper& bookDto);
dto::BookDto::ObjectWrapper deserializeToDto(const model::Book& book);
model::Book serializeFromDto(const oatpp::Object<dto::BookDto>& bookDto);
oatpp::Object<dto::BookDto> deserializeToDto(const model::Book& book);
public:

Database()
Expand All @@ -37,11 +37,11 @@ class Database {
createBook(dto::BookDto::createShared(0, 5, "The Catcher in the Rye"));

}
dto::BookDto::ObjectWrapper createBook(const dto::BookDto::ObjectWrapper& bookDto);
dto::BookDto::ObjectWrapper updateBook(const dto::BookDto::ObjectWrapper& bookDto);
dto::BookDto::ObjectWrapper getBookById(v_int64 id);
oatpp::data::mapping::type::List<dto::BookDto::ObjectWrapper>::ObjectWrapper getBooks();

oatpp::Object<dto::BookDto> createBook(const oatpp::Object<dto::BookDto>& bookDto);
oatpp::Object<dto::BookDto> updateBook(const oatpp::Object<dto::BookDto>& bookDto);
oatpp::Object<dto::BookDto> getBookById(v_int64 id);
oatpp::List<oatpp::Object<dto::BookDto>> getBooks();
bool deleteBook(v_int64 id);

};
Expand Down
4 changes: 2 additions & 2 deletions book-service/src/book-service/dto/BookDto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace example { namespace book { namespace dto {

#include OATPP_CODEGEN_BEGIN(DTO)

class BookDto : public oatpp::Object {
class BookDto : public oatpp::DTO {

DTO_INIT(BookDto, Object)
DTO_INIT(BookDto, DTO)

DTO_FIELD(Int64, id);
DTO_FIELD(Int64, authorId, "authorId");
Expand Down
4 changes: 2 additions & 2 deletions facade/src/facade/dto/BookDto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace example { namespace facade { namespace dto {

#include OATPP_CODEGEN_BEGIN(DTO)

class BookDto : public oatpp::Object {
class BookDto : public oatpp::DTO {

DTO_INIT(BookDto, Object)
DTO_INIT(BookDto, DTO)

DTO_FIELD(Int64, id);
DTO_FIELD(Int64, authorId, "authorId");
Expand Down
4 changes: 2 additions & 2 deletions facade/src/facade/dto/BookInfoDto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace example { namespace facade { namespace dto {

#include OATPP_CODEGEN_BEGIN(DTO)

class BookInfoDto : public oatpp::Object {
class BookInfoDto : public oatpp::DTO {

DTO_INIT(BookInfoDto, Object)
DTO_INIT(BookInfoDto, DTO)

DTO_FIELD(Int64, id);
DTO_FIELD(String, title, "title");
Expand Down
4 changes: 2 additions & 2 deletions facade/src/facade/dto/UserDto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace example { namespace facade { namespace dto {

#include OATPP_CODEGEN_BEGIN(DTO)

class UserDto : public oatpp::Object {
class UserDto : public oatpp::DTO {

DTO_INIT(UserDto, Object)
DTO_INIT(UserDto, DTO)

DTO_FIELD(Int64, id);
DTO_FIELD(String, name, "name");
Expand Down
16 changes: 8 additions & 8 deletions user-service/src/user-service/controller/UserController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ class UserController : public oatpp::web::server::api::ApiController {

ENDPOINT_INFO(createUser) {
info->summary = "Create new User";
info->addConsumes<dto::UserDto>("application/json");
info->addResponse<dto::UserDto>(Status::CODE_200, "application/json");
info->addConsumes<Object<dto::UserDto>>("application/json");
info->addResponse<Object<dto::UserDto>>(Status::CODE_200, "application/json");
}
ENDPOINT("POST", "/users", createUser,
BODY_DTO(dto::UserDto, userDto)) {
BODY_DTO(Object<dto::UserDto>, userDto)) {
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
}


ENDPOINT_INFO(putUser) {
// general
info->summary = "Update User by userId";
info->addConsumes<dto::UserDto>("application/json");
info->addResponse<dto::UserDto>(Status::CODE_200, "application/json");
info->addConsumes<Object<dto::UserDto>>("application/json");
info->addResponse<Object<dto::UserDto>>(Status::CODE_200, "application/json");
info->addResponse<String>(Status::CODE_404, "text/plain");
// params specific
info->pathParams["userId"].description = "User Identifier";
}
ENDPOINT("PUT", "/users/{userId}", putUser,
PATH(Int64, userId),
BODY_DTO(dto::UserDto, userDto)) {
BODY_DTO(Object<dto::UserDto>, userDto)) {
userDto->id = userId;
return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
}
Expand All @@ -59,7 +59,7 @@ class UserController : public oatpp::web::server::api::ApiController {
ENDPOINT_INFO(getUserById) {
// general
info->summary = "Get one User by userId";
info->addResponse<dto::UserDto>(Status::CODE_200, "application/json");
info->addResponse<Object<dto::UserDto>>(Status::CODE_200, "application/json");
info->addResponse<String>(Status::CODE_404, "text/plain");
// params specific
info->pathParams["userId"].description = "User Identifier";
Expand All @@ -74,7 +74,7 @@ class UserController : public oatpp::web::server::api::ApiController {

ENDPOINT_INFO(getUsers) {
info->summary = "get all stored users";
info->addResponse<List<dto::UserDto>>(Status::CODE_200, "application/json");
info->addResponse<List<Object<dto::UserDto>>>(Status::CODE_200, "application/json");
}
ENDPOINT("GET", "/users", getUsers) {
return createDtoResponse(Status::CODE_200, m_database->getUsers());
Expand Down
14 changes: 7 additions & 7 deletions user-service/src/user-service/db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace example { namespace user { namespace db {

model::User Database::serializeFromDto(const dto::UserDto::ObjectWrapper &userDto) {
model::User Database::serializeFromDto(const oatpp::Object<dto::UserDto> &userDto) {
model::User user;
if (userDto->id) {
user.id = userDto->id;
Expand All @@ -12,22 +12,22 @@ model::User Database::serializeFromDto(const dto::UserDto::ObjectWrapper &userDt
return user;
}

dto::UserDto::ObjectWrapper Database::deserializeToDto(const model::User &user) {
oatpp::Object<dto::UserDto> Database::deserializeToDto(const model::User &user) {
auto dto = dto::UserDto::createShared();
dto->id = user.id;
dto->name = user.name;
return dto;
}

dto::UserDto::ObjectWrapper Database::createUser(const dto::UserDto::ObjectWrapper &userDto) {
oatpp::Object<dto::UserDto> Database::createUser(const oatpp::Object<dto::UserDto> &userDto) {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto user = serializeFromDto(userDto);
user.id = m_idCounter++;
m_usersById[user.id] = user;
return deserializeToDto(user);
}

dto::UserDto::ObjectWrapper Database::updateUser(const dto::UserDto::ObjectWrapper &userDto) {
oatpp::Object<dto::UserDto> Database::updateUser(const oatpp::Object<dto::UserDto> &userDto) {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto user = serializeFromDto(userDto);
if (user.id < 0) {
Expand All @@ -42,7 +42,7 @@ dto::UserDto::ObjectWrapper Database::updateUser(const dto::UserDto::ObjectWrapp
return deserializeToDto(user);
}

dto::UserDto::ObjectWrapper Database::getUserById(v_int64 id) {
oatpp::Object<dto::UserDto> Database::getUserById(v_int64 id) {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto it = m_usersById.find(id);
if (it == m_usersById.end()) {
Expand All @@ -51,9 +51,9 @@ dto::UserDto::ObjectWrapper Database::getUserById(v_int64 id) {
return deserializeToDto(it->second);
}

oatpp::List<dto::UserDto> Database::getUsers() {
oatpp::List<oatpp::Object<dto::UserDto>> Database::getUsers() {
std::lock_guard<oatpp::concurrency::SpinLock> lock(m_lock);
auto result = oatpp::List<dto::UserDto::ObjectWrapper>::createShared();
auto result = oatpp::List<oatpp::Object<dto::UserDto>>::createShared();
auto it = m_usersById.begin();
while (it != m_usersById.end()) {
result->push_back(deserializeToDto(it->second));
Expand Down
14 changes: 7 additions & 7 deletions user-service/src/user-service/db/Database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Database {
v_int64 m_idCounter; ///< counter to generate userIds
std::unordered_map<v_int64, model::User> m_usersById; ///< Map userId to User
private:
model::User serializeFromDto(const dto::UserDto::ObjectWrapper& userDto);
dto::UserDto::ObjectWrapper deserializeToDto(const model::User& user);
model::User serializeFromDto(const oatpp::Object<dto::UserDto>& userDto);
oatpp::Object<dto::UserDto> deserializeToDto(const model::User& user);
public:

Database()
Expand All @@ -37,11 +37,11 @@ class Database {
createUser(dto::UserDto::createShared(0, "J.D. Salinger"));

}
dto::UserDto::ObjectWrapper createUser(const dto::UserDto::ObjectWrapper& userDto);
dto::UserDto::ObjectWrapper updateUser(const dto::UserDto::ObjectWrapper& userDto);
dto::UserDto::ObjectWrapper getUserById(v_int64 id);
oatpp::List<dto::UserDto> getUsers();

oatpp::Object<dto::UserDto> createUser(const oatpp::Object<dto::UserDto>& userDto);
oatpp::Object<dto::UserDto> updateUser(const oatpp::Object<dto::UserDto>& userDto);
oatpp::Object<dto::UserDto> getUserById(v_int64 id);
oatpp::List<oatpp::Object<dto::UserDto>> getUsers();
bool deleteUser(v_int64 id);

};
Expand Down
4 changes: 2 additions & 2 deletions user-service/src/user-service/dto/UserDto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace example { namespace user { namespace dto {

#include OATPP_CODEGEN_BEGIN(DTO)

class UserDto : public oatpp::Object {
class UserDto : public oatpp::DTO {

DTO_INIT(UserDto, Object)
DTO_INIT(UserDto, DTO)

DTO_FIELD(Int64, id);
DTO_FIELD(String, name, "name");
Expand Down

0 comments on commit a155876

Please sign in to comment.