Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stiffstream/restinio-crud-example

It is a simple example of CRUD application based on RESTinio library
https://github.com/stiffstream/restinio-crud-example

Last synced: 6 days ago
JSON representation

It is a simple example of CRUD application based on RESTinio library

Awesome Lists containing this project

README

        

# restinio-crud-example

restinio-crud-example is a simple example of CRUD application based on
[RESTinio](https://github.com/stiffstream/restinio) library.

# How to obtain and build

## Prerequisites

restinio-crud-example requires C++ compiler with C++14 support, vcpkg and CMake.

## Obtaining

Just clone restinio-crud-example from GitHub:

```sh
git clone https://github.com/stiffstream/restinio-crud-example
cd restinio-crud-example
```

## Building

### Obtaining the dependencies

restinio-crud-example uses the following libraries directly: RESTinio, json_dto, SQLiteCpp and optional-lite. Those libraries also use RapidJson, fmtlib and sqlite3. All those dependencies should be obtained via vcpkg:

```sh
vcpkg install restinio json-dto sqlitecpp optional-lite
```

### Building

The build of restinio-crud-example is performed via CMake. Please note the usage of vcpkg's toolchain file:

```sh
cd restinio-crud-example
mkdir cmake_build
cd cmake_build
cmake -DCMAKE_TOOLCHAIN_FILE=/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release .
cmake --build .
```

# Running

Just launch `crud_example` executable. The DB file (`pets.db3`) will be created in the current path.

## A brief reminder of how to try

To create a new pet in the DB prepare a .json file like that:

```js
{"name":"Bunny", "type":"dog", "owner":"John Smith", "picture":"bunny.jpg"}
```

then issue the following command:

```sh
curl -d @new_pet.json -H "Content-Type: application/json" -X POST http://localhost:8080/all/v1/pets
```

To query a particular pet just issue the following command:

```sh
curl http://localhost:8080/all/v1/pets/
```
Where `ID` is a numeric identity of pet to retrive. For example:
```sh
curl http://localhost:8080/all/v1/pets/2
```

To query info about all pets in the database:

```sh
curl http://localhost:8080/all/v1/pets
```

To change the info about a particular pet prepare a .json file (the same way as for a new pet) and issue the following command:
```sh
curl -d @new_pet.json -H "Content-Type: application/json" -X PATCH http://localhost:8080/all/v1/pets/
```
Where `ID` is a numeric identity of pet to update.

To remove a particular pet from the DB issue the following command:
```sh
curl -X DELETE http://localhost:8080/all/v1/pets/
```
Where `ID` is a numeric identity of pet to remove.

To create a bunch of pets at once it is necessary to prepare a .json file like that:
```js
{"pets": [
{"name":"Bunny", "type":"dog", "owner":"John Smith", "picture":"bunny.jpg"}
,{"name":"Baff", "type":"dog", "owner":"John Smith", "picture":"baff.jpg"}
,{"name":"Princess", "type":"cat", "owner":"John Smith", "picture":"princess.jpg"}
]
}
```
Then access the URL `http://localhost:8080/all/v1/pets/batch-upload-form` in a brawser: a form for selection of .json file will be returned. The .json file should be selected and submitted to the server by hitting "Submit" button on that form. New pets would be created and JSON like that will be returned in the response:
```js
{"ids":[7,8,9]}
```