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

refactor(rest/rcp/controllers): Extracted a class from the repeated code within the RPC controllers #814

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,12 @@ public DeleteService deleteService(

//RPC
@Bean
public FunctionService functionService(JdbcManager jdbcManager) {
public RpcService functionService(JdbcManager jdbcManager) {
return new JdbcFunctionService(jdbcManager);
}

@Bean
public ProcedureService procedureService(JdbcManager jdbcManager) {
public RpcService procedureService(JdbcManager jdbcManager) {
return new JdbcProcedureService(jdbcManager);
}

Expand Down Expand Up @@ -357,14 +357,14 @@ public DeleteController deleteController(DeleteService deleteService, Db2RestCon

//RPC
@Bean
@ConditionalOnBean(FunctionService.class)
public FunctionController functionController(FunctionService functionService) {
@ConditionalOnBean(RpcService.class)
public FunctionController functionController(RpcService functionService) {
return new FunctionController(functionService);
}

@Bean
@ConditionalOnBean(ProcedureService.class)
public ProcedureController procedureController(ProcedureService procedureService) {
@ConditionalOnBean(RpcService.class)
public ProcedureController procedureController(RpcService procedureService) {
return new ProcedureController(procedureService);
}

Expand Down
24 changes: 24 additions & 0 deletions api-rest/src/main/java/com/homihq/db2rest/jdbc/rest/RpcApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.homihq.db2rest.jdbc.rest;

import com.homihq.db2rest.jdbc.core.service.RpcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;

import java.util.Map;

@Slf4j
public abstract class RpcApi {
private final RpcService service;

protected RpcApi(RpcService service) {
this.service = service;
}

public ResponseEntity<Map<String, Object>> execute(
String dbId,
String funcName,
Map<String, Object> inParams) {
log.debug("Execute function {} with IN params {}", funcName, inParams.entrySet());
return ResponseEntity.ok(service.execute(dbId, funcName, inParams));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.jdbc.rest.rpc;

import com.homihq.db2rest.jdbc.core.service.FunctionService;
import com.homihq.db2rest.jdbc.core.service.RpcService;
import com.homihq.db2rest.jdbc.rest.RpcApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,19 +12,16 @@
@RestController
@RequestMapping(VERSION + "/{dbId}/function")
@Slf4j
@RequiredArgsConstructor
public class FunctionController {

private final FunctionService functionService;
public class FunctionController extends RpcApi {
public FunctionController(RpcService functionService) {
super(functionService);
}

@PostMapping("/{funcName}")
public ResponseEntity<Map<String, Object>> execute(
@PathVariable String dbId,
@PathVariable String funcName,
@RequestBody Map<String,Object> inParams) {

log.debug("Execute function {} with IN params {}", funcName, inParams.entrySet());

return ResponseEntity.ok(functionService.execute(dbId, funcName, inParams));
return super.execute(dbId, funcName, inParams);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.homihq.db2rest.jdbc.rest.rpc;

import com.homihq.db2rest.jdbc.core.service.ProcedureService;
import lombok.RequiredArgsConstructor;
import com.homihq.db2rest.jdbc.core.service.RpcService;
import com.homihq.db2rest.jdbc.rest.RpcApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -11,17 +11,17 @@
@RestController
@RequestMapping(VERSION + "/{dbId}/procedure")
@Slf4j
@RequiredArgsConstructor
public class ProcedureController {

private final ProcedureService procedureService;
public class ProcedureController extends RpcApi {
public ProcedureController(RpcService procedureService) {
super(procedureService);
}

@Override
@PostMapping("/{procName}")
public ResponseEntity<Map<String, Object>> execute(
@PathVariable String dbId,
@PathVariable String procName,
@RequestBody Map<String,Object> inParams) {
log.debug("Execute stored procedure {} with IN params {}", procName, inParams.entrySet());
return ResponseEntity.ok(procedureService.execute(dbId,procName, inParams));
@RequestBody Map<String,Object> inParams) {
return super.execute(dbId, procName, inParams);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

@Slf4j
@RequiredArgsConstructor
public class JdbcFunctionService implements FunctionService {

public class JdbcFunctionService implements RpcService {
private final JdbcManager jdbcManager;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@

@Slf4j
@RequiredArgsConstructor
public class JdbcProcedureService implements ProcedureService {

public class JdbcProcedureService implements RpcService {
private final JdbcManager jdbcManager;

@Override
public SimpleJdbcCall getSimpleJdbcCall(String dbId, String subRoutineName) {
log.info("dbId - {}", dbId);
JdbcTemplate jdbcTemplate = jdbcManager.getNamedParameterJdbcTemplate(dbId).getJdbcTemplate();
return new SimpleJdbcCall(jdbcTemplate).withProcedureName(subRoutineName);
}

@Override
public Map<String, Object> execute(String dbId, String subRoutineName, Map<String, Object> inParams) {
JdbcTemplate jdbcTemplate = jdbcManager.getNamedParameterJdbcTemplate(dbId).getJdbcTemplate();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.Map;

public interface FunctionService extends SubRoutine {
public interface RpcService extends SubRoutine {
SimpleJdbcCall getSimpleJdbcCall(String dbId, String subRoutineName);
Map<String, Object> execute(String dbId, String subRoutineName, Map<String, Object> inParams);
}