Skip to content

Commit

Permalink
feat: refactoring api (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Mar 4, 2024
1 parent 0f9c103 commit 011bfc3
Show file tree
Hide file tree
Showing 24 changed files with 396 additions and 479 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
<inputSpec>src/main/openapi/search-config-openapi-v1.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.search.config.v1</apiPackage>
<modelPackage>gen.org.tkit.onecx.search.config.v1.model</modelPackage>
<apiNameSuffix>V1Api</apiNameSuffix>
<modelNameSuffix>DTOV1</modelNameSuffix>
<skipValidateSpec>true</skipValidateSpec>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
@Getter
@Setter
public class SearchConfigCriteria {

private String configId;
private String productName;
private String name;
private String appId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ public SearchConfig findById(Object id) throws DAOException {
} catch (NoResultException nre) {
return null;
} catch (Exception e) {
throw new DAOException(ErrorKeys.FIND_ENTITY_BY_ID_FAILED, e, entityName, id);
throw new DAOException(ErrorKeys.FIND_SEARCH_CONFIG_BY_ID_FAILED, e, entityName, id);
}
}

public SearchConfig findByConfigId(Object configId) throws DAOException {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(SearchConfig.class);
var root = cq.from(SearchConfig.class);
cq.where(cb.equal(root.get(SearchConfig_.CONFIG_ID), configId));
return this.getEntityManager().createQuery(cq).getSingleResult();
} catch (NoResultException nre) {
return null;
} catch (Exception e) {
throw new DAOException(ErrorKeys.FIND_SEARCH_CONFIG_BY_CONFIG_ID_FAILED, e, entityName, configId);
}
}

Expand All @@ -47,6 +61,8 @@ public PageResult<SearchConfig> findBySearchCriteria(SearchConfigCriteria search
var searchConfigRoot = criteriaQuery.from(SearchConfig.class);

List<Predicate> predicates = new ArrayList<>();
addSearchStringPredicate(predicates, criteriaBuilder, searchConfigRoot.get(SearchConfig_.CONFIG_ID),
searchCriteria.getConfigId());
addSearchStringPredicate(predicates, criteriaBuilder, searchConfigRoot.get(SearchConfig_.PRODUCT_NAME),
searchCriteria.getProductName());
addSearchStringPredicate(predicates, criteriaBuilder, searchConfigRoot.get(SearchConfig_.APP_ID),
Expand All @@ -68,11 +84,10 @@ public PageResult<SearchConfig> findBySearchCriteria(SearchConfigCriteria search
}

public enum ErrorKeys {
/**
* The find search templates by criteria failed.
*/

FIND_SEARCH_CONFIG_BY_CONFIG_ID_FAILED,
FIND_SEARCH_CONFIGS_BY_CRITERIA_FAILED,
FIND_ENTITY_BY_ID_FAILED
FIND_SEARCH_CONFIG_BY_ID_FAILED
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tkit.onecx.search.config.domain.models;

import java.util.UUID;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
Expand All @@ -14,11 +16,15 @@
@Setter
@Entity
@Table(name = "search_config", uniqueConstraints = {
@UniqueConstraint(name = "search_config_id", columnNames = { "config_id" }),
@UniqueConstraint(name = "ck_config", columnNames = { "name", "product_name", "page", "appId", "tenant_id" })
})
@SuppressWarnings("java:S2160")
public class SearchConfig extends TraceableEntity {

@Column(name = "config_id")
private String configId = UUID.randomUUID().toString();

@Column(name = "product_name")
private String productName;
@Column
Expand All @@ -37,8 +43,6 @@ public class SearchConfig extends TraceableEntity {
private boolean advanced;
@Column(name = "columns")
private String columns;
@Column(name = "api_version")
private String apiVersion;
@TenantId
@Column(name = "tenant_id")
private String tenantId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.search.config.rs.internal.controller;
package org.tkit.onecx.search.config.rs.internal.controllers;

import static org.jboss.resteasy.reactive.RestResponse.StatusCode.NOT_FOUND;

Expand All @@ -15,9 +15,8 @@
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.tkit.onecx.search.config.domain.daos.SearchConfigDAO;
import org.tkit.onecx.search.config.domain.models.SearchConfig;
import org.tkit.onecx.search.config.rs.internal.mapper.ExceptionMapper;
import org.tkit.onecx.search.config.rs.internal.mapper.SearchConfigMapper;
import org.tkit.onecx.search.config.rs.internal.mappers.ExceptionMapper;
import org.tkit.onecx.search.config.rs.internal.mappers.SearchConfigMapper;
import org.tkit.quarkus.jpa.exceptions.ConstraintException;

import gen.org.tkit.onecx.search.config.rs.internal.SearchConfigInternalApi;
Expand All @@ -30,58 +29,59 @@
public class SearchConfigControllerInternal implements SearchConfigInternalApi {

@Inject
SearchConfigMapper searchConfigMapper;
SearchConfigMapper mapper;

@Inject
ExceptionMapper exceptionMapper;

@Inject
SearchConfigDAO searchConfigDAO;
SearchConfigDAO dao;

@Context
UriInfo uriInfo;

@Override
public Response createSearchConfig(CreateSearchConfigRequestDTO createSearchConfigRequestDTO) {
var searchConfig = searchConfigMapper.create(createSearchConfigRequestDTO);
var responseSearchConfig = searchConfigDAO.create(searchConfig);
public Response createConfig(CreateSearchConfigRequestDTO createSearchConfigRequestDTO) {
var searchConfig = mapper.create(createSearchConfigRequestDTO);
var responseSearchConfig = dao.create(searchConfig);
return Response
.created(uriInfo.getAbsolutePathBuilder().path(responseSearchConfig.getId()).build())
.entity(searchConfigMapper.map(responseSearchConfig))
.entity(mapper.map(responseSearchConfig))
.build();
}

@Override
public Response deleteSearchConfig(String configId) {
searchConfigDAO.deleteQueryById(configId);
public Response deleteConfig(String id) {
dao.deleteQueryById(id);
return Response.noContent().build();
}

@Override
public Response findBySearchCriteria(SearchConfigSearchRequestDTO searchConfigSearchRequestDTO) {
var results = searchConfigDAO.findBySearchCriteria(searchConfigMapper.map(searchConfigSearchRequestDTO));
return Response.ok().entity(searchConfigMapper.map(results)).build();
var results = dao.findBySearchCriteria(mapper.map(searchConfigSearchRequestDTO));
return Response.ok().entity(mapper.map(results)).build();
}

@Override
public Response getSearchConfigById(String configId) {
var searchConfig = searchConfigDAO.findById(configId);
if (searchConfig != null) {
return Response.ok().entity(searchConfigMapper.map(searchConfig)).build();
public Response getConfigById(String id) {
var searchConfig = dao.findById(id);
if (searchConfig == null) {
return Response.status(NOT_FOUND).build();
}
return Response.status(NOT_FOUND).build();
return Response.ok().entity(mapper.map(searchConfig)).build();
}

@Override
public Response updateSearchConfig(String configId,
public Response updateConfig(String configId,
UpdateSearchConfigRequestDTO updateSearchConfigRequestDTO) {
var searchConfig = searchConfigDAO.findById(configId);
if (searchConfig != null) {
SearchConfig updatedSearchConfig = searchConfigDAO
.update(searchConfigMapper.update(searchConfig, updateSearchConfigRequestDTO));
return Response.ok(searchConfigMapper.map(updatedSearchConfig)).build();
var searchConfig = dao.findById(configId);
if (searchConfig == null) {
return Response.status(NOT_FOUND).build();
}
return Response.status(NOT_FOUND).build();

mapper.update(searchConfig, updateSearchConfigRequestDTO);
var updatedSearchConfig = dao.update(searchConfig);
return Response.ok(mapper.map(updatedSearchConfig)).build();
}

@ServerExceptionMapper
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.search.config.rs.internal.mapper;
package org.tkit.onecx.search.config.rs.internal.mappers;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.tkit.onecx.search.config.rs.internal.mapper;
package org.tkit.onecx.search.config.rs.internal.mappers;

import java.util.HashMap;
import java.util.List;
Expand All @@ -18,7 +18,7 @@
import gen.org.tkit.onecx.search.config.rs.internal.model.*;

@Mapper(uses = OffsetDateTimeMapper.class)
public abstract class SearchConfigMapper {
public interface SearchConfigMapper {

@Mapping(target = "id", ignore = true)
@Mapping(target = "modificationCount", ignore = true)
Expand All @@ -29,9 +29,10 @@ public abstract class SearchConfigMapper {
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "persisted", ignore = true)
public abstract SearchConfig create(CreateSearchConfigRequestDTO dto);
@Mapping(target = "configId", ignore = true)
SearchConfig create(CreateSearchConfigRequestDTO dto);

public String map(Map<String, String> values) throws JsonProcessingException {
default String map(Map<String, String> values) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(values);

Expand All @@ -45,23 +46,23 @@ public String map(Map<String, String> values) throws JsonProcessingException {
@Mapping(target = "tenantId", ignore = true)
@Mapping(target = "controlTraceabilityManual", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "apiVersion", ignore = true)
@Mapping(target = "productName", ignore = true)
public abstract SearchConfig update(@MappingTarget SearchConfig searchConfig, UpdateSearchConfigRequestDTO dto);
@Mapping(target = "configId", ignore = true)
void update(@MappingTarget SearchConfig searchConfig, UpdateSearchConfigRequestDTO dto);

public String map(List<String> value) throws JsonProcessingException {
default String map(List<String> value) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(value);
}

public List<String> map(String value) throws JsonProcessingException {
default List<String> map(String value) throws JsonProcessingException {
List<String> columns;
ObjectMapper mapper = new ObjectMapper();
columns = mapper.readValue(value, List.class);
return columns;
}

public Map<String, String> mapValues(String value) throws JsonProcessingException {
default Map<String, String> mapValues(String value) throws JsonProcessingException {
Map<String, String> values;

ObjectMapper mapper = new ObjectMapper();
Expand All @@ -70,13 +71,13 @@ public Map<String, String> mapValues(String value) throws JsonProcessingExceptio
return values;
}

public abstract SearchConfigCriteria map(SearchConfigSearchRequestDTO configSearchRequestDTO);
SearchConfigCriteria map(SearchConfigSearchRequestDTO configSearchRequestDTO);

@Mapping(target = "removeStreamItem", ignore = true)
public abstract SearchPageResultDTO map(PageResult<SearchConfig> page);
SearchConfigPageResultDTO map(PageResult<SearchConfig> page);

@Mapping(target = "removeColumnsItem", ignore = true)
@Mapping(target = "removeValuesItem", ignore = true)
public abstract SearchConfigDTO map(SearchConfig searchConfig);
SearchConfigDTO map(SearchConfig searchConfig);

}

This file was deleted.

Loading

0 comments on commit 011bfc3

Please sign in to comment.