# Pet Store CRUD

We will use the [Truebit CLI](https://devs.truebit.io/developing-truebit-tasks/truebit-cli-reference) to [compile](https://devs.truebit.io/how-to-create-function-tasks/function-task-examples/reverse-alphabet#step-2-build-the-source-code) and [test](https://devs.truebit.io/how-to-create-function-tasks/function-task-examples/reverse-alphabet#step-3-try-the-code) our source code. Once finalized, we will [deploy](#step-4-deploy-the-task) it to the Coordination Hub so that any user with the `namespace` and `taskname` can access or execute it.

### Step 1. Define The API Manifest

The manifest is already defined within the folder *examples/api-tasks/petstore-crud/manifest.json*.

### Step 2. Test The API Task

Execute the [`start-api`](https://devs.truebit.io/truebit-cli-reference#start-api) command to test the manifest.

#### Add a New Record Using POST Verb

{% code overflow="wrap" %}

```bash
truebit start-api truebit-nextgen-examples/api-tasks/petstore-crud/manifest.json truebit-nextgen-examples/api-tasks/petstore-crud/post.add.input.json 
```

{% endcode %}

#### Output

```bash
Executing the API Task
API Task executed with status - OK 

Input request :
{
  "path": "/store/order",
  "method": "POST",
  "body": {
    "id": 2,
    "petId": 198772,
    "quantity": 7,
    "shipDate": "2077-08-24T14:15:22Z",
    "status": "approved",
    "complete": true
  },
  "headers": {}
}

Base URL = https://petstore.swagger.io/v2 
OpenAPI manifest check - OK 

API Task response
{
  "id": 2,
  "petId": 198772,
  "quantity": 7,
  "shipDate": "2077-08-24T14:15:22.000+0000",
  "status": "approved",
  "complete": true
}
```

#### Retrieve The Record Using GET Verb

{% code overflow="wrap" %}

```bash
truebit start-api truebit-nextgen-examples/api-tasks/petstore-crud/manifest.json truebit-nextgen-examples/api-tasks/petstore-crud/get.input.json 
```

{% endcode %}

#### Output

{% code overflow="wrap" %}

```bash
Executing the API Task
API Task executed with status - OK 

Input request :
{
  "path": "/store/order/{orderId}",
  "method": "GET",
  "params": {
    "orderId": 2
  },
  "headers": {}
}

Base URL = https://petstore.swagger.io/v2 
OpenAPI manifest check - OK 

API Task response
{
  "id": 2,
  "petId": 198772,
  "quantity": 7,
  "shipDate": "2077-08-24T14:15:22.000+0000",
  "status": "approved",
  "complete": true
}
```

{% endcode %}

#### Update The Record Using The POST Verb

{% code overflow="wrap" %}

```bash
truebit start-api truebit-nextgen-examples/api-tasks/petstore-crud/manifest.json truebit-nextgen-examples/api-tasks/petstore-crud/post.update.input.json
```

{% endcode %}

#### Output

{% code overflow="wrap" %}

```bash
Executing the API Task
API Task executed with status - OK 

Input request :
{
  "path": "/store/order",
  "method": "POST",
  "body": {
    "id": 2,
    "petId": 198772,
    "quantity": 18,
    "shipDate": "2077-08-24T14:15:22Z",
    "status": "approved",
    "complete": true
  },
  "headers": {}
}

Base URL = https://petstore.swagger.io/v2 
OpenAPI manifest check - OK 

API Task response
{
  "id": 2,
  "petId": 198772,
  "quantity": 18,
  "shipDate": "2077-08-24T14:15:22.000+0000",
  "status": "approved",
  "complete": true
}
```

{% endcode %}

#### Delete The Record Using The DELETE Verb

{% code overflow="wrap" %}

```bash
truebit start-api truebit-nextgen-examples/api-tasks/petstore-crud/manifest.json truebit-nextgen-examples/api-tasks/petstore-crud/delete.input.json
```

{% endcode %}

#### Output

{% code overflow="wrap" %}

```bash
Executing the API Task
API Task executed with status - OK 

Input request :
{
  "path": "/store/order/{orderId}",
  "method": "DELETE",
  "params": {
    "orderId": 2
  },
  "headers": {}
}

Base URL = https://petstore.swagger.io/v2 
OpenAPI manifest check - OK 

API Task response
{
  "code": 200,
  "type": "unknown",
  "message": "2"
}
```

{% endcode %}

### Step 3. Create The API Task

Execute the [`create-api`](https://devs.truebit.io/truebit-cli-reference#create-api) command against the Truebit Verify Node to execute the endpoint.

{% code overflow="wrap" %}

```bash
truebit create-api truebit-nextgen-examples/api-tasks/petstore-crud/manifest.json
```

{% endcode %}

#### Output

{% code overflow="wrap" %}

```
Creating API task...
OpenAPI manifest is valid!
Your API task is ready!
Use the following TaskId for execution or deployment: <taskId>
```

{% endcode %}

### Step 4. Deploy The API Task

Last but not least, please go ahead and execute the [deploy](https://devs.truebit.io/truebit-cli-reference#deploy) command to deploy the API task to the coordination hub, so that anyone with the `taskId` can use it.

{% code overflow="wrap" %}

```bash
truebit deploy <namespace> <taskname> --taskId <taskId>
```

{% endcode %}

* \[taskId]: Add the `taskId` generated in step 3

#### Output

{% code overflow="wrap" lineNumbers="true" %}

```bash
Deploying the API task
The function task has been deployed successfully.
```

{% endcode %}
