Skip to content

Commit 922a4c8

Browse files
committed
updated swagger and added exception handling in couchbase service provider
Update .gitignore to include composer.lock and package-lock.json files. Fixed Hotel Test and formatted readme liniting
1 parent ab6d8ef commit 922a4c8

File tree

5 files changed

+228
-175
lines changed

5 files changed

+228
-175
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,5 @@ yarn-error.log
1818
/.fleet
1919
/.idea
2020
/.vscode
21+
composer.lock
22+
package-lock.json

README.md

Lines changed: 91 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
# Quickstart in Couchbase with PHP using Laravel
22

3-
#### REST API using Couchbase Capella in PHP using Laravel
3+
## REST API using Couchbase Capella in PHP using Laravel
44

55
Often, the first step developers take after creating their database is to create a REST API that can perform Create, Read, Update, and Delete (CRUD) operations for that database. This repo is designed to teach you and give you a starter project (in PHP using Laravel) to generate such a REST API. After you have installed the travel-sample bucket in your database, you can run this application which is a REST API with Swagger documentation so that you can learn:
66

7-
1. How to create, read, update, and delete documents using Key-Value[ operations](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html) (KV operations). KV operations are unique to Couchbase and provide super fast (think microseconds) queries.
7+
1. How to create, read, update, and delete documents using Key-Value[operations](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html) (KV operations). KV operations are unique to Couchbase and provide super fast (think microseconds) queries.
88
2. How to write simple parametrized [N1QL queries](https://docs.couchbase.com/php-sdk/current/howtos/n1ql-queries-with-sdk.html) using the built-in travel-sample bucket.
99

1010
Full documentation for the tutorial can be found on the [Couchbase Developer Portal](https://developer.couchbase.com/tutorial-quickstart-laravel-php/).
1111

12-
1312
## Prerequisites
1413

1514
To run this prebuilt project, you will need:
1615

1716
- [Couchbase Capella](https://www.couchbase.com/products/capella/) cluster with [travel-sample](https://docs.couchbase.com/php-sdk/current/ref/travel-app-data-model.html) bucket loaded.
18-
- To run this tutorial using a self-managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
17+
- To run this tutorial using a self-managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
1918
- [PHP](https://www.php.net/downloads.php) 8.2 or higher installed
20-
- Ensure that the PHP version is [compatible](https://docs.couchbase.com/php-sdk/current/project-docs/compatibility.html#php-version-compat) with the Couchbase SDK.
19+
- Ensure that the PHP version is [compatible](https://docs.couchbase.com/php-sdk/current/project-docs/compatibility.html#php-version-compat) with the Couchbase SDK.
2120
- Loading Travel Sample Bucket
22-
If travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster:
23-
- [Load travel-sample bucket in Couchbase Capella](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html#import-sample-data)
21+
If travel-sample is not loaded in your Capella cluster, you can load it by following the instructions for your Capella Cluster:
22+
- [Load travel-sample bucket in Couchbase Capella](https://docs.couchbase.com/cloud/clusters/data-service/import-data-documents.html#import-sample-data)
2423

2524
## App Setup
2625

@@ -40,7 +39,36 @@ The dependencies for the application are specified in the `composer.json` file i
4039
composer install
4140
```
4241

42+
### Initial Configuration
43+
44+
After installing dependencies, you need to set up the basic Laravel configuration:
45+
46+
1. Create the environment file:
47+
48+
```sh
49+
cp .env.example .env
50+
```
51+
52+
2. Generate application key:
53+
54+
```sh
55+
php artisan key:generate
56+
```
57+
58+
3. Create the database and run the migrations:
59+
60+
```sh
61+
mkdir -p database && touch database/database.sqlite && php artisan migrate
62+
```
63+
64+
4. Generate Swagger documentation:
65+
66+
```sh
67+
php artisan l5-swagger:generate
68+
```
69+
4370
### Setup Database Configuration
71+
4472
To learn more about connecting to your Capella cluster, please follow the [instructions](https://docs.couchbase.com/cloud/get-started/connect.html).
4573

4674
Specifically, you need to do the following:
@@ -96,7 +124,7 @@ Once the application starts, you can see the details of the application on the l
96124

97125
![Application Startup](./public/php-quickstart-app-startup.png)
98126

99-
The application will run on port 8000 of your local machine (http://localhost:8000/api/documentation). You will find the Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API endpoints and how they can be invoked. More details on the Swagger documentation can be found in the [appendix](#swagger-documentation).
127+
The application will run on port 8000 of your local machine ([http://localhost:8000/api/documentation](http://localhost:8000/api/documentation)). You will find the Swagger documentation of the API if you go to the URL in your browser. Swagger documentation is used in this demo to showcase the different API endpoints and how they can be invoked. More details on the Swagger documentation can be found in the [appendix](#swagger-documentation).
100128

101129
![Swagger Documentation](./public/php-quickstart-swagger.png)
102130

@@ -123,152 +151,72 @@ If you would like to add another entity to the APIs, follow these steps:
123151
- **Create the new entity (collection) in the Couchbase bucket:** You can create the collection using the [SDK](https://docs.couchbase.com/php-sdk/current/howtos/provisioning-cluster-resources.html#collection-management) or via the [Couchbase Server interface](https://docs.couchbase.com/cloud/n1ql/n1ql-language-reference/createcollection.html).
124152

125153
- **Define the model:** Create a new model in the `app/Models` directory, similar to the existing `Airline` model. For example, you can create a file `Hotel.php`:
126-
```php
127-
namespace App\Models;
128-
129-
use Illuminate\Database\Eloquent\Model;
130154

131-
class Hotel extends Model
132-
{
133-
protected $bucket;
134-
135-
protected $fillable = [
136-
'name',
137-
'address',
138-
'city',
139-
'country',
140-
'stars'
141-
];
142-
143-
public function __construct(array $attributes = [])
144-
{
145-
parent::__construct($attributes);
146-
$this->bucket = app('couchbase.bucket');
147-
}
148-
149-
// Add methods for querying, saving, and deleting Hotel data
150-
}
151-
```
155+
```php
156+
namespace App\Models;
157+
158+
use Illuminate\Database\Eloquent\Model;
159+
160+
class Hotel extends Model
161+
{
162+
protected $bucket;
163+
164+
protected $fillable = [
165+
'name',
166+
'address',
167+
'city',
168+
'country',
169+
'stars'
170+
];
171+
172+
public function __construct(array $attributes = [])
173+
{
174+
parent::__construct($attributes);
175+
$this->bucket = app('couchbase.bucket');
176+
}
177+
178+
// Add methods for querying, saving, and deleting Hotel data
179+
}
180+
```
152181

153182
- **Define the controller:** Create a new controller in the `app/Http/Controllers` directory, similar to the existing `AirlineController`. For example, you can create a file `HotelController.php`:
154-
```php
155-
namespace App\Http\Controllers;
156-
157-
use Illuminate\Http\Request;
158-
use App\Models\Hotel;
159-
160-
class HotelController extends Controller
161-
{
162-
// Add methods for handling HTTP requests for the Hotel entity
163-
}
164-
```
165-
166-
- **Define the routes:** In the `routes/api.php` file, define the routes for the new entity similar to the existing routes for airlines:
167-
```php
168-
Route::prefix('v1/hotels')->group(function () {
169-
Route::get('list', 'HotelController@index');
170-
Route::get('{id}', 'HotelController@show');
171-
Route::post('{id}', 'HotelController@store');
172-
Route::put('{id}', 'HotelController@update');
173-
Route::delete('{id}', 'HotelController@destroy');
174-
});
175-
```
176-
177-
- **Integration tests:** Create a new test class in the `tests/Feature` directory, similar to the existing tests. For example, you can create a file `HotelIntegrationTest.php`:
178-
```php
179-
namespace Tests\Feature;
180-
181-
use Tests\TestCase;
182183

183-
class HotelIntegrationTest extends TestCase
184-
{
185-
// Add test methods for the Hotel endpoints
186-
}
187-
```
188-
189-
### Running Self Managed Couchbase Cluster
190-
191-
If you are running this quickstart with a self-managed Couchbase cluster, you need to [load](https://docs.couchbase.com/server/current/manage/manage-settings/install-sample-buckets.html) the travel-sample data bucket in your cluster and generate the credentials for the bucket.
184+
```php
185+
namespace App\Http\Controllers;
192186

193-
You need to update the connection string and the credentials in the `config/couchbase.php` file:
187+
use Illuminate\Http\Request;
188+
use App\Models\Hotel;
194189

195-
```env
196-
DB_CONN_STR_=couchbase://<your-couchbase-server>
197-
DB_USERNAME=<your-username>
198-
DB_PASSWORD=<your-password>
199-
DB_BUCKET=travel-sample
190+
class HotelController extends Controller
191+
{
192+
// Add methods for handling HTTP requests for the Hotel entity
193+
}
200194
```
201195

202-
Replace `<your-couchbase-server>`, `<your-username>`, and `<your-password>` with your actual Couchbase server details and credentials.
203-
> **NOTE:** Couchbase must be installed and running prior to running the Spring Boot app.### Extending API by Adding New Entity
204-
205-
If you would like to add another entity to the APIs, follow these steps:
206-
207-
- **Create the new entity (collection) in the Couchbase bucket:** You can create the collection using the [SDK](https://docs.couchbase.com/php-sdk/current/howtos/provisioning-cluster-resources.html#collection-management) or via the [Couchbase Server interface](https://docs.couchbase.com/cloud/n1ql/n1ql-language-reference/createcollection.html).
208-
209-
- **Define the model:** Create a new model in the `app/Models` directory, similar to the existing `Airline` model. For example, you can create a file `Hotel.php`:
210-
```php
211-
namespace App\Models;
212-
213-
use Illuminate\Database\Eloquent\Model;
214-
215-
class Hotel extends Model
216-
{
217-
protected $bucket;
218-
219-
protected $fillable = [
220-
'name',
221-
'address',
222-
'city',
223-
'country',
224-
'stars'
225-
];
226-
227-
public function __construct(array $attributes = [])
228-
{
229-
parent::__construct($attributes);
230-
$this->bucket = app('couchbase.bucket');
231-
}
232-
233-
// Add methods for querying, saving, and deleting Hotel data
234-
}
235-
```
236-
237-
- **Define the controller:** Create a new controller in the `app/Http/Controllers` directory, similar to the existing `AirlineController`. For example, you can create a file `HotelController.php`:
238-
```php
239-
namespace App\Http\Controllers;
240-
241-
use Illuminate\Http\Request;
242-
use App\Models\Hotel;
243-
244-
class HotelController extends Controller
245-
{
246-
// Add methods for handling HTTP requests for the Hotel entity
247-
}
248-
```
249-
250196
- **Define the routes:** In the `routes/api.php` file, define the routes for the new entity similar to the existing routes for airlines:
251-
```php
252-
Route::prefix('v1/hotels')->group(function () {
253-
Route::get('list', 'HotelController@index');
254-
Route::get('{id}', 'HotelController@show');
255-
Route::post('{id}', 'HotelController@store');
256-
Route::put('{id}', 'HotelController@update');
257-
Route::delete('{id}', 'HotelController@destroy');
258-
});
259-
```
197+
198+
```php
199+
Route::prefix('v1/hotels')->group(function () {
200+
Route::get('list', 'HotelController@index');
201+
Route::get('{id}', 'HotelController@show');
202+
Route::post('{id}', 'HotelController@store');
203+
Route::put('{id}', 'HotelController@update');
204+
Route::delete('{id}', 'HotelController@destroy');
205+
});
206+
```
260207

261208
- **Integration tests:** Create a new test class in the `tests/Feature` directory, similar to the existing tests. For example, you can create a file `HotelIntegrationTest.php`:
262-
```php
263-
namespace Tests\Feature;
264209

265-
use Tests\TestCase;
210+
```php
211+
namespace Tests\Feature;
212+
213+
use Tests\TestCase;
266214

267-
class HotelIntegrationTest extends TestCase
268-
{
269-
// Add test methods for the Hotel endpoints
270-
}
271-
```
215+
class HotelIntegrationTest extends TestCase
216+
{
217+
// Add test methods for the Hotel endpoints
218+
}
219+
```
272220

273221
### Running Self Managed Couchbase Cluster
274222

@@ -277,7 +225,6 @@ If you are running this quickstart with a self-managed Couchbase cluster, you ne
277225
You need to update the connection string and the credentials in the `config/couchbase.php` file:
278226

279227
```php
280-
281228
<?php
282229
return [
283230
'host' => env('DB_CONN_STR', 'couchbase://<your-couchbase-server>'),
@@ -288,7 +235,8 @@ return [
288235
```
289236

290237
Replace `<your-couchbase-server>`, `<your-username>`, and `<your-password>` with your actual Couchbase server details and credentials.
291-
> **NOTE:** Couchbase must be installed and running prior to running the Spring Boot app.
238+
239+
> **NOTE:** Couchbase must be installed and running prior to running the Laravel application.
292240
293241
### Swagger Documentation
294242

@@ -307,4 +255,3 @@ You can try out an API by clicking on the "Try it out" button next to the endpoi
307255
#### Models
308256

309257
Swagger documents the structure of request and response bodies using models. These models define the expected data structure using JSON schema and are extremely helpful in understanding what data to send and expect.
310-

0 commit comments

Comments
 (0)