From b09ffedbac1557c116b783c026a2a2aab9bb6a9f Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Tue, 11 Feb 2020 15:24:24 +0800 Subject: [PATCH] workspace add --- .../controller/WorkspaceController.java | 22 ++++++++++ .../handler/ResultResponseBodyAdvice.java | 42 +++++++++++++++++++ .../io/metersphere/service/UserService.java | 2 + .../metersphere/service/WorkspaceService.java | 34 +++++++++++++++ .../components/settings/Workspace.vue | 37 +++++++++++----- 5 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/controller/WorkspaceController.java create mode 100644 backend/src/main/java/io/metersphere/controller/handler/ResultResponseBodyAdvice.java create mode 100644 backend/src/main/java/io/metersphere/service/WorkspaceService.java diff --git a/backend/src/main/java/io/metersphere/controller/WorkspaceController.java b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java new file mode 100644 index 0000000000..9a8b5aeb12 --- /dev/null +++ b/backend/src/main/java/io/metersphere/controller/WorkspaceController.java @@ -0,0 +1,22 @@ +package io.metersphere.controller; + +import io.metersphere.base.domain.Workspace; +import io.metersphere.service.WorkspaceService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; + +@RequestMapping("workspace") +@RestController +public class WorkspaceController { + @Resource + private WorkspaceService workspaceService; + + @PostMapping("add") + public Workspace insertUser(@RequestBody Workspace workspace) { + return workspaceService.add(workspace); + } +} diff --git a/backend/src/main/java/io/metersphere/controller/handler/ResultResponseBodyAdvice.java b/backend/src/main/java/io/metersphere/controller/handler/ResultResponseBodyAdvice.java new file mode 100644 index 0000000000..59d8fbec87 --- /dev/null +++ b/backend/src/main/java/io/metersphere/controller/handler/ResultResponseBodyAdvice.java @@ -0,0 +1,42 @@ +package io.metersphere.controller.handler; + +import com.alibaba.fastjson.JSON; +import io.metersphere.controller.ResultHolder; +import org.springframework.core.MethodParameter; +import org.springframework.http.MediaType; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.StringHttpMessageConverter; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +/** + * 统一处理返回结果集 + */ +@RestControllerAdvice(value = {"io.metersphere.controller"}) +public class ResultResponseBodyAdvice implements ResponseBodyAdvice { + + @Override + public boolean supports(MethodParameter methodParameter, Class> converterType) { + return MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType) || StringHttpMessageConverter.class.isAssignableFrom(converterType); + } + + @Override + public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class> converterType, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { + // 处理空值 + if (o == null && StringHttpMessageConverter.class.isAssignableFrom(converterType)) { + return null; + } + + if (!(o instanceof ResultHolder)) { + if (o instanceof String) { + return JSON.toJSONString(ResultHolder.success(o)); + } + return ResultHolder.success(o); + } + return o; + } + +} diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index e35885429e..0eb23fa972 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -9,6 +9,7 @@ import io.metersphere.dto.UserDTO; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; @@ -16,6 +17,7 @@ import java.util.List; import java.util.stream.Collectors; @Service +@Transactional(rollbackFor = Exception.class) public class UserService { @Resource diff --git a/backend/src/main/java/io/metersphere/service/WorkspaceService.java b/backend/src/main/java/io/metersphere/service/WorkspaceService.java new file mode 100644 index 0000000000..5af66cf79c --- /dev/null +++ b/backend/src/main/java/io/metersphere/service/WorkspaceService.java @@ -0,0 +1,34 @@ +package io.metersphere.service; + +import io.metersphere.base.domain.Workspace; +import io.metersphere.base.mapper.WorkspaceMapper; +import io.metersphere.commons.MSException; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.util.UUID; + +@Service +@Transactional(rollbackFor = Exception.class) +public class WorkspaceService { + @Resource + private WorkspaceMapper workspaceMapper; + + public Workspace add(Workspace workspace) { + if (StringUtils.isBlank(workspace.getName())) { + MSException.throwException("Workspace name cannot be null."); + } + // TODO 组织ID 暂无 + if (StringUtils.isBlank(workspace.getOrganizationId())) { + workspace.setOrganizationId("root"); + } + long createTime = System.currentTimeMillis(); + workspace.setCreateTime(createTime); + workspace.setUpdateTime(createTime); // 首次 update time + workspace.setId(UUID.randomUUID().toString()); // 设置ID + workspaceMapper.insertSelective(workspace); + return workspace; + } +} diff --git a/frontend/src/performance/components/settings/Workspace.vue b/frontend/src/performance/components/settings/Workspace.vue index 825a8d5e76..b9690d70eb 100644 --- a/frontend/src/performance/components/settings/Workspace.vue +++ b/frontend/src/performance/components/settings/Workspace.vue @@ -25,17 +25,17 @@ - - + + - - + + - 创建 - + 创建 + @@ -49,8 +49,17 @@ methods: { create() { this.createVisible = true; - this.$get("/test/user", function (response) { - window.console.log(response); + }, + submit(formName) { + this.$refs[formName].validate((valid) => { + if (valid) { + this.createVisible = false; + this.$post("/workspace/add", this.form, () => { + this.$message({message: '保存成功', type: 'success'}); + }) + } else { + return false; + } }); }, edit(row) { @@ -78,9 +87,15 @@ enable: "是" }], form: { - name: "", - enable: false - } + // name: "", + // description: "" + }, + rules: { + name: [ + {required: true, message: '请输入工作空间名称', trigger: 'blur'}, + {min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur'} + ] + }, } } }