-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#78 Added support for HTTP PATCH method, updated CRUD example
- Loading branch information
Showing
5 changed files
with
112 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
tmp | ||
*.creator.user* | ||
*.creator.user* | ||
CMakeLists.txt.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,8 @@ enum class HttpMethod | |
POST, | ||
GET, | ||
PUT, | ||
DELETE | ||
DELETE, | ||
PATCH | ||
}; | ||
|
||
} // namespace ngrest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,63 +24,108 @@ | |
#include <string> | ||
#include <vector> | ||
#include <ngrest/common/Service.h> | ||
#include <ngrest/common/Nullable.h> | ||
|
||
namespace ngrest { | ||
namespace examples { | ||
|
||
/** | ||
* @brief User user | ||
*/ | ||
struct User | ||
{ | ||
ngrest::Nullable<int> id; //!< user id | ||
std::string name; //!< user name | ||
std::string email; //!< user email | ||
}; | ||
|
||
|
||
/** | ||
* @brief User user for PATCH request | ||
*/ | ||
struct UserPatch | ||
{ | ||
ngrest::Nullable<std::string> name; //!< user name, don't update if not set | ||
ngrest::Nullable<std::string> email; //!< user email, don't update if not set | ||
}; | ||
|
||
|
||
//! Example on how to use a service in CRUD model | ||
// *location: ngrest/examples/data | ||
// *location: ngrest/examples/users | ||
class Crud: public Service | ||
{ | ||
public: | ||
//! get all identifiers from resource | ||
/*! | ||
example of request: | ||
http://server:port/ngrest/examples/data/ | ||
GET http://server:port/ngrest/examples/users/ | ||
*/ | ||
// *method: GET | ||
// *location: / | ||
std::vector<int> getIds(); | ||
std::vector<User> getAll(); | ||
|
||
//! create new object in resource | ||
/*! | ||
example of request: | ||
http://server:port/ngrest/examples/data/1 | ||
POST http://server:port/ngrest/examples/users/create | ||
-- body ----------------------- | ||
{ | ||
"data": "Object #1" | ||
"name": "John Doe", | ||
"email": "[email protected]" | ||
} | ||
------------------------------- | ||
returns id of created user: | ||
-- response ------------------- | ||
{ | ||
"result": 1 | ||
} | ||
------------------------------- | ||
*/ | ||
// *method: POST | ||
// *location: /{id} | ||
void create(int id, const std::string& data); | ||
// *location: /create | ||
int create(const User& user); | ||
|
||
//! create get object by identifier | ||
/*! | ||
example of request: | ||
http://server:port/ngrest/examples/data/1 | ||
http://server:port/ngrest/examples/users/1 | ||
*/ | ||
// *method: GET | ||
// *location: /{id} | ||
std::string read(int id) const; | ||
User get(int id) const; | ||
|
||
//! update existing object in resource | ||
//! update whole user record | ||
/*! | ||
example of request: | ||
http://server:port/ngrest/examples/data/1 | ||
PUT http://server:port/ngrest/examples/users/1 | ||
-- body ----------------------- | ||
{ | ||
"data": "Updated object #1" | ||
"name": "James Doe", | ||
"email": "[email protected]" | ||
} | ||
*/ | ||
// *method: PUT | ||
// *location: /{id} | ||
void update(int id, const std::string& data); | ||
void update(int id, const User& user); | ||
|
||
//! update particular fields of specified user | ||
/*! | ||
example of request: | ||
PATCH http://server:port/ngrest/examples/users/1 | ||
-- body ----------------------- | ||
{ | ||
"name": "Jimm Doe" | ||
} | ||
*/ | ||
// *method: PATCH | ||
// *location: /{id} | ||
void patch(int id, const UserPatch& user); | ||
|
||
//! delete existing object by identifier | ||
//! delete existing user by identifier | ||
/*! | ||
example of request: | ||
http://server:port/ngrest/examples/data/1 | ||
DELETE http://server:port/ngrest/examples/users/1 | ||
*/ | ||
// *method: DELETE | ||
// *location: /{id} | ||
|