Skip to content

Commit

Permalink
feat: add search criteria for internal api
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Nov 15, 2023
1 parent b299a08 commit 6049237
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.onecx.theme.domain.criteria;

import io.quarkus.runtime.annotations.RegisterForReflection;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@RegisterForReflection
public class ThemeSearchCriteria {

private String name;

private Integer pageNumber;

private Integer pageSize;

}
19 changes: 19 additions & 0 deletions src/main/java/io/github/onecx/theme/domain/daos/ThemeDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import org.tkit.quarkus.jpa.daos.Page;
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.jpa.exceptions.DAOException;
import org.tkit.quarkus.jpa.utils.QueryCriteriaUtil;

import io.github.onecx.theme.domain.criteria.ThemeSearchCriteria;
import io.github.onecx.theme.domain.models.Theme;
import io.github.onecx.theme.domain.models.ThemeInfo;
import io.github.onecx.theme.domain.models.Theme_;
Expand All @@ -37,6 +39,22 @@ public Stream<Theme> findThemeByNames(Set<String> themeNames) {
}
}

public PageResult<Theme> findThemesByCriteria(ThemeSearchCriteria criteria) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Theme.class);
var root = cq.from(Theme.class);

if (criteria.getName() != null && !criteria.getName().isBlank()) {
cq.where(cb.like(root.get(Theme_.name), QueryCriteriaUtil.wildcard(criteria.getName())));
}

return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_THEMES_BY_CRITERIA, ex);
}
}

public PageResult<Theme> findAll(Integer pageNumber, Integer pageSize) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
Expand Down Expand Up @@ -78,6 +96,7 @@ public Theme findThemeByName(String themeName) {

public enum ErrorKeys {

ERROR_FIND_THEMES_BY_CRITERIA,
ERROR_FIND_ALL_THEME_INFO,
ERROR_FIND_ALL_THEME_PAGE,
ERROR_FIND_THEME_BY_NAMES,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public Response getThemes(Integer pageNumber, Integer pageSize) {
return Response.ok(mapper.mapPage(items)).build();
}

@Override
public Response searchThemes(ThemeSearchCriteriaDTO themeSearchCriteriaDTO) {
var criteria = mapper.map(themeSearchCriteriaDTO);
var result = dao.findThemesByCriteria(criteria);
return Response.ok(mapper.mapPage(result)).build();
}

@Override
@Transactional
public Response updateTheme(String id, UpdateThemeDTO updateThemeDTO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import gen.io.github.onecx.theme.rs.internal.model.*;
import io.github.onecx.theme.domain.criteria.ThemeSearchCriteria;
import io.github.onecx.theme.domain.models.Theme;
import io.github.onecx.theme.domain.models.ThemeInfo;

Expand All @@ -25,6 +26,8 @@ public abstract class ThemeMapper {
@Inject
ObjectMapper mapper;

public abstract ThemeSearchCriteria map(ThemeSearchCriteriaDTO dto);

public ThemeInfoListDTO mapInfoList(Stream<ThemeInfo> data) {
ThemeInfoListDTO result = new ThemeInfoListDTO();
result.setThemes(mapInfo(data));
Expand Down
50 changes: 42 additions & 8 deletions src/main/openapi/onecx-theme-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ paths:
type: array
items:
$ref: '#/components/schemas/ThemePageResult'
"403":
description: Forbidden
"500":
description: Internal Server Error
content:
Expand Down Expand Up @@ -155,12 +153,6 @@ paths:
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Theme'
"204":
description: No Content
"500":
Expand Down Expand Up @@ -190,6 +182,33 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/RestException'
/internal/themes/search:
post:
tags:
- themesInternal
description: Search for themes
operationId: searchThemes
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/ThemeSearchCriteria'
responses:
"200":
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/ThemePageResult'
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/RestException'
/internal/themes/name/{name}:
get:
tags:
Expand Down Expand Up @@ -219,6 +238,21 @@ paths:
$ref: '#/components/schemas/RestException'
components:
schemas:
ThemeSearchCriteria:
type: object
properties:
name:
type: string
pageNumber:
format: int32
description: The number of page.
default: 0
type: integer
pageSize:
format: int32
description: The size of page
default: 10
type: integer
ThemeInfoList:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void beforeAll() {

@Test
void methodExceptionTests() {
methodExceptionTests(() -> dao.findThemesByCriteria(null),
ThemeDAO.ErrorKeys.ERROR_FIND_THEMES_BY_CRITERIA);
methodExceptionTests(() -> dao.findThemeByName(null),
ThemeDAO.ErrorKeys.ERROR_FIND_THEME_BY_NAME);
methodExceptionTests(() -> dao.findThemeByNames(null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,56 @@ void getThemesTest() {

}

@Test
void searchThemesTest() {
var criteria = new ThemeSearchCriteriaDTO();

var data = given()
.contentType(APPLICATION_JSON)
.body(criteria)
.post("/search")
.then()
.statusCode(OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.as(ThemePageResultDTO.class);

assertThat(data).isNotNull();
assertThat(data.getTotalElements()).isEqualTo(3);
assertThat(data.getStream()).isNotNull().hasSize(3);

criteria.setName(" ");
data = given()
.contentType(APPLICATION_JSON)
.body(criteria)
.post("/search")
.then()
.statusCode(OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.as(ThemePageResultDTO.class);

assertThat(data).isNotNull();
assertThat(data.getTotalElements()).isEqualTo(3);
assertThat(data.getStream()).isNotNull().hasSize(3);

criteria.setName("cg");
data = given()
.contentType(APPLICATION_JSON)
.body(criteria)
.post("/search")
.then()
.statusCode(OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.as(ThemePageResultDTO.class);

assertThat(data).isNotNull();
assertThat(data.getTotalElements()).isEqualTo(1);
assertThat(data.getStream()).isNotNull().hasSize(1);

}

@Test
void getThemeInfoListTest() {
var data = given()
Expand Down

0 comments on commit 6049237

Please sign in to comment.