11# 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?definitionId=9?branchName=master )
22
3- Example project how-to create basic CRUD endpoints and document them with Swagger-UI and OpenApi 3.0.0
3+ A complete example of a " CRUD" service (UserService) built with Oat++.
44
5- See more:
5+ In this example:
6+
7+ - How to create CRUD endpoint.
8+ - How to use [ oatpp ORM] ( https://oatpp.io/docs/components/orm/#high-level-overview ) - SQLite example.
9+ - How to document API with Swagger-UI and OpenApi 3.0.0.
10+
11+ More about Oat++:
612
713- [ Oat++ Website] ( https://oatpp.io/ )
814- [ Oat++ Github Repository] ( https://github.com/oatpp/oatpp )
915- [ Get Started] ( https://oatpp.io/docs/start )
1016
1117## Overview
1218
13- This project is using [ oatpp] ( https://github.com/oatpp/oatpp ) and [ oatpp-swagger] ( https://github.com/oatpp/oatpp-swagger ) modules.
19+ This project is using the following oatpp modules:
20+
21+ - [ oatpp] ( https://github.com/oatpp/oatpp )
22+ - [ oatpp-swagger] ( https://github.com/oatpp/oatpp-swagger )
23+ - [ oatpp-sqlite] ( https://github.com/oatpp/oatpp-sqlite )
1424
1525### Project layout
1626
1727```
1828|- CMakeLists.txt // projects CMakeLists.txt
29+ |- sql/ // SQL migration scripts for SQLite database
1930|- src/
2031| |
21- | |- controller/ // Folder containing UserController where all endpoints are declared
22- | |- db/ // Folder with database mock
32+ | |- controller/ // Folder containing REST Controllers (UserController)
33+ | |- db/ // Folder containing the database client
2334| |- dto/ // DTOs are declared here
24- | |- SwaggerComponent.hpp // Swagger-UI config
35+ | |- service/ // Service business logic classes (UserService)
2536| |- AppComponent.hpp // Service config
37+ | |- DatabaseComponent.hpp // Database config
38+ | |- SwaggerComponent.hpp // Swagger-UI config
2639| |- App.cpp // main() is here
2740|
2841|- test/ // test folder
@@ -37,7 +50,7 @@ This project is using [oatpp](https://github.com/oatpp/oatpp) and [oatpp-swagger
3750
3851** Requires**
3952
40- - ` oatpp ` and ` oatpp-swagger ` modules installed. You may run ` utility/install-oatpp-modules.sh `
53+ - ` oatpp ` , ` oatpp-swagger ` and ` oatpp-sqlite ` modules installed. You may run ` utility/install-oatpp-modules.sh `
4154script to install required oatpp modules.
4255
4356```
@@ -56,78 +69,21 @@ $ docker run -p 8000:8000 -t example-crud
5669
5770---
5871
59- ### Endpoints declaration
72+ ### Endpoints
6073
61- #### Create User
74+ #### HTML
6275
63- ``` c++
64- ENDPOINT_INFO (createUser) {
65- info->summary = "Create new User";
66- info->addConsumes<Object<UserDto >>("application/json");
67- info->addResponse<Object<UserDto >>(Status::CODE_200, "application/json");
68- }
69- ENDPOINT("POST", "demo/api/users", createUser,
70- BODY_DTO(Object<UserDto >, userDto)) {
71- return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
72- }
73- ```
76+ | HTTP Method| URL| Description|
77+ | ---| ---| ---|
78+ | ` GET ` | http://localhost:8000/ | Root page |
79+ | ` GET ` | http://localhost:8000/swagger/ui | Swagger UI page |
7480
75- #### Update User
76-
77- ```c++
78- ENDPOINT_INFO(putUser) {
79- info->summary = "Update User by userId";
80- info->addConsumes<Object<UserDto>>("application/json");
81- info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
82- info->addResponse<String>(Status::CODE_404, "text/plain");
83- }
84- ENDPOINT("PUT", "demo/api/users/{userId}", putUser,
85- PATH(Int32, userId),
86- BODY_DTO(Object<UserDto>, userDto)) {
87- userDto->id = userId;
88- return createDtoResponse(Status::CODE_200, m_database->updateUser(userDto));
89- }
90- ```
81+ #### User Service
9182
92- #### Get one User
93-
94- ``` c++
95- ENDPOINT_INFO (getUserById) {
96- info->summary = "Get one User by userId";
97- info->addResponse<Object<UserDto >>(Status::CODE_200, "application/json");
98- info->addResponse<String >(Status::CODE_404, "text/plain");
99- }
100- ENDPOINT("GET", "demo/api/users/{userId}", getUserById,
101- PATH(Int32, userId)) {
102- auto user = m_database->getUserById(userId);
103- OATPP_ASSERT_HTTP(user, Status::CODE_404, "User not found");
104- return createDtoResponse(Status::CODE_200, user);
105- }
106- ```
107-
108- #### Get list of users
109-
110- ```c++
111- ENDPOINT_INFO(getUsers) {
112- info->summary = "get all stored users";
113- info->addResponse<List<Object<UserDto>>>(Status::CODE_200, "application/json");
114- }
115- ENDPOINT("GET", "demo/api/users", getUsers) {
116- return createDtoResponse(Status::CODE_200, m_database->getUsers());
117- }
118- ```
119-
120- #### Delete User
121- ``` c++
122- ENDPOINT_INFO (deleteUser) {
123- info->summary = "Delete User by userId";
124- info->addResponse<String >(Status::CODE_200, "text/plain");
125- info->addResponse<String >(Status::CODE_404, "text/plain");
126- }
127- ENDPOINT("DELETE", "demo/api/users/{userId}", deleteUser,
128- PATH(Int32, userId)) {
129- bool success = m_database->deleteUser(userId);
130- OATPP_ASSERT_HTTP(success, Status::CODE_417, "User not deleted. Perhaps no such User in the Database");
131- return createResponse(Status::CODE_200, "User successfully deleted");
132- }
133- ```
83+ | HTTP Method| URL| Description|
84+ | ---| ---| ---|
85+ | ` POST ` | http://localhost:8000/users | Create new User |
86+ | ` PUT ` | http://localhost:8000/users/{userId} | Update User by ID |
87+ | ` GET ` | http://localhost:8000/users/{userId} | Get User by ID |
88+ | ` DELETE ` | http://localhost:8000/users/{userId} | Delete User by ID |
89+ | ` GET ` | http://localhost:8000/users/offset/{offset}/limit/{limit} | Get All Users with Paging |
0 commit comments