workspace add

This commit is contained in:
Captain.B 2020-02-11 15:24:24 +08:00
parent f51ddf2039
commit b09ffedbac
5 changed files with 126 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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<Object> {
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> converterType) {
return MappingJackson2HttpMessageConverter.class.isAssignableFrom(converterType) || StringHttpMessageConverter.class.isAssignableFrom(converterType);
}
@Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> 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;
}
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -25,16 +25,16 @@
</el-card>
<ms-create-box :tips="btnTips" :exec="create"/>
<el-dialog title="创建工作空间" :visible.sync="createVisible" width="30%">
<el-form :model="form" label-position="left" label-width="100px" size="small">
<el-form-item label="名称">
<el-form :model="form" :rules="rules" ref="form" label-position="left" label-width="100px" size="small">
<el-form-item label="名称" prop="name">
<el-input v-model="form.name" autocomplete="off"/>
</el-form-item>
<el-form-item label="启用">
<el-switch v-model="form.enable"/>
<el-form-item label="描述">
<el-input type="textarea" v-model="form.description"></el-input>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="createVisible = false" size="medium">创建</el-button>
<el-button type="primary" @click="submit('form')" size="medium">创建</el-button>
</span>
</el-dialog>
</div>
@ -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'}
]
},
}
}
}