Skip to content

Commit

Permalink
sample: Transactional annotation sample to demonstrate Read-write tra…
Browse files Browse the repository at this point in the history
…nsaction (#2356)
  • Loading branch information
jainsahab authored Nov 20, 2023
1 parent e3579d4 commit 18f265d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
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;

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

0 comments on commit 18f265d

Please sign in to comment.