Skip to content

Commit

Permalink
Fix Sample application code and fix instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
AV25242 committed May 11, 2020
1 parent 12e00cd commit 29b03ad
Showing 1 changed file with 19 additions and 90 deletions.
109 changes: 19 additions & 90 deletions modules/hello-world/pages/sample-application.adoc
Expand Up @@ -18,7 +18,7 @@ set up as described above, you will need `git` to fetch the travel sample applic

[source,bash]
----
git clone git@github.com:couchbaselabs/try-cb-nodejs.git
git clone https://github.com/couchbaselabs/try-cb-nodejs.git
----

Change directory into your cloned repository, and check out the latest branch (this will most probably be enabled as the default branch).
Expand All @@ -33,7 +33,7 @@ npm install

== Running the Travel Sample Application

Next, edit the `storage.host` field in src/main/resources/application.properties to the one for your containerised Couchbase Server (or localhost, 127.0.0.1, if appropriate), and any other local changes -- such as password.
Next, edit the https://github.com/couchbaselabs/try-cb-nodejs/blob/6.5/index.js[index.js] file to point to one for your containerised Couchbase Server (or localhost, 127.0.0.1, if appropriate), and any other configuration changes -- such as password.
From here onwards, we'll assume the defaults.

And run with
Expand Down Expand Up @@ -66,103 +66,32 @@ Here's the airport search code, which checks to see whether the search term for

[source,javascript]
----
var qs;
var qs;
if (searchTerm.length === 3) {
// FAA code
qs = "SELECT airportname from `travel-sample` WHERE faa = '" + searchTerm.toUpperCase() + "';";
qs = `SELECT airportname from \`travel-sample\` WHERE faa = '${searchTerm.toUpperCase()}';`;
} else if (searchTerm.length === 4 &&
(searchTerm.toUpperCase() === searchTerm ||
searchTerm.toLowerCase() === searchTerm)) {
// ICAO code
qs = "SELECT airportname from `travel-sample` WHERE icao = '" + searchTerm.toUpperCase() + "';";
qs = `SELECT airportname from \`travel-sample\` WHERE icao = '${searchTerm.toUpperCase()}';`;
} else {
// Airport name
qs = "SELECT airportname from `travel-sample` WHERE LOWER(airportname) LIKE '%" + searchTerm.toLowerCase() + "%';";
qs = `SELECT airportname from \`travel-sample\` WHERE LOWER(airportname) LIKE '%${searchTerm.toLowerCase()}%';`;
}
let result = await coll.query(qs);
let rows = result.rows;
----

The https://github.com/couchbaselabs/try-cb-nodejs/blob/6.5/index.js[index.js] file also contains the functions for handling users, registration, and N1QL queries.




////
Look at `User.java` to see some of the pieces necessary in most applications, such as the User `@Service`:
[source,java]
----
@Service
public class User {
private final TokenService jwtService;
@Autowired
public User(TokenService jwtService) {
this.jwtService = jwtService;
}
static final String USERS_COLLECTION_NAME = "users";
static final String FLIGHTS_COLLECTION_NAME = "flights";
let result, rows;
try {
result = await cluster.query(qs);
rows = result.rows;
} catch (error) {
console.error(error)
};
res.send({
data: rows,
context: [qs]
});
----

Here showing _users_ and _flights_ as Collections.
Creating a user shows the typical security concerns, with salted password hashes, as well as the mundane but essential business of using the KV interface to `insert` the username into the database:
[source,java]
----
public Result<Map<String, Object>> createLogin(final Scope scope, final String username, final String password,
DurabilityLevel expiry) {
String passHash = BCrypt.hashpw(password, BCrypt.gensalt());
JsonObject doc = JsonObject.create()
.put("type", "user")
.put("name", username)
.put("password", passHash);
InsertOptions options = insertOptions();
if (expiry.ordinal() > 0) {
options.durability(expiry);
}
String narration = "User account created in document " + username + " in bucket " + scope.bucketName()
+ " scope " + scope.name() + " collection " + USERS_COLLECTION_NAME
+ (expiry.ordinal() > 0 ? ", with expiry of " + expiry.ordinal() + "s" : "");
try {
scope.collection(USERS_COLLECTION_NAME).insert(username, doc);
return Result.of(
JsonObject.create().put("token", jwtService.buildToken(username)).toMap(),
narration);
} catch (Exception e) {
e.printStackTrace();
throw new AuthenticationServiceException("There was an error creating account");
}
}
----
Here, the _flights_ array, containing the flight IDs, is converted to actual objects:
[source,java]
----
Collection flightsCollection = scope.collection(FLIGHTS_COLLECTION_NAME);
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (int i = 0; i < flights.size(); i++) {
String flightId = flights.getString(i);
GetResult res;
try {
res = flightsCollection.get(flightId);
} catch (DocumentNotFoundException ex) {
throw new RuntimeException("Unable to retrieve flight id " + flightId);
}
Map<String, Object> flight = res.contentAsObject().toMap();
results.add(flight);
}
return results;
}
}
----
////


The https://github.com/couchbaselabs/try-cb-nodejs/blob/6.5/index.js[index.js] file also contains the functions for handling users, registration, and N1QL queries.

0 comments on commit 29b03ad

Please sign in to comment.