Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 115 additions & 96 deletions markdown/tests/android/java_relationship.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ In order to save a 1:1 relationship, create the target model instance first and

```java
Author newAuthor = Author.builder()
.name("Rene Brandel")
.build();
.name("Rene Brandel")
.build();
Post post = Post.builder()
.content("My first post!")
.author(newAuthor)
.build();
.content("My first post!")
.author(newAuthor)
.build();

Amplify.DataStore.save(newAuthor,
savedAuthor -> {
Amplify.DataStore.save(post,
savedPost -> Log.i("Amplify DataStore", "Post saved."),
failure -> Log.e("Amplify DataStore", "Error while saving:", failure)
);
},
failure -> Log.e("Amplify DataStore", "Error while saving:", failure)
authorSaved -> {
Amplify.DataStore.save(post,
postSaved -> Log.i("DataStore", "Post saved"),
postSaveFailure ->
Log.e("DataStore", "Failed to save post:", postSaveFailure)
);
},
authorSaveFailure ->
Log.e("DataStore", "Failed to save author:", authorSaveFailure)
);
```
Here we've first created a new `Author` instance and then saved it to the `Post`'s "author" relationship field.
Expand All @@ -34,13 +36,14 @@ To query one-to-one relationships, access the target model instance through its

```java
Amplify.DataStore.query(Post.class,
matches -> {
while (matches.hasNext()) {
Post post = matches.next();
Author author = post.getAuthor();
}
},
failure -> Log.e("Amplify DataStore", "Query failed.", failure)
matchingPosts -> {
while (matchingPosts.hasNext()) {
Post post = matchingPosts.next();
Author author = post.getAuthor();
Log.i("DataStore", "Author: " + author.toString());
}
},
failure -> Log.e("DataStore", "Query failed", failure)
);
```

Expand All @@ -50,8 +53,9 @@ In one-to-one relationships, if the target model instance is deleted, it will al

```java
Amplify.DataStore.delete(author,
deleted -> Log.i("Amplify DataStore", "Author + Post deleted"),
failure -> Log.e("Amplify DataStore", "Deletion failed", failure));
success -> Log.i("DataStore", "Author + Post deleted"),
failure -> Log.e("DataStore", "Deletion failed", failure)
);
```

In this example, the `Post`'s "author" field will be cleared and the `Author` model instance will be deleted.
Expand All @@ -67,22 +71,23 @@ In order to save a one-to-many relationship, create the source model instance fi

```java
Publication publication = Publication.builder()
.title("Amplify Weekly")
.build();
.title("Amplify Weekly")
.build();

Article article = Article.builder()
.publicationId(publication.getId())
.title("Add auth to your app in 3 steps")
.build();
.publicationId(publication.getId())
.title("Add auth to your app in 3 steps")
.build();

Amplify.DataStore.save(publication,
savedPublication -> {
Amplify.DataStore.save(article,
savedArticle -> Log.i("Amplify DataStore", "Article saved." + savedArticle),
failure -> Log.e("Amplify DataStore", "Error while saving:", failure)
);
},
failure -> Log.e("Amplify DataStore", "Error while saving:", failure)
pubSaved -> {
Amplify.DataStore.save(article,
articleSaved -> Log.i("DataStore", "Article saved"),
articleSaveFailure ->
Log.e("DataStore", "Failed to save article", articleSaveFailure)
);
},
pubSaveFailure -> Log.e("DataStore", "Error while saving:", pubSaveFailure)
);
```
Here we've first created a new `Publication` instance and then saved its _id_ to the `Article`'s "publicationID" relationship field.
Expand All @@ -92,41 +97,49 @@ Here we've first created a new `Publication` instance and then saved its _id_ to
To query one-to-many relationships, filter based on the source model instance's id on the target model.

```java
Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")),
matches -> {
while(matches.hasNext()) {
Article article = matches.next();
Log.i("Amplify DataStore", "Matched article: " + article);
}
},
failure -> Log.e("Amplify DataStore", "Query failed.", failure));
QueryPredicate conditions = Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")
Amplify.DataStore.query(Article.class, Where.matches(conditions),
matchingArticles -> {
while (matchingArticles.hasNext()) {
Article article = matchingArticles.next();
Log.i("DataStore", "Matched article: " + article);
}
},
failure -> Log.e("DataStore", "Query failed", failure)
);
```

**Delete**

In one-to-many relationships, delete the target model instance first and then delete the source model.

```java
Amplify.DataStore.query(Article.class, Where.matches(Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID")),
matches -> {
while (matches.hasNext()) {
Article article = matches.next();
Amplify.DataStore.delete(article,
deletedArticle -> Log.i("Amplify DataStore", "Article deleted"),
failure -> {});
}
Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"),
matchedPublications -> {
while(matchedPublications.hasNext()) {
Publication match = matchedPublications.next();
Amplify.DataStore.delete(match,
deleted -> Log.i("Amplify DataStore", "Publication deleted"),
failure -> Log.e("Amplify DataStore", "Deletion failed.", failure));
}
},
failure -> {});

}, failure -> {}
QueryPredicate conditions = Article.PUBLICATION_ID.eq("YOUR_PUBLICATION_ID");
Amplify.DataStore.query(Article.class, Where.matches(conditions),
matchingArticles -> {
while (matchingArticles.hasNext()) {
Article article = matchingArticles.next();
Amplify.DataStore.delete(article,
articleDeleted -> Log.i("DataStore", "Article deleted"),
articleDeletionFailure ->
Log.e("DataStore", "Failed to delete article", articleDeletionFailure)
);
}
Amplify.DataStore.query(Publication.class, Where.id("YOUR_PUBLICATION_ID"),
matchingPubs -> {
while (matchingPubs.hasNext()) {
Publication publication = matchingPubs.next();
Amplify.DataStore.delete(publication,
pubDeleted -> Log.i("DataStore", "Publication deleted"),
pubDeletionFailure ->
Log.e("DataStore", "Failed to delete publication", pubDeletionFailure)
);
}
},
pubQueryFailure -> Log.e("DataStore", "Failed to query publications", pubQueryFailure)
);
},
articleQueryFailure -> Log.e("DataStore", "Failed to query articles", articleQueryFailure)
);
```

Expand All @@ -145,33 +158,35 @@ In order to save a many-to-many relationship, create both model instance first a

```java
Post post = Post.builder()
.body("How to build deploy a web app on AWS Amplify")
.build();
.body("How to build deploy a web app on AWS Amplify")
.build();

Tag tag = Tag.builder()
.label("static-web-hosting")
.build();
.label("static-web-hosting")
.build();

PostTag postTag = PostTag.builder()
.post(post)
.tag(tag)
.build();
.post(post)
.tag(tag)
.build();

Amplify.DataStore.save(post,
savedPost -> {
Log.i("Amplify DataStore", "post saved.");
Amplify.DataStore.save(post,
postSaved -> {
Log.i("DataStore", "Post saved");
Amplify.DataStore.save(tag,
savedEditor -> {
Log.i("Amplify DataStore", "Tag saved.");
Amplify.DataStore.save(postTag,
saved -> Log.i("Amplify DataStore", "PostTag saved."),
failure -> Log.e("Amplify DataStore", "PostTag not saved.", failure)
);
},
failure -> Log.e("Amplify DataStore", "Tag not saved.", failure)
tagSaved -> {
Log.i("DataStore", "Tag saved");
Amplify.DataStore.save(postTag,
postTagSaved -> Log.i("DataStore", "PostTag saved"),
postTagSaveFailure ->
Log.e("DataStore", "PostTag not saved", postTagSaveFailure)
);
},
tagSaveFailure ->
Log.e("DataStore", "Tag not saved", tagSaveFailure)
);
},
failure -> Log.e("Amplify DataStore", "Post not saved.", failure)
postSaveFailure -> Log.e("DataStore", "Post not saved", postSaveFailure)
);
```

Expand All @@ -182,13 +197,17 @@ Here we've first created a new `Post` instance and a new `Tag` instance. Then, s
To query many-to-many relationships, filter the join model based on one of the model's _id_.

```java
Amplify.DataStore.query(ContentTag.class, Where.matches(ContentTag.CONTENT.eq("YOUR_CONTENT_ID")),
matches -> {
while (matches.hasNext()) {
ContentTag contentTag = matches.next();
Log.i("Amplify DataStore", "Tag: " + contentTag.getTag());
}
}, failure -> {});

QueryPredicate conditions = ContentTag.CONTENT.eq("YOUR_CONTENT_ID");
Amplify.DataStore.query(ContentTag.class, Where.matches(conditions)),
matchingContentTags -> {
while (matchingContentTags.hasNext()) {
ContentTag contentTag = matchingContentTags.next();
Log.i("DataStore", "Tag: " + contentTag.getTag());
}
},
failure -> Log.e("DataStore", "Failed to query ContentTags", failure)
);
```

In this example, first filter the _join model_ `PostTag` with your `Post`'s _id, then map the `PostTag`s to `Tag`s.
Expand All @@ -198,19 +217,19 @@ In this example, first filter the _join model_ `PostTag` with your `Post`'s _id,
Deleting the _join model instance_ will not delete any source model instances.

```java
Amplify.DataStore.delete(
toBeDeletedPostTag,
deleted -> Log.i("Amplify DataStore", "Deleted " + deleted),
failure -> Log.e("Amplify DataStore", "Deletion failed", failure));
Amplify.DataStore.delete(toBeDeletedPostTag,
success -> Log.i("DataStore", "PostTag deleted"),
failure -> Log.e("DataStore", "Deletion failed", failure)
);
```
Both the `Post` and the `Tag` instances will not be deleted. Only the join model instances containing the link between a `Post` and a `Tag`.

Deleting a _source model instance_ will also delete the join model instances containing the source model instance.
```java
Amplify.DataStore.delete(
toBeDeletedTag,
deleted -> Log.i("Amplify DataStore", "Deleted " + deleted),
failure -> Log.e("Amplify DataStore", "Deletion failed", failure));
Amplify.DataStore.delete(toBeDeletedTag,
success -> Log.i("DataStore", "PostTag deleted"),
failure -> Log.e("DataStore", "Deletion failed", failure)
);

```
The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted.
The `toBeDeletedTag` `Tag` instance and all `PostTag` instances where _tag_ is linked to `toBeDeletedTag` will be deleted.
2 changes: 1 addition & 1 deletion markdown/tests/android/java_step3.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Install the Amplify CLI:
Run the following command from your project's root folder:
```bash
amplify pull --sandboxId :::BACKEND_MANAGER_ID:::
```
```
9 changes: 4 additions & 5 deletions markdown/tests/android/java_step4.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ dependencies {
```
```gradle
// Amplify plugins
implementation 'com.amplifyframework:core:1.6.5'
implementation 'com.amplifyframework:aws-api:1.6.5'
implementation 'com.amplifyframework:aws-datastore:1.6.5'
implementation 'com.amplifyframework:aws-api:1.16.11'
implementation 'com.amplifyframework:aws-datastore:1.16.11'

// Support for Java 8 features
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.10'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.1'
```
```gradle
:::NO_COPY:::
Expand Down Expand Up @@ -62,4 +61,4 @@ public class MainActivity extends AppCompatActivity {
:::NO_COPY:::
}
}
```
```
2 changes: 1 addition & 1 deletion markdown/tests/android/java_step5.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Amplify DataStore allows you to manage your app data without writing additional code for offline and online scenarios.
Amplify DataStore allows you to manage your app data without writing additional code for offline and online scenarios.
36 changes: 20 additions & 16 deletions markdown/tests/android/java_step5_auth_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@
:::GET_AUTH_SESSION:::
```java
Amplify.Auth.fetchAuthSession(
result -> Log.i("Amplify Auth", result.toString()),
error -> Log.e("Amplify Auth", error.toString())
session -> Log.i("AmplifyAuth", "Auth Session: " + session.toString()),
failure -> Log.e("AmplifyAuth", "Failed to fetch auth session", failure)
);
```

:::NEW_COMMAND:::
:::SIGNIN:::
```java
Amplify.Auth.signIn(
"username",
"password",
result -> Log.i("Amplify Auth", result.isSignInComplete() ? "Sign in succeeded" : "Sign in not complete"),
error -> Log.e("Amplify Auth", error.toString())
Amplify.Auth.signIn("username", "password",
result -> {
if (result.isSignInComplete()) {
Log.i("AmplifyAuth", "Sign-in succeeded");
} else {
Log.w("AmplifyAuth", "Sign in not complete");
}
},
failure -> Log.e("AmplifyAuth", "Failed to sign-in.", failure)
);
```
:::NEW_COMMAND:::
:::SIGNOUT:::
```java
Amplify.Auth.signOut(
() -> Log.i("Amplify Auth", "Signed out successfully"),
error -> Log.e("Amplify Auth", error.toString())
() -> Log.i("AmplifyAuth", "Signed out successfully"),
failure -> Log.e("AmplifyAuth", "Failed to sign out.", failure)
);
```
:::NEW_COMMAND:::
:::SIGNUP:::
```java
Amplify.Auth.signUp(
"username",
"Password123",
AuthSignUpOptions.builder().userAttribute(AuthUserAttributeKey.email(), "my@email.com").build(),
result -> Log.i("Amplify Auth", "Result: " + result.toString()),
error -> Log.e("Amplify Auth", "Sign up failed", error)
Amplify.Auth.signUp("username", "Password123",
AuthSignUpOptions.builder()
.userAttribute(AuthUserAttributeKey.email(), "my@email.com")
.build(),
result -> Log.i("AmplifyAuth", "Result: " + result.toString()),
failure -> Log.e("AmplifyAuth", "Sign up failed", failure)
);
```
```
Loading