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

feat: add external v1 api #3

Merged
merged 1 commit into from
Nov 13, 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
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@
<modelNameSuffix>DTOV1</modelNameSuffix>
</configuration>
</execution>
<execution>
<id>v1</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/openapi/onecx-theme-v1.yaml</inputSpec>
<apiPackage>gen.io.github.onecx.theme.rs.external.v1</apiPackage>
<modelPackage>gen.io.github.onecx.theme.rs.external.v1.model</modelPackage>
<modelNameSuffix>DTOV1</modelNameSuffix>
<apiNameSuffix>V1Api</apiNameSuffix>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/io/github/onecx/theme/domain/daos/ThemeDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@
import jakarta.transaction.Transactional;

import org.tkit.quarkus.jpa.daos.AbstractDAO;
import org.tkit.quarkus.jpa.daos.Page;
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.jpa.exceptions.DAOException;

import io.github.onecx.theme.domain.models.Theme;
import io.github.onecx.theme.domain.models.ThemeInfo;
import io.github.onecx.theme.domain.models.Theme_;

@ApplicationScoped
Expand All @@ -27,14 +30,36 @@ public Stream<Theme> findThemeByNames(Set<String> themeNames) {
if (themeNames != null && !themeNames.isEmpty()) {
cq.where(root.get(Theme_.name).in(themeNames));
}

return this.getEntityManager().createQuery(cq).getResultStream();

} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_THEME_BY_NAMES, ex);
}
}

public PageResult<Theme> findAll(Integer pageNumber, Integer pageSize) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Theme.class);
cq.from(Theme.class);
return createPageQuery(cq, Page.of(pageNumber, pageSize)).getPageResult();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_ALL_THEME_PAGE, ex);
}
}

public Stream<ThemeInfo> findAllInfos() {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(ThemeInfo.class);
var root = cq.from(Theme.class);
cq.select(cb.construct(ThemeInfo.class, root.get(Theme_.NAME), root.get(Theme_.DESCRIPTION)));
return this.getEntityManager().createQuery(cq).getResultStream();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_ALL_THEME_INFO, ex);
}
}

public Theme findThemeByName(String themeName) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
Expand All @@ -53,6 +78,8 @@ public Theme findThemeByName(String themeName) {

public enum ErrorKeys {

ERROR_FIND_ALL_THEME_INFO,
ERROR_FIND_ALL_THEME_PAGE,
ERROR_FIND_THEME_BY_NAMES,
ERROR_FIND_THEME_BY_NAME,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.onecx.theme.domain.models;

public record ThemeInfo(String name, String description) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.github.onecx.theme.rs.external.v1.controllers;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;

import gen.io.github.onecx.theme.rs.external.v1.ThemesV1Api;
import gen.io.github.onecx.theme.rs.external.v1.model.RestExceptionDTOV1;
import io.github.onecx.theme.domain.daos.ThemeDAO;
import io.github.onecx.theme.rs.external.v1.mappers.ExceptionMapper;
import io.github.onecx.theme.rs.external.v1.mappers.ThemeMapper;

@Path("/v1/themes")
@ApplicationScoped
@Transactional(Transactional.TxType.NOT_SUPPORTED)
public class ThemesRestControllerV1 implements ThemesV1Api {

@Inject
ThemeDAO dao;

@Inject
ExceptionMapper exceptionMapper;

@Inject
ThemeMapper mapper;

@Override
public Response getThemeByThemeDefinitionName(String name) {
var theme = dao.findThemeByName(name);
if (theme == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(mapper.map(theme)).build();
}

@Override
public Response getThemeInfoList() {
var items = dao.findAllInfos();
return Response.ok(mapper.mapInfoList(items)).build();
}

@ServerExceptionMapper
public RestResponse<RestExceptionDTOV1> exception(Exception ex) {
return exceptionMapper.exception(ex);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.github.onecx.theme.rs.external.v1.mappers;

import java.util.List;

import jakarta.ws.rs.core.Response;

import org.jboss.resteasy.reactive.RestResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.tkit.quarkus.jpa.exceptions.DAOException;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.io.github.onecx.theme.rs.external.v1.model.RestExceptionDTOV1;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Mapper(uses = { OffsetDateTimeMapper.class })
public abstract class ExceptionMapper {

public RestResponse<RestExceptionDTOV1> exception(Exception ex) {
log.error("Processing theme external v1 rest controller error: {}", ex.getMessage());

if (ex instanceof DAOException de) {
return RestResponse.status(Response.Status.BAD_REQUEST,
exception(de.getMessageKey().name(), ex.getMessage(), de.parameters));
}
return RestResponse.status(Response.Status.INTERNAL_SERVER_ERROR,
exception("UNDEFINED_ERROR_CODE", ex.getMessage()));

}

@Mapping(target = "removeParametersItem", ignore = true)
@Mapping(target = "namedParameters", ignore = true)
@Mapping(target = "removeNamedParametersItem", ignore = true)
@Mapping(target = "parameters", ignore = true)
@Mapping(target = "validations", ignore = true)
@Mapping(target = "removeValidationsItem", ignore = true)
public abstract RestExceptionDTOV1 exception(String errorCode, String message);

@Mapping(target = "removeParametersItem", ignore = true)
@Mapping(target = "namedParameters", ignore = true)
@Mapping(target = "removeNamedParametersItem", ignore = true)
@Mapping(target = "validations", ignore = true)
@Mapping(target = "removeValidationsItem", ignore = true)
public abstract RestExceptionDTOV1 exception(String errorCode, String message, List<Object> parameters);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.github.onecx.theme.rs.external.v1.mappers;

import java.util.List;
import java.util.stream.Stream;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.io.github.onecx.theme.rs.external.v1.model.ThemeDTOV1;
import gen.io.github.onecx.theme.rs.external.v1.model.ThemeInfoDTOV1;
import gen.io.github.onecx.theme.rs.external.v1.model.ThemeInfoListDTOV1;
import io.github.onecx.theme.domain.models.Theme;
import io.github.onecx.theme.domain.models.ThemeInfo;

@Mapper(uses = { OffsetDateTimeMapper.class })
public abstract class ThemeMapper {

@Mapping(target = "version", source = "modificationCount")
public abstract ThemeDTOV1 map(Theme theme);

public ThemeInfoListDTOV1 mapInfoList(Stream<ThemeInfo> data) {
var result = new ThemeInfoListDTOV1();
result.setThemes(mapInfo(data));
return result;
}

public abstract List<ThemeInfoDTOV1> mapInfo(Stream<ThemeInfo> page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public Response deleteTheme(String id) {
return Response.noContent().build();
}

@Override
public Response getThemeInfoList() {
var items = dao.findAllInfos();
return Response.ok(mapper.mapInfoList(items)).build();
}

@Override
public Response getThemeById(String id) {
var theme = dao.findById(id);
Expand All @@ -76,9 +82,9 @@ public Response getThemeByThemeDefinitionName(String name) {
}

@Override
public Response getThemes() {
var items = dao.findAll();
return Response.ok(mapper.map(items)).build();
public Response getThemes(Integer pageNumber, Integer pageSize) {
var items = dao.findAll(pageNumber, pageSize);
return Response.ok(mapper.mapPage(items)).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

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

@Mapper(uses = { OffsetDateTimeMapper.class })
public abstract class ThemeMapper {

@Inject
ObjectMapper mapper;

public ThemeInfoListDTO mapInfoList(Stream<ThemeInfo> data) {
ThemeInfoListDTO result = new ThemeInfoListDTO();
result.setThemes(mapInfo(data));
return result;
}

public abstract List<ThemeInfoDTO> mapInfo(Stream<ThemeInfo> page);

@Mapping(target = "removeStreamItem", ignore = true)
public abstract ThemePageResultDTO mapPage(PageResult<Theme> page);

@Mapping(target = "id", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
Expand Down
74 changes: 72 additions & 2 deletions src/main/openapi/onecx-theme-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ paths:
- themesInternal
description: Return list of themes
operationId: getThemes
parameters:
- name: pageNumber
in: query
schema:
format: int32
description: The number of page.
default: 0
type: integer
- name: pageSize
in: query
schema:
format: int32
description: The size of page
default: 10
type: integer
responses:
"200":
description: OK
Expand All @@ -22,7 +37,7 @@ paths:
schema:
type: array
items:
$ref: '#/components/schemas/Theme'
$ref: '#/components/schemas/ThemePageResult'
"403":
description: Forbidden
"500":
Expand Down Expand Up @@ -148,6 +163,27 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/RestException'
/internal/themes/info:
get:
tags:
- themesInternal
description: Get list of all themes
operationId: getThemeInfoList
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/ThemeInfoList'
"404":
description: Not found
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/RestException'
/internal/themes/name/{name}:
get:
tags:
Expand Down Expand Up @@ -175,9 +211,43 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/RestException'
deprecated: true
components:
schemas:
ThemeInfoList:
type: object
properties:
themes:
type: array
items:
$ref: '#/components/schemas/ThemeInfo'
ThemeInfo:
type: object
properties:
name:
minLength: 2
type: string
description:
type: string
ThemePageResult:
type: object
properties:
totalElements:
format: int64
description: The total elements in the resource.
type: integer
number:
format: int32
type: integer
size:
format: int32
type: integer
totalPages:
format: int64
type: integer
stream:
type: array
items:
$ref: '#/components/schemas/Theme'
Theme:
required:
- name
Expand Down
Loading
Loading