Skip to content

Commit 9237204

Browse files
authored
Update README.md
1 parent b3bda55 commit 9237204

File tree

1 file changed

+134
-1
lines changed

1 file changed

+134
-1
lines changed

README.md

Lines changed: 134 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,134 @@
1-
# example-crud [![Build Status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.example-crud?branchName=master)](https://dev.azure.com/lganzzzo/lganzzzo/_build/latest?definitionId=9?branchName=master)
1+
# Example-CRUD [![Build Status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.example-crud?branchName=master)](https://dev.azure.com/lganzzzo/lganzzzo/_build/latest?definitionId=9?branchName=master)
2+
3+
Example project how-to create basic CRUD endpoints and document them with Swagger-UI and OpenApi 3.0.0
4+
5+
More about oat++:
6+
- Website: [https://oatpp.io](https://oatpp.io)
7+
- Docs: [https://oatpp.io/docs/start](https://oatpp.io/docs/start)
8+
- Oat++ Repo: [https://github.com/oatpp/oatpp](https://github.com/oatpp/oatpp)
9+
10+
## Overview
11+
This project is using `oatpp` and `oatpp-swagger` modules.
12+
13+
### Project layout
14+
15+
```
16+
17+
- CMakeLists.txt // project loader script. load and build dependencies
18+
- main/ // main project directory
19+
|
20+
|- CMakeLists.txt // projects CMakeLists.txt
21+
|- src/ // source folder
22+
|- test/ // test folder
23+
24+
```
25+
```
26+
- src/
27+
|
28+
|- controller/ // Folder containing UserController where all endpoints are declared
29+
|- db/ // Folder with database mock
30+
|- dto/ // DTOs are declared here
31+
|- SwaggerComponent.hpp // Swagger-UI config
32+
|- AppComponent.hpp // Service config
33+
|- Logger.hpp // Application Logger
34+
|- App.cpp // main() is here
35+
36+
```
37+
38+
---
39+
40+
### Build and Run
41+
42+
#### Using CMake
43+
44+
```
45+
$ mkdir build && cd build
46+
$ cmake ..
47+
$ make run ## Download, build, and install all dependencies. Run project
48+
49+
```
50+
51+
#### In Docker
52+
53+
```
54+
$ docker build --no-cache -t example-crud .
55+
$ docker run -t example-crud
56+
```
57+
58+
---
59+
60+
### Endpoints declaration
61+
62+
#### Create User
63+
64+
```c++
65+
ENDPOINT_INFO(createUser) {
66+
info->summary = "Create new User";
67+
info->addConsumes<UserDto::ObjectWrapper>("application/json");
68+
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
69+
}
70+
ENDPOINT("POST", "demo/api/users", createUser,
71+
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
72+
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
73+
}
74+
```
75+
76+
#### Update User
77+
78+
```c++
79+
ENDPOINT_INFO(putUser) {
80+
info->summary = "Update User by userId";
81+
info->addConsumes<UserDto::ObjectWrapper>("application/json");
82+
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
83+
info->addResponse<String>(Status::CODE_404, "text/plain");
84+
}
85+
ENDPOINT("PUT", "demo/api/users/{userId}", putUser,
86+
PATH(Int32, userId),
87+
BODY_DTO(UserDto::ObjectWrapper, userDto)) {
88+
userDto->id = userId;
89+
return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
90+
}
91+
```
92+
93+
#### Get one User
94+
95+
```c++
96+
ENDPOINT_INFO(getUserById) {
97+
info->summary = "Get one User by userId";
98+
info->addResponse<UserDto::ObjectWrapper>(Status::CODE_200, "application/json");
99+
info->addResponse<String>(Status::CODE_404, "text/plain");
100+
}
101+
ENDPOINT("GET", "demo/api/users/{userId}", getUserById,
102+
PATH(Int32, userId)) {
103+
auto user = m_database->getUserById(userId);
104+
OATPP_ASSERT_HTTP(user, Status::CODE_404, "User not found");
105+
return createDtoResponse(Status::CODE_200, user);
106+
}
107+
```
108+
109+
#### Get list of users
110+
111+
```c++
112+
ENDPOINT_INFO(getUsers) {
113+
info->summary = "get all stored users";
114+
info->addResponse<List<UserDto::ObjectWrapper>::ObjectWrapper>(Status::CODE_200, "application/json");
115+
}
116+
ENDPOINT("GET", "demo/api/users", getUsers) {
117+
return createDtoResponse(Status::CODE_200, m_database->getUsers());
118+
}
119+
```
120+
121+
#### Delete User
122+
```c++
123+
ENDPOINT_INFO(deleteUser) {
124+
info->summary = "Delete User by userId";
125+
info->addResponse<String>(Status::CODE_200, "text/plain");
126+
info->addResponse<String>(Status::CODE_404, "text/plain");
127+
}
128+
ENDPOINT("DELETE", "demo/api/users/{userId}", deleteUser,
129+
PATH(Int32, userId)) {
130+
bool success = m_database->deleteUser(userId);
131+
OATPP_ASSERT_HTTP(success, Status::CODE_417, "User not deleted. Perhaps no such User in the Database");
132+
return createResponse(Status::CODE_200, "User successfully deleted");
133+
}
134+
```

0 commit comments

Comments
 (0)