Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sample: Transactional annotation sample to demonstrate Read-write transaction #2356

Merged
merged 2 commits into from
Nov 20, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
import java.util.Map;
import java.util.UUID;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.Persistable;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

/** Book entity. */
@Table
public class Book implements Persistable {
public class Book implements Persistable<String> {

@Id
@Column("ID")
Expand All @@ -44,11 +45,19 @@ public class Book implements Persistable {
@Column("CATEGORIES")
private List<String> categories;

@Column("COUNT")
private int count;

@Transient
private boolean isNew;

jainsahab marked this conversation as resolved.
Show resolved Hide resolved
public Book(String title, Map<String, String> extraDetails, Review review) {
this.id = UUID.randomUUID().toString();
this.title = title;
this.extraDetails = extraDetails;
this.review = review;
this.count = 0;
this.isNew = true;
}

public String getId() {
Expand All @@ -57,7 +66,7 @@ public String getId() {

@Override
public boolean isNew() {
return true;
return this.isNew;
}

public String getTitle() {
Expand All @@ -80,6 +89,15 @@ public void setCategories(List<String> categories) {
this.categories = categories;
}

public int getCount() {
return count;
}

public void incrementCount() {
this.count++;
this.isNew = false;
}

@Override
public String toString() {
return "Book{"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public void setUpData() {
+ " TITLE STRING(MAX) NOT NULL,"
+ " EXTRADETAILS JSON,"
+ " REVIEWS JSON,"
+ " CATEGORIES ARRAY<STRING(64)>"
+ " CATEGORIES ARRAY<STRING(64)>,"
+ " COUNT INT64 NOT NULL"
+ ") PRIMARY KEY (ID)")
.fetch()
.rowsUpdated()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -62,4 +63,14 @@ public Mono<Void> deleteAll() {
public Mono<Book> searchBooks(@PathVariable String id) {
return r2dbcRepository.findById(id);
}

@Transactional
@PostMapping("/increment-count/{id}")
public Mono<Void> incrementCount(@PathVariable String id) {
return r2dbcRepository.findById(id)
.doOnNext(Book::incrementCount)
.flatMap(r2dbcRepository::save)
.log()
.then();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@
return false;
}

function incrementCount() {
const id = document.getElementById('bookIdCountIncrement').value;
if (!id) {
appendOutput("Please provide non-empty id");
return false;
}

fetch('/increment-count/' + id, {
method: 'POST',
headers: { 'Content-Type': 'application/json'}
}).then(response => {
if (response.ok) {
appendOutput("Count updated.");
} else {
appendOutput("Failed to update count.");
}
});

return false;
}

</script>

<style>
Expand Down Expand Up @@ -142,6 +163,10 @@ <h3>Comma-separated categories (List entity field turning into ARRAY<STRING> Spa
Search for: <input id="bookId" name="bookId"/> <a class="buttonLink" href="/cloud-spanner-r2dbc-samples/cloud-spanner-spring-data-r2dbc-sample/src/main/resources/static" onClick="return findBookById();">Find
Books</a>
</div>

<div class="buttonLink">
Read count: <input id="bookIdCountIncrement" name="bookId" placeholder="Book ID"/> <a class="buttonLink" href="/cloud-spanner-r2dbc-samples/cloud-spanner-spring-data-r2dbc-sample/src/main/resources/static" onClick="return incrementCount();">Increment</a>
</div>
</div>

<label for="result" style="display:block">Command Output</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ void testBasicWebEndpoints() throws JsonProcessingException {
books -> {
assertThat(books).hasSize(1);
assertThat(books[0].getTitle()).isEqualTo("Call of the wild");
assertThat(books[0].getCount()).isEqualTo(0);
id.set(books[0].getId());
});

Expand All @@ -105,6 +106,24 @@ void testBasicWebEndpoints() throws JsonProcessingException {
book -> {
assertThat(book.getTitle()).isEqualTo("Call of the wild");
});

this.webTestClient
.post()
.uri("/increment-count/" + id.get())
.exchange()
.expectStatus()
.is2xxSuccessful();

this.webTestClient
.get()
.uri("/search/" + id.get())
.exchange()
.expectBody(Book.class)
.value(
book -> {
assertThat(book.getCount()).isEqualTo(1);
});

}

@Test
Expand Down
Loading