Skip to content

Commit f9fc93f

Browse files
authored
Merge pull request #2 from oatpp/v_1.1.0
V 1.1.0
2 parents 3b2b7df + 4aff328 commit f9fc93f

File tree

10 files changed

+31
-89
lines changed

10 files changed

+31
-89
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ target_include_directories(crud-lib PUBLIC src)
2020

2121
## link libs
2222

23-
find_package(oatpp 1.0.0 REQUIRED)
24-
find_package(oatpp-swagger 1.0.0 REQUIRED)
23+
find_package(oatpp 1.1.0 REQUIRED)
24+
find_package(oatpp-swagger 1.1.0 REQUIRED)
2525

2626
target_link_libraries(crud-lib
2727
PUBLIC oatpp::oatpp

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ $ docker run -p 8000:8000 -t example-crud
6363
```c++
6464
ENDPOINT_INFO(createUser) {
6565
info->summary = "Create new User";
66-
info->addConsumes<UserDto::ObjectWrapper>("application/json");
67-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
66+
info->addConsumes<UserDto>("application/json");
67+
info->addResponse<UserDto>(Status::CODE_200, "application/json");
6868
}
6969
ENDPOINT("POST", "demo/api/users", createUser,
70-
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
70+
BODY_DTO(UserDto, userDto)) {
7171
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
7272
}
7373
```
@@ -77,13 +77,13 @@ ENDPOINT("POST", "demo/api/users", createUser,
7777
```c++
7878
ENDPOINT_INFO(putUser) {
7979
info->summary = "Update User by userId";
80-
info->addConsumes<UserDto::ObjectWrapper>("application/json");
81-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
80+
info->addConsumes<UserDto>("application/json");
81+
info->addResponse<UserDto>(Status::CODE_200, "application/json");
8282
info->addResponse<String>(Status::CODE_404, "text/plain");
8383
}
8484
ENDPOINT("PUT", "demo/api/users/{userId}", putUser,
8585
PATH(Int32, userId),
86-
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
86+
BODY_DTO(UserDto, userDto)) {
8787
userDto->id = userId;
8888
return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
8989
}
@@ -94,7 +94,7 @@ ENDPOINT("PUT", "demo/api/users/{userId}", putUser,
9494
```c++
9595
ENDPOINT_INFO(getUserById) {
9696
info->summary = "Get one User by userId";
97-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
97+
info->addResponse<UserDto>(Status::CODE_200, "application/json");
9898
info->addResponse<String>(Status::CODE_404, "text/plain");
9999
}
100100
ENDPOINT("GET", "demo/api/users/{userId}", getUserById,
@@ -110,7 +110,7 @@ ENDPOINT("GET", "demo/api/users/{userId}", getUserById,
110110
```c++
111111
ENDPOINT_INFO(getUsers) {
112112
info->summary = "get all stored users";
113-
info->addResponse<List<UserDto::ObjectWrapper>::ObjectWrapper>(Status::CODE_200, "application/json");
113+
info->addResponse<List<UserDto>>(Status::CODE_200, "application/json");
114114
}
115115
ENDPOINT("GET", "demo/api/users", getUsers) {
116116
return createDtoResponse(Status::CODE_200, m_database->getUsers());

src/App.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// main.cpp
3-
// web-starter-project
4-
//
5-
// Created by Leonid on 2/12/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
81

92
#include "./controller/UserController.hpp"
103
#include "./AppComponent.hpp"

src/AppComponent.hpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// AppComponent.hpp
3-
// oatpp-web-starter
4-
//
5-
// Created by Leonid on 3/2/18.
6-
// Copyright © 2018 lganzzzo. All rights reserved.
7-
//
81

92
#ifndef AppComponent_hpp
103
#define AppComponent_hpp
@@ -60,10 +53,8 @@ class AppComponent {
6053
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
6154
*/
6255
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
63-
auto serializerConfig = oatpp::parser::json::mapping::Serializer::Config::createShared();
64-
auto deserializerConfig = oatpp::parser::json::mapping::Deserializer::Config::createShared();
65-
deserializerConfig->allowUnknownFields = false;
66-
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared(serializerConfig, deserializerConfig);
56+
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
57+
objectMapper->getDeserializer()->getConfig()->allowUnknownFields = false;
6758
return objectMapper;
6859
}());
6960

src/SwaggerComponent.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// SwaggerComponent.hpp
3-
// crud
4-
//
5-
// Created by Leonid on 7/31/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
81

92
#ifndef SwaggerComponent_hpp
103
#define SwaggerComponent_hpp

src/controller/UserController.hpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// UserController.hpp
3-
// web-starter-project
4-
//
5-
// Created by Leonid on 2/12/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
81

92
#ifndef UserController_hpp
103
#define UserController_hpp
@@ -54,7 +47,7 @@ class UserController : public oatpp::web::server::api::ApiController {
5447

5548
ENDPOINT_INFO(root) {
5649
info->summary = "Index.html page";
57-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "text/html");
50+
info->addResponse<String>(Status::CODE_200, "text/html");
5851
}
5952
ENDPOINT("GET", "/", root) {
6053
const char* html =
@@ -74,27 +67,27 @@ class UserController : public oatpp::web::server::api::ApiController {
7467

7568
ENDPOINT_INFO(createUser) {
7669
info->summary = "Create new User";
77-
info->addConsumes<UserDto::ObjectWrapper>("application/json");
78-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
70+
info->addConsumes<UserDto>("application/json");
71+
info->addResponse<UserDto>(Status::CODE_200, "application/json");
7972
}
8073
ENDPOINT("POST", "demo/api/users", createUser,
81-
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
74+
BODY_DTO(UserDto, userDto)) {
8275
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
8376
}
8477

8578

8679
ENDPOINT_INFO(putUser) {
8780
// general
8881
info->summary = "Update User by userId";
89-
info->addConsumes<UserDto::ObjectWrapper>("application/json");
90-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
82+
info->addConsumes<UserDto>("application/json");
83+
info->addResponse<UserDto>(Status::CODE_200, "application/json");
9184
info->addResponse<String>(Status::CODE_404, "text/plain");
9285
// params specific
9386
info->pathParams["userId"].description = "User Identifier";
9487
}
9588
ENDPOINT("PUT", "demo/api/users/{userId}", putUser,
9689
PATH(Int32, userId),
97-
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
90+
BODY_DTO(UserDto, userDto)) {
9891
userDto->id = userId;
9992
return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
10093
}
@@ -103,7 +96,7 @@ class UserController : public oatpp::web::server::api::ApiController {
10396
ENDPOINT_INFO(getUserById) {
10497
// general
10598
info->summary = "Get one User by userId";
106-
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
99+
info->addResponse<UserDto>(Status::CODE_200, "application/json");
107100
info->addResponse<String>(Status::CODE_404, "text/plain");
108101
// params specific
109102
info->pathParams["userId"].description = "User Identifier";
@@ -118,7 +111,7 @@ class UserController : public oatpp::web::server::api::ApiController {
118111

119112
ENDPOINT_INFO(getUsers) {
120113
info->summary = "get all stored users";
121-
info->addResponse<List<UserDto::ObjectWrapper>::ObjectWrapper>(Status::CODE_200, "application/json");
114+
info->addResponse<List<UserDto>>(Status::CODE_200, "application/json");
122115
}
123116
ENDPOINT("GET", "demo/api/users", getUsers) {
124117
return createDtoResponse(Status::CODE_200, m_database->getUsers());

src/db/Database.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
1-
//
2-
// Database.cpp
3-
// crud
4-
//
5-
// Created by Leonid on 3/13/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
81

92
#include "Database.hpp"
103

114
User Database::serializeFromDto(const UserDto::ObjectWrapper& userDto){
125
User user;
136
if(userDto->id){
14-
user.id = userDto->id->getValue();
7+
user.id = *userDto->id;
158
}
169
user.firstName = userDto->firstName;
1710
user.lastName = userDto->lastName;
18-
userDto->friends->forEach([&user](const oatpp::String& friendId){
19-
user.friends.push_back(friendId);
20-
});
11+
12+
for(auto& id : *userDto->friends) {
13+
user.friends.push_back(id);
14+
}
2115
return user;
2216
}
2317

@@ -28,7 +22,7 @@ UserDto::ObjectWrapper Database::deserializeToDto(const User& user){
2822
dto->lastName = user.lastName;
2923
auto it = user.friends.begin();
3024
while (it != user.friends.end()) {
31-
dto->friends->pushBack(*it++);
25+
dto->friends->push_back(*it++);
3226
}
3327
return dto;
3428
}
@@ -70,7 +64,7 @@ oatpp::data::mapping::type::List<UserDto::ObjectWrapper>::ObjectWrapper Database
7064
auto result = oatpp::data::mapping::type::List<UserDto::ObjectWrapper>::createShared();
7165
auto it = m_usersById.begin();
7266
while (it != m_usersById.end()) {
73-
result->pushBack(deserializeToDto(it->second));
67+
result->push_back(deserializeToDto(it->second));
7468
it++;
7569
}
7670
return result;

src/db/Database.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// Database.hpp
3-
// crud
4-
//
5-
// Created by Leonid on 3/13/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
81

92
#ifndef Database_hpp
103
#define Database_hpp

src/db/model/User.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// User.hpp
3-
// crud
4-
//
5-
// Created by Leonid on 3/13/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
81

92
#ifndef db_User_hpp
103
#define db_User_hpp

src/dto/UserDto.hpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
//
2-
// UserDto.hpp
3-
// crud
4-
//
5-
// Created by Leonid on 3/13/18.
6-
// Copyright © 2018 oatpp. All rights reserved.
7-
//
8-
91
#ifndef UserDto_hpp
102
#define UserDto_hpp
113

12-
#include "oatpp/core/data/mapping/type/Object.hpp"
134
#include "oatpp/core/macro/codegen.hpp"
5+
#include "oatpp/core/Types.hpp"
146

157
#include OATPP_CODEGEN_BEGIN(DTO)
168

179
/**
1810
* Data Transfer Object. Object containing fields only.
1911
* Used in API for serialization/deserialization and validation
2012
*/
21-
class UserDto : public oatpp::data::mapping::type::Object {
13+
class UserDto : public oatpp::Object {
2214

2315
DTO_INIT(UserDto, Object)
2416

2517
DTO_FIELD(Int32, id);
2618
DTO_FIELD(String, firstName, "first-name");
2719
DTO_FIELD(String, lastName, "last-name");
28-
DTO_FIELD(List<String>::ObjectWrapper, friends) = List<String>::createShared();
20+
DTO_FIELD(Vector<String>, friends) = Vector<String>({});
2921

3022
};
3123

0 commit comments

Comments
 (0)