Skip to content

Commit 4edc2b0

Browse files
unicodeveloperpeggyrayzis
authored andcommitted
Add reference to isBookedOnLaunch method
1 parent 55ef230 commit 4edc2b0

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

‎docs/source/tutorial/data-source.md‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Now that you have an understanding of how data sources work, let's hook it up fo
4949

5050
Create a new `datasources` folder inside the `src` directory. This folder will contain our data source files. Now, create `launch.js` within the `datasources` directory.
5151

52-
The REST API endpoint we'll use for our app is `https://api.spacexdata.com/v2/`. Add the endpoint as the base URL as shown in the code below:
52+
The REST API endpoint we'll use for our app is `https://api.spacexdata.com/v2/`. Go ahead and add the endpoint as the base URL as shown in the code below:
5353

5454
_src/datasources/launch.js_
5555

@@ -132,7 +132,7 @@ async getAllLaunches() {
132132

133133
With the above changes, we can easily make changes to the `launchReducer` method while the `getAllLaunches` method stays lean and concise. The `launchReducer` method also makes testing the `LaunchAPI` data source class easier. The later part of this tutorial covers testing!
134134

135-
Next, let's take care of getting a specific launch. Add the following methods, `getLaunchById`, and `getLaunchesByIds` to the `LaunchAPI` class.
135+
Next, let's take care of getting a specific launch. Add the method, `getLaunchById`, and `getLaunchesByIds` to the `LaunchAPI` class.
136136

137137
```js
138138
...
@@ -259,7 +259,7 @@ The `cancelTrip` method requires a `userId` and `launchId` to delete a trip from
259259

260260
<h4 id="get-launches">Get All Launches By User</h4>
261261

262-
We need to get all the launches reserved by a user. This calls for a method, `getLaunchIdsByUser`. Copy the code below and add it to the file.
262+
We need to get all the launches reserved by a user. This calls for a method, `getLaunchIdsByUser`. Copy the method below and add it to the file.
263263

264264
_src/datasources/user.js_
265265

@@ -289,8 +289,26 @@ Let's analyze the code above.
289289

290290
In the `getLaunchIdsByUser` method, a `userId` is accepted via the `context` object. All the trips booked by a user with a particular `userId` are fetched and stored in the `found` variable. If there are trips found, then an array of launch ids are returned else an empty array is returned.
291291

292+
<h4 id="get-launches">Get Booked Status on a Launch</h4>
292293

293-
In the various methods that we created and copied to the `UserAPI` class, you must have noticed `this.store.users` and `this.store.trips`. These are two tables from our SQLite data store.
294+
We need to add a method that can return the booked status of a launch for a particular user. Copy the method, `isBookedOnLaunch` below and add it to the `UserAPI` class.
295+
296+
_src/datasources/user.js_
297+
298+
```js
299+
...
300+
async isBookedOnLaunch({ launchId }) {
301+
const userId = this.context.user.id;
302+
const found = await this.store.trips.findAll({
303+
where: { userId, launchId },
304+
});
305+
return found && found.length > 0;
306+
}
307+
```
308+
309+
In the `isBookedOnLaunch` method, we invoke the `findAll` method of the `trips` table to find out if the particular launch passed to the method via `launchId` has been booked by the logged-in user.
310+
311+
In the various methods that we created and copied to the `UserAPI` class, you must have noticed `this.store.users` and `this.store.trips`. These are two tables from our potential SQLite data store. Let's create the store!
294312

295313
<h4 id="create-the-store">Create the Store</h4>
296314

0 commit comments

Comments
 (0)