Skip to content

Commit

Permalink
changes to open-api-generator server guide to use JDBC (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdelamo authored Mar 3, 2022
1 parent 96153ce commit ae8d20d
Show file tree
Hide file tree
Showing 25 changed files with 342 additions and 243 deletions.
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
implementation "io.micronaut.docs:micronaut-docs-asciidoc-extensions:$micronautDocsVersion", {
exclude group: "org.codehaus.groovy"
}
implementation("org.openapitools:openapi-generator:5.4.0")
implementation("org.openapitools:openapi-generator:$openapiGeneratorVersion")
}

sourceSets {
Expand Down
1 change: 1 addition & 0 deletions buildSrc/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openapiGeneratorVersion=5.4.0
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class GuideProjectGenerator implements AutoCloseable {
"${slug}-${guidesOption.buildTool}-${guidesOption.language}"
}

private void generateOne(GuideMetadata metadata, File inputDir, File outputDir) {
void generateOne(GuideMetadata metadata, File inputDir, File outputDir) {
if (!outputDir.exists()) {
assert outputDir.mkdir()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
public class JakartaPersistenceApi extends AbstractFeature {

public JakartaPersistenceApi() {
super("jakarta-persistence-api", "jakarta.persistence-api", COMPILE_ONLY);
super("jakarta-persistence-api", "jakarta.persistence-api");
}
}
11 changes: 5 additions & 6 deletions buildSrc/src/main/resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-xe</artifactId>
Expand Down Expand Up @@ -67,12 +72,6 @@
<version>0.4.17</version>
</dependency>

<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.2</version>
</dependency>

<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

org.gradle.parallel=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package example.micronaut;

import example.micronaut.model.BookAvailability;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.data.annotation.GeneratedValue;
import io.micronaut.data.annotation.Id;
import io.micronaut.data.annotation.MappedEntity;
import io.micronaut.data.annotation.MappedProperty;

import javax.validation.constraints.NotNull;

@MappedEntity("book") // <1>
public class BookEntity {

@Id // <2>
@GeneratedValue(GeneratedValue.Type.AUTO)
private Long id;

@NonNull
@NotNull
private String name;

@NonNull
@NotNull
private BookAvailability availability;

@Nullable
private String author;

@Nullable
@MappedProperty("ISBN")
private String isbn;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@NonNull
public BookAvailability getAvailability() {
return availability;
}

public void setAvailability(@NonNull BookAvailability availability) {
this.availability = availability;
}

@Nullable
public String getIsbn() {
return isbn;
}

public void setIsbn(@Nullable String isbn) {
this.isbn = isbn;
}

@NonNull
public String getName() {
return name;
}

public void setName(@NonNull String name) {
this.name = name;
}

@Nullable
public String getAuthor() {
return author;
}

public void setAuthor(@Nullable String author) {
this.author = author;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package example.micronaut;

import example.micronaut.model.BookAvailability;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.GenericRepository;
import io.micronaut.data.repository.jpa.JpaSpecificationExecutor;
import io.micronaut.data.repository.jpa.criteria.PredicateSpecification;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@JdbcRepository(dialect = Dialect.MYSQL) // <1>
public interface BookRepository extends GenericRepository<BookEntity, Long>, // <2>
JpaSpecificationExecutor<BookEntity> { // <3>

@NonNull
List<BookEntity> findAll(PredicateSpecification<BookEntity> spec); // <4>

@NonNull
List<BookEntity> findAll();


@NonNull
BookEntity save(@NonNull @NotBlank String name,
@NonNull @NotNull BookAvailability availability,
@NonNull @NotBlank String author,
@NonNull @NotBlank String isbn);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package example.micronaut;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.repository.jpa.criteria.PredicateSpecification;

public class BookSpecifications {
public static PredicateSpecification<BookEntity> nameLike(@NonNull String name) {
return (root, criteriaBuilder) -> criteriaBuilder.like(root.get("name"), "%"+name+"%");
}

public static PredicateSpecification<BookEntity> authorLike(@NonNull String author) {
return (root, criteriaBuilder) -> criteriaBuilder.like(root.get("author"), "%"+author+"%");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This is a library API
*
* The version of the OpenAPI document: 1.0.0
*
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
Expand All @@ -13,33 +13,48 @@
package example.micronaut.controller;

//tag::import[]
import example.micronaut.BookService;
//end::import[]
import io.micronaut.http.annotation.*;
import example.micronaut.BookEntity;
import example.micronaut.BookRepository;
import example.micronaut.BookSpecifications;
import example.micronaut.model.BookAvailability;
import example.micronaut.model.BookInfo;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.convert.format.Format;
import io.micronaut.core.util.StringUtils;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Post;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.micronaut.security.annotation.Secured;
import io.micronaut.security.rules.SecurityRule;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import reactor.core.publisher.Mono;
import example.micronaut.model.BookInfo;
import static io.micronaut.http.HttpStatus.OK;
import javax.annotation.Generated;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.*;
import io.swagger.annotations.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.stream.Collectors;
import io.micronaut.http.annotation.Status;
//end::import[]

@Generated(value="org.openapitools.codegen.languages.JavaMicronautServerCodegen", date="2022-01-31T13:21:29.860-05:00[America/Toronto]")
@Controller("${context-path}")
public class BooksController {

//tag::inject[]
private final BookService bookService; // <1>
private final BookRepository bookRepository; // <1>

public BooksController(BookService bookService) { // <1>
this.bookService = bookService;
public BooksController(BookRepository bookRepository) { // <1>
this.bookRepository = bookRepository;
}
//end::inject[]

Expand All @@ -49,25 +64,25 @@ public BooksController(BookService bookService) { // <1>
* @param bookInfo (required)
*/
@ApiOperation(
value = "Add a new book",
nickname = "addBook",
authorizations = {},
tags={})
value = "Add a new book",
nickname = "addBook",
authorizations = {},
tags={})
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success"),
@ApiResponse(code = 400, message = "Bad Request")})
@ApiResponse(code = 200, message = "Success"),
@ApiResponse(code = 400, message = "Bad Request")})
@Post(uri="/add")
@Produces(value = {})
@Consumes(value = {"application/json"})
@Secured(SecurityRule.IS_ANONYMOUS)
//tag::addBook[]
public Mono<Object> addBook(
@Body @NotNull @Valid BookInfo bookInfo
) {
return Mono.fromCallable(() -> {
bookService.addBook(bookInfo);
return ""; // <1>
});
@ExecuteOn(TaskExecutors.IO) // <1>
@Status(OK) // <2>
public void addBook(@Body @NotNull @Valid BookInfo bookInfo) {
bookRepository.save(bookInfo.getName(),
bookInfo.getAvailability(),
bookInfo.getAuthor(),
bookInfo.getISBN());
}
//end::addBook[]

Expand All @@ -79,24 +94,49 @@ public Mono<Object> addBook(
* @return List&lt;BookInfo&gt;
*/
@ApiOperation(
value = "Search for a book",
nickname = "search",
response = BookInfo.class,
responseContainer = "array",
authorizations = {},
tags={})
value = "Search for a book",
nickname = "search",
response = BookInfo.class,
responseContainer = "array",
authorizations = {},
tags={})
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = BookInfo.class, responseContainer = "array"),
@ApiResponse(code = 400, message = "Bad Request")})
@ApiResponse(code = 200, message = "Success", response = BookInfo.class, responseContainer = "array"),
@ApiResponse(code = 400, message = "Bad Request")})
@Get(uri="/search")
@Produces(value = {"applicaton/json"})
@Secured(SecurityRule.IS_ANONYMOUS)
//tag::search[]
public Mono<List<BookInfo>> search(
@QueryValue(value="book-name") @Nullable @Size(min=3) String bookName,
@QueryValue(value="author-name") @Nullable String authorName
) {
return Mono.fromCallable(() -> bookService.searchForBook(bookName, authorName)); // <1>
@ExecuteOn(TaskExecutors.IO) // <1>
public List<BookInfo> search(
@QueryValue(value="book-name") @Nullable @Size(min=3) String bookName,
@QueryValue(value="author-name") @Nullable String authorName) {
return searchEntities(bookName, authorName)
.stream()
.map(this::map)
.collect(Collectors.toList());
}

private BookInfo map(BookEntity entity) {
BookInfo book = new BookInfo(entity.getName(), entity.getAvailability());
book.setISBN(entity.getIsbn());
book.setAuthor(entity.getAuthor());
return book;
}

@NonNull
private List<BookEntity> searchEntities(@Nullable String name, @Nullable String author) {
if (StringUtils.isEmpty(name) && StringUtils.isEmpty(author)) {
return bookRepository.findAll();
} else if (StringUtils.isEmpty(name)) {
return bookRepository.findAll(BookSpecifications.authorLike(author));

} else if (StringUtils.isEmpty(author)) {
return bookRepository.findAll(BookSpecifications.nameLike(name));
} else {
return bookRepository.findAll(BookSpecifications.authorLike(author)
.and(BookSpecifications.nameLike(name))); // <2>
}
}
//end::search[]
}
Loading

0 comments on commit ae8d20d

Please sign in to comment.