You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -35,7 +35,8 @@ To run this prebuilt project, you will need:
35
35
36
36
-[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.
37
37
- To run this tutorial using a self managed Couchbase cluster, please refer to the [appendix](#running-self-managed-couchbase-cluster).
38
-
- PHP installed
38
+
-[PHP](https://www.php.net/downloads.php) 8.2 or higher installed
39
+
- 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.
39
40
- Code Editor installed (Visual Studio Code, PhpStorm, or Sublime Text)
40
41
- Composer command line
41
42
@@ -45,6 +46,12 @@ To run this prebuilt project, you will need:
> Note: Composer automatically installs the required dependencies when building the project.
56
63
57
-
### Database Server Configuration
64
+
### Initial Configuration
58
65
59
-
The `CouchbaseServiceProvider` class is a Laravel service provider class responsible for setting up the connection to a Couchbase database in a Laravel application. It defines the following configurations:
66
+
After installing dependencies, you need to set up the basic Laravel configuration:
60
67
61
-
-`couchbase.cluster`: This configuration specifies the Couchbase cluster connection using the provided hostname, username, and password.
62
-
-`couchbase.bucket`: This configuration specifies the Couchbase bucket connection using the provided bucket name.
63
-
-`couchbase.airlineCollection`: This configuration specifies the airline collection in the Couchbase bucket.
64
-
-`couchbase.airportCollection`: This configuration specifies the airport collection in the Couchbase bucket.
65
-
-`couchbase.routeCollection`: This configuration specifies the route collection in the Couchbase bucket.
66
-
-`couchbase.hotelCollection`: This configuration specifies the hotel collection in the Couchbase bucket.
68
+
1. Create the environment file:
69
+
70
+
```sh
71
+
cp .env.example .env
72
+
```
73
+
74
+
2. Generate application key:
75
+
76
+
```sh
77
+
php artisan key:generate
78
+
```
79
+
80
+
### Couchbase Service Provider
81
+
82
+
The `CouchbaseServiceProvider` class is a Laravel service provider class responsible for setting up the connection to a Couchbase database in a Laravel application.
67
83
68
84
69
85
### Application Environment
70
-
You need to configure the connection details to your Couchbase Server in the `config/couchbase.php` file located in the root directory of the project.
71
86
72
-
In the `config/couchbase.php` file, update the following variables:
73
-
-`DB_CONN_STR`: Replace with the connection string of your Couchbase cluster.
74
-
-`DB_USERNAME`: Replace with the username of a Couchbase user with access to the bucket.
75
-
-`DB_PASSWORD`: Replace with the password of the Couchbase user.
87
+
All configuration for communication with the database is read from environment variables. As a security best practice, you should set these values in your `.env` file:
76
88
77
-
Make sure to save the changes after updating the variables in the `config/couchbase.php` file.
89
+
```env
90
+
DB_CONN_STR=<connection_string>
91
+
DB_USERNAME=<username>
92
+
DB_PASSWORD=<password>
93
+
DB_BUCKET=travel-sample
94
+
```
95
+
96
+
Replace the placeholder values with your actual Couchbase cluster details.
78
97
79
98
The connection string should be in the following format:
Note: The `config/couchbase.php` file has the connection information to connect to your Capella cluster. You can also pass the connection information as environment variables to the Docker container.
130
-
If you choose not to pass the environment variables, you can update the `config/couchbase.php` file in the root directory.
148
+
Note: Environment variables passed to the Docker container will override the default values in the configuration.
131
149
132
150
Once the application is running, you can access it in your browser at [http://localhost:8000](http://localhost:8000). Swagger UI is available at [http://localhost:8000/api/documentation](http://localhost:8000/api/documentation).
133
151
@@ -162,136 +180,83 @@ Our airline document will have a structure similar to the following example:
162
180
}
163
181
```
164
182
165
-
## Let's Review the Code
166
-
167
-
To begin, open the repository in an IDE of your choice to learn about how to create, read, update, and delete documents in your Couchbase Server.
168
-
169
-
### Code Organization
170
-
171
-
-`tests/Feature`: Contains integration tests.
172
-
-`app/Http/Controllers`: Contains the controller classes.
173
-
-`app/Models`: Contains the model classes.
174
-
175
-
## API Endpoints Documentation
176
-
177
-
### 1. Get List of Airlines
178
-
179
-
**Endpoint:**
180
-
181
-
`GET /api/v1/airlines/list`
182
-
183
-
**Description:**
184
-
185
-
This endpoint retrieves a list of airlines from the database. Clients can optionally filter the results by country and paginate through the results using the `offset` and `limit` query parameters. This helps in managing large datasets by breaking them into smaller, manageable chunks.
186
-
187
-
**Steps:**
188
-
189
-
1. The client sends a GET request to the endpoint `/api/v1/airlines/list`.
190
-
2. Optionally, the client includes query parameters:
191
-
-`offset` specifies the number of items to skip before starting to collect the result set.
192
-
-`limit` specifies the number of items to return.
193
-
-`country` filters the results to only include airlines from a specific country.
194
-
3. The server processes the request and queries the database for airlines that match the given criteria.
195
-
4. The server returns a list of airlines. If no airlines match the criteria, an appropriate error message is returned.
196
-
197
-
---
198
-
199
-
### 2. Get Airline by ID
200
-
201
-
**Endpoint:**
183
+
## Code Structure
202
184
203
-
`GET /api/v1/airlines/{id}`
185
+
### Project Structure
204
186
205
-
**Description:**
206
-
207
-
This endpoint retrieves detailed information about a specific airline by its ID. This is useful for getting the full profile of an airline, including its callsign, country, IATA code, ICAO code, and name.
208
-
209
-
**Steps:**
210
-
211
-
1. The client sends a GET request to the endpoint `/api/v1/airlines/{id}`.
212
-
2. The client includes the airline ID in the path of the request.
213
-
3. The server searches the database for an airline with the specified ID.
214
-
4. If the airline is found, the server returns the airline's details. If the airline is not found, an error message is returned.
215
-
216
-
---
217
-
218
-
### 3. Create a New Airline
219
-
220
-
**Endpoint:**
221
-
222
-
`POST /api/v1/airlines/{id}`
223
-
224
-
**Description:**
225
-
226
-
This endpoint allows the creation of a new airline with the specified ID. The client must provide all required airline details in the request body, such as callsign, country, IATA code, ICAO code, and name.
227
-
228
-
**Steps:**
229
-
230
-
1. The client sends a POST request to the endpoint `/api/v1/airlines/{id}`.
231
-
2. The client includes the airline ID in the path and provides the airline details in the request body.
232
-
3. The server validates the provided data to ensure all required fields are present and correctly formatted.
233
-
4. If validation passes, the server saves the new airline in the database. If validation fails, an error message is returned.
234
-
235
-
---
236
-
237
-
### 4. Update an Existing Airline
238
-
239
-
**Endpoint:**
240
-
241
-
`PUT /api/v1/airlines/{id}`
242
-
243
-
**Description:**
187
+
```
188
+
.
189
+
├── app/
190
+
│ ├── Http/
191
+
│ │ └── Controllers/
192
+
│ │ ├── AirlineController.php
193
+
│ │ ├── AirportController.php
194
+
│ │ ├── RouteController.php
195
+
│ │ └── HotelController.php
196
+
│ ├── Models/
197
+
│ │ ├── Airline.php
198
+
│ │ ├── Airport.php
199
+
│ │ ├── Route.php
200
+
│ │ └── Hotel.php
201
+
│ └── Providers/
202
+
│ └── CouchbaseServiceProvider.php
203
+
├── config/
204
+
│ └── couchbase.php
205
+
├── routes/
206
+
│ └── api.php
207
+
└── tests/
208
+
└── Feature/
209
+
├── AirlineTest.php
210
+
├── AirportTest.php
211
+
├── RouteTest.php
212
+
└── HotelTest.php
213
+
```
244
214
245
-
This endpoint updates an existing airline or creates a new one if it does not exist. The client must provide the necessary airline details in the request body. This allows updating airline information or adding new airlines to the database.
215
+
### Key Components
246
216
247
-
**Steps:**
217
+
-**Controllers**: Handle HTTP requests and responses for each entity
218
+
-**Models**: Define data structures and business logic for Couchbase operations
219
+
-**Service Provider**: Configures Couchbase connection and collection references
220
+
-**Tests**: Integration tests for all API endpoints
248
221
249
-
1. The client sends a PUT request to the endpoint `/api/v1/airlines/{id}`.
250
-
2. The client includes the airline ID in the path and provides the updated airline details in the request body.
251
-
3. The server validates the provided data to ensure all required fields are present and correctly formatted.
252
-
4. If the airline exists, the server updates the existing record with the new data. If the airline does not exist, the server creates a new airline with the provided details.
253
-
5. The server returns a success message indicating whether the airline was updated or created. If validation fails, an error message is returned.
222
+
### Airline Entity
254
223
255
-
---
224
+
For this tutorial, we will focus on the airline entity to demonstrate CRUD operations and SQL++ queries.
256
225
257
-
### 5. Delete an Airline
226
+
We will be setting up a REST API to manage airline documents:
258
227
259
-
**Endpoint:**
228
+
-[POST Airline](#post-airline) – Create a new airline
-[List Airlines](#list-airlines) – Get all airlines. Optionally filter the list by country
233
+
-[Airlines to Destination](#airlines-to-destination) – Get airlines flying to a destination airport
260
234
261
-
`DELETE /api/v1/airlines/{id}`
235
+
For CRUD operations, we will use the [Key-Value operations](https://docs.couchbase.com/php-sdk/current/howtos/kv-operations.html) that are built into the Couchbase SDK to create, read, update, and delete a document. Every document will need an ID (similar to a primary key in other databases) to save it to the database. This ID is passed in the URL. For other endpoints, we will use [SQL++](https://docs.couchbase.com/php-sdk/current/howtos/n1ql-queries-with-sdk.html) to query for documents.
262
236
263
-
**Description:**
237
+
### POST Airline
264
238
265
-
This endpoint deletes a specific airline from the database by its ID. This is useful for removing outdated or incorrect airline records.
239
+
Create a new airline document using the POST endpoint.
266
240
267
-
**Steps:**
241
+
### GET Airline
268
242
269
-
1. The client sends a DELETE request to the endpoint `/api/v1/airlines/{id}`.
270
-
2. The client includes the airline ID in the path of the request.
271
-
3. The server searches the database for an airline with the specified ID.
272
-
4. If the airline is found, the server deletes the airline from the database. If the airline is not found, an error message is returned.
243
+
Retrieve a specific airline document by its ID using the GET endpoint.
273
244
274
-
---
245
+
### PUT Airline
275
246
276
-
### 6. Get Airlines Flying to a Destination Airport
247
+
Update an existing airline document or create a new one if it doesn't exist using the PUT endpoint.
Delete an airline document by its ID using the DELETE endpoint.
281
252
282
-
**Description:**
253
+
### List Airlines
283
254
284
-
This endpoint retrieves a list of airlines that fly to a specific destination airport. This can be useful for travelers or systems that need to display available airlines for a particular route.
255
+
Retrieve a list of airlines with optional filtering by country and pagination using query parameters.
285
256
286
-
**Steps:**
257
+
### Airlines to Destination
287
258
288
-
1. The client sends a GET request to the endpoint `/api/v1/airlines/to-airport/{destinationAirportCode}`.
289
-
2. The client includes the destination airport code in the path of the request.
290
-
3. Optionally, the client includes query parameters:
291
-
-`offset` specifies the number of items to skip before starting to collect the result set.
292
-
-`limit` specifies the number of items to return.
293
-
4. The server processes the request and queries the database for airlines that fly to the specified airport.
294
-
5. The server returns a list of airlines. If no airlines are found, an appropriate error message is returned.
259
+
Find airlines that fly to a specific destination airport using SQL++ queries to join collections.
295
260
296
261
## Running The Tests
297
262
@@ -301,25 +266,6 @@ This command will execute all the test cases in your project.
301
266
php artisan test
302
267
```
303
268
304
-
### Run Individual Tests:
305
-
306
-
Additionally, you can run individual test classes or methods using the following commands:
307
-
308
-
To run the tests for the AirlineTest class:
309
-
```sh
310
-
php artisan test --filter AirlineTest
311
-
```
312
-
313
-
To run the tests for the AirportTest class:
314
-
315
-
```sh
316
-
php artisan test --filter AirportTest
317
-
```
318
-
319
-
To run the tests for the RouteTest class:
320
-
321
-
```sh
322
-
php artisan test --filter RouteTest
323
269
```
324
270
325
271
## Project Setup Notes
@@ -341,22 +287,18 @@ If you would like to add another entity to the APIs, follow these steps:
341
287
342
288
- **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).
343
289
344
-
-**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`:
290
+
- **Define the model:** Create a new model in the `app/Models` directory, similar to the existing `Airline` model. Note that this quickstart already includes a `Hotel` model as an example. For a new entity, you would create a similar structure:
345
291
```php
346
292
namespace App\Models;
347
293
348
294
use Illuminate\Database\Eloquent\Model;
349
295
350
-
class Hotel extends Model
296
+
class YourEntity extends Model
351
297
{
352
298
protected $bucket;
353
299
354
300
protected $fillable = [
355
-
'name',
356
-
'address',
357
-
'city',
358
-
'country',
359
-
'stars'
301
+
// Define your entity fields here
360
302
];
361
303
362
304
public function __construct(array $attributes = [])
@@ -365,7 +307,7 @@ If you would like to add another entity to the APIs, follow these steps:
365
307
$this->bucket = app('couchbase.bucket');
366
308
}
367
309
368
-
// Add methods for querying, saving, and deleting Hotel data
310
+
// Add methods for querying, saving, and deleting data
0 commit comments