Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
elunez committed Dec 18, 2019
1 parent 8c7217e commit 554aa58
Show file tree
Hide file tree
Showing 24 changed files with 325 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class GlobalExceptionHandler {
* 处理所有不可知的异常
*/
@ExceptionHandler(Throwable.class)
public ResponseEntity handleException(Throwable e){
public ResponseEntity<ApiError> handleException(Throwable e){
// 打印堆栈信息
log.error(ThrowableUtil.getStackTrace(e));
return buildResponseEntity(ApiError.error(e.getMessage()));
Expand All @@ -37,7 +37,7 @@ public ResponseEntity handleException(Throwable e){
* BadCredentialsException
*/
@ExceptionHandler(BadCredentialsException.class)
public ResponseEntity badCredentialsException(BadCredentialsException e){
public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e){
// 打印堆栈信息
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
log.error(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void generator(GenConfig genConfig, List<ColumnInfo> columns) {
}

@Override
public ResponseEntity preview(GenConfig genConfig, List<ColumnInfo> columns) {
public ResponseEntity<Object> preview(GenConfig genConfig, List<ColumnInfo> columns) {
if(genConfig.getId() == null){
throw new BadRequestException("请先配置生成器");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class ServerMonitorController {
private static final double GB = 1024*1024*1024.00;

@GetMapping
public ResponseEntity getServerInfo(){
public ResponseEntity<Object> getServerInfo(){
Map<String,Object> resultMap = new HashMap<>(8);
try {
CpuInfo[] infoList = sigar.getCpuInfoList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;

/**
* @author zhanghouying
Expand Down Expand Up @@ -46,6 +49,8 @@ public class Database implements Serializable {
@Column(name = "user_name",nullable = false)
private String userName;

@CreationTimestamp
private Timestamp createTime;

public void copy(Database source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
* @author zhanghouying
Expand All @@ -28,37 +31,45 @@ public AppController(AppService appService){
this.appService = appService;
}

@Log("导出应用数据")
@ApiOperation("导出应用数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('app:list')")
public void download(HttpServletResponse response, AppQueryCriteria criteria) throws IOException {
appService.download(appService.queryAll(criteria), response);
}

@Log("查询应用")
@ApiOperation(value = "查询应用")
@GetMapping
@PreAuthorize("@el.check('app:list')")
public ResponseEntity getApps(AppQueryCriteria criteria, Pageable pageable){
public ResponseEntity<Object> getApps(AppQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(appService.queryAll(criteria,pageable),HttpStatus.OK);
}

@Log("新增应用")
@ApiOperation(value = "新增应用")
@PostMapping
@PreAuthorize("@el.check('app:add')")
public ResponseEntity create(@Validated @RequestBody App resources){
public ResponseEntity<Object> create(@Validated @RequestBody App resources){
return new ResponseEntity<>(appService.create(resources),HttpStatus.CREATED);
}

@Log("修改应用")
@ApiOperation(value = "修改应用")
@PutMapping
@PreAuthorize("@el.check('app:edit')")
public ResponseEntity update(@Validated @RequestBody App resources){
public ResponseEntity<Object> update(@Validated @RequestBody App resources){
appService.update(resources);
return new ResponseEntity(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Log("删除应用")
@ApiOperation(value = "删除应用")
@DeleteMapping(value = "/{id}")
@DeleteMapping
@PreAuthorize("@el.check('app:del')")
public ResponseEntity delete(@PathVariable Long id){
appService.delete(id);
return new ResponseEntity(HttpStatus.OK);
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
appService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Set;

/**
* @author zhanghouying
Expand All @@ -38,53 +40,61 @@ public DatabaseController(DatabaseService databaseService) {
this.databaseService = databaseService;
}

@Log("导出数据库数据")
@ApiOperation("导出数据库数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('database:list')")
public void download(HttpServletResponse response, DatabaseQueryCriteria criteria) throws IOException {
databaseService.download(databaseService.queryAll(criteria), response);
}

@Log("查询数据库")
@ApiOperation(value = "查询数据库")
@GetMapping
@PreAuthorize("@el.check('database:list')")
public ResponseEntity getDatabases(DatabaseQueryCriteria criteria, Pageable pageable){
public ResponseEntity<Object> getDatabases(DatabaseQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(databaseService.queryAll(criteria,pageable),HttpStatus.OK);
}

@Log("新增数据库")
@ApiOperation(value = "新增数据库")
@PostMapping
@PreAuthorize("@el.check('database:add')")
public ResponseEntity create(@Validated @RequestBody Database resources){
public ResponseEntity<Object> create(@Validated @RequestBody Database resources){
return new ResponseEntity<>(databaseService.create(resources),HttpStatus.CREATED);
}

@Log("修改数据库")
@ApiOperation(value = "修改数据库")
@PutMapping
@PreAuthorize("@el.check('database:edit')")
public ResponseEntity update(@Validated @RequestBody Database resources){
public ResponseEntity<Object> update(@Validated @RequestBody Database resources){
databaseService.update(resources);
return new ResponseEntity(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Log("删除数据库")
@ApiOperation(value = "删除数据库")
@DeleteMapping(value = "/{id}")
@DeleteMapping
@PreAuthorize("@el.check('database:del')")
public ResponseEntity delete(@PathVariable String id){
databaseService.delete(id);
return new ResponseEntity(HttpStatus.OK);
public ResponseEntity<Object> delete(@RequestBody Set<String> ids){
databaseService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}

@Log("测试数据库链接")
@ApiOperation(value = "测试数据库链接")
@PostMapping("/testConnect")
@PreAuthorize("@el.check('database:testConnect')")
public ResponseEntity testConnect(@Validated @RequestBody Database resources){
public ResponseEntity<Object> testConnect(@Validated @RequestBody Database resources){
return new ResponseEntity<>(databaseService.testConnection(resources),HttpStatus.CREATED);
}

@Log("执行SQL脚本")
@ApiOperation(value = "执行SQL脚本")
@PostMapping(value = "/upload")
@PreAuthorize("@el.check('database:add')")
public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
public ResponseEntity<Object> upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
String id = request.getParameter("id");
DatabaseDto database = databaseService.findById(id);
String fileName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* @author zhanghouying
Expand All @@ -38,45 +41,53 @@ public DeployController(DeployService deployService) {
this.deployService = deployService;
}

@Log("导出部署数据")
@ApiOperation("导出部署数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('database:list')")
public void download(HttpServletResponse response, DeployQueryCriteria criteria) throws IOException {
deployService.download(deployService.queryAll(criteria), response);
}

@Log("查询部署")
@ApiOperation(value = "查询部署")
@GetMapping
@PreAuthorize("@el.check('deploy:list')")
public ResponseEntity getDeploys(DeployQueryCriteria criteria, Pageable pageable){
public ResponseEntity<Object> getDeploys(DeployQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(deployService.queryAll(criteria,pageable),HttpStatus.OK);
}

@Log("新增部署")
@ApiOperation(value = "新增部署")
@PostMapping
@PreAuthorize("@el.check('deploy:add')")
public ResponseEntity create(@Validated @RequestBody Deploy resources){
public ResponseEntity<Object> create(@Validated @RequestBody Deploy resources){
return new ResponseEntity<>(deployService.create(resources),HttpStatus.CREATED);
}

@Log("修改部署")
@ApiOperation(value = "修改部署")
@PutMapping
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity update(@Validated @RequestBody Deploy resources){
public ResponseEntity<Object> update(@Validated @RequestBody Deploy resources){
deployService.update(resources);
return new ResponseEntity(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Log("删除部署")
@ApiOperation(value = "删除部署")
@DeleteMapping(value = "/{id}")
@DeleteMapping
@PreAuthorize("@el.check('deploy:del')")
public ResponseEntity delete(@PathVariable Long id){
deployService.delete(id);
return new ResponseEntity(HttpStatus.OK);
public ResponseEntity<Object> delete(@RequestBody Set<Long> ids){
deployService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}

@Log("上传文件部署")
@ApiOperation(value = "上传文件部署")
@PostMapping(value = "/upload")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
public ResponseEntity<Object> upload(@RequestBody MultipartFile file, HttpServletRequest request)throws Exception{
Long id = Long.valueOf(request.getParameter("id"));
String fileName = "";
if(file != null){
Expand All @@ -99,31 +110,31 @@ public ResponseEntity upload(@RequestBody MultipartFile file, HttpServletRequest
@ApiOperation(value = "系统还原")
@PostMapping(value = "/serverReduction")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity serverReduction(@Validated @RequestBody DeployHistory resources){
public ResponseEntity<Object> serverReduction(@Validated @RequestBody DeployHistory resources){
String result = deployService.serverReduction(resources);
return new ResponseEntity<>(result,HttpStatus.OK);
}
@Log("服务运行状态")
@ApiOperation(value = "服务运行状态")
@PostMapping(value = "/serverStatus")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity serverStatus(@Validated @RequestBody Deploy resources){
public ResponseEntity<Object> serverStatus(@Validated @RequestBody Deploy resources){
String result = deployService.serverStatus(resources);
return new ResponseEntity<>(result,HttpStatus.OK);
}
@Log("启动服务")
@ApiOperation(value = "启动服务")
@PostMapping(value = "/startServer")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity startServer(@Validated @RequestBody Deploy resources){
public ResponseEntity<Object> startServer(@Validated @RequestBody Deploy resources){
String result = deployService.startServer(resources);
return new ResponseEntity<>(result,HttpStatus.OK);
}
@Log("停止服务")
@ApiOperation(value = "停止服务")
@PostMapping(value = "/stopServer")
@PreAuthorize("@el.check('deploy:edit')")
public ResponseEntity stopServer(@Validated @RequestBody Deploy resources){
public ResponseEntity<Object> stopServer(@Validated @RequestBody Deploy resources){
String result = deployService.stopServer(resources);
return new ResponseEntity<>(result,HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.mnt.domain.DeployHistory;
import me.zhengjie.modules.mnt.service.DeployHistoryService;
import me.zhengjie.modules.mnt.service.dto.DeployHistoryQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
* @author zhanghouying
Expand All @@ -29,20 +29,28 @@ public DeployHistoryController(DeployHistoryService deployhistoryService) {
this.deployhistoryService = deployhistoryService;
}

@Log("导出部署历史数据")
@ApiOperation("导出部署历史数据")
@GetMapping(value = "/download")
@PreAuthorize("@el.check('deployHistory:list')")
public void download(HttpServletResponse response, DeployHistoryQueryCriteria criteria) throws IOException {
deployhistoryService.download(deployhistoryService.queryAll(criteria), response);
}

@Log("查询部署历史")
@ApiOperation(value = "查询部署历史")
@GetMapping
@PreAuthorize("@el.check('deployHistory:list')")
public ResponseEntity getDeployHistorys(DeployHistoryQueryCriteria criteria, Pageable pageable){
public ResponseEntity<Object> getDeployHistorys(DeployHistoryQueryCriteria criteria, Pageable pageable){
return new ResponseEntity<>(deployhistoryService.queryAll(criteria,pageable),HttpStatus.OK);
}

@Log("删除DeployHistory")
@ApiOperation(value = "删除部署历史")
@DeleteMapping(value = "/{id}")
@PreAuthorize("hasAnyRole('deployHistory:del')")
public ResponseEntity delete(@PathVariable String id){
deployhistoryService.delete(id);
return new ResponseEntity(HttpStatus.OK);
@DeleteMapping
@PreAuthorize("@el.check('deployHistory:del')")
public ResponseEntity<Object> delete(@RequestBody Set<String> ids){
deployhistoryService.delete(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
}
Loading

0 comments on commit 554aa58

Please sign in to comment.