build: dto demo

This commit is contained in:
CaptainB 2023-05-22 19:39:15 +08:00
parent f7eb9a33c7
commit e6383778d1
9 changed files with 116 additions and 17 deletions

View File

@ -30,10 +30,10 @@ CREATE TABLE IF NOT EXISTS `user`
CREATE TABLE IF NOT EXISTS `user_extend`
(
`user_id` VARCHAR(50) NOT NULL COMMENT '用户ID',
`id` VARCHAR(50) NOT NULL COMMENT '用户ID',
`platform_info` BLOB COMMENT '其他平台对接信息',
`selenium_server` VARCHAR(255) DEFAULT NULL COMMENT 'UI本地调试地址',
PRIMARY KEY (`user_id`)
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_general_ci COMMENT ='用户扩展';

View File

@ -0,0 +1,17 @@
package io.metersphere.sdk.dto;
import io.metersphere.system.domain.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
public class UserDTO extends User {
@ApiModelProperty(name = "其他平台对接信息", required = false, allowableValues = "range[1, 2000]")
private byte[] platformInfo;
@ApiModelProperty(name = "UI本地调试地址", required = false, allowableValues = "range[1, 255]")
private String seleniumServer;
}

View File

@ -0,0 +1,7 @@
package io.metersphere.sdk.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.metersphere.system.domain.UserExtend;
public interface UserExtendMapper extends BaseMapper<UserExtend> {
}

View File

@ -1,7 +1,9 @@
package io.metersphere.sdk.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.system.domain.User;
public interface UserMapper extends BaseMapper<User> {
UserDTO selectById(String id);
}

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.sdk.mapper.UserMapper">
<select id="selectById" resultType="io.metersphere.sdk.dto.UserDTO">
SELECT *
FROM user
LEFT JOIN user_extend ON user.id = user_extend.id
WHERE user.id = #{id}
</select>
</mapper>

View File

@ -5,6 +5,7 @@ import io.metersphere.system.domain.User;
import io.metersphere.sdk.mapper.UserMapper;
import org.springframework.stereotype.Service;
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
public class BaseUserService extends ServiceImpl<UserMapper, User> {
}

View File

@ -1,8 +1,9 @@
package io.metersphere.system.controller;
import io.metersphere.sdk.service.UserService;
import io.metersphere.system.domain.User;
import io.metersphere.sdk.dto.UserDTO;
import io.metersphere.system.service.UserService;
import io.metersphere.validation.groups.Created;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
@ -27,7 +28,7 @@ public class UserController {
}
@PostMapping("/add")
public boolean addUser(@Validated({Created.class}) @RequestBody User user) {
public boolean addUser(@Validated({Created.class}) @RequestBody UserDTO user) {
return userService.save(user);
}
}

View File

@ -0,0 +1,41 @@
package io.metersphere.system.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.metersphere.sdk.mapper.UserExtendMapper;
import io.metersphere.sdk.mapper.UserMapper;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.domain.User;
import io.metersphere.system.domain.UserExtend;
import io.metersphere.sdk.dto.UserDTO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional(rollbackFor = Exception.class)
public class UserService {
@Resource
private UserMapper userMapper;
@Resource
private UserExtendMapper userExtendMapper;
public boolean save(UserDTO entity) {
userMapper.insert(entity);
UserExtend userExtend = new UserExtend();
BeanUtils.copyBean(userExtend, entity);
userExtendMapper.insert(userExtend);
return true;
}
public UserDTO getById(String id) {
return userMapper.selectById(id);
}
public List<User> list() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>(new User());
return userMapper.selectList(userQueryWrapper);
}
}

View File

@ -1,7 +1,7 @@
package io.metersphere.system.controller;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.domain.User;
import io.metersphere.sdk.dto.UserDTO;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -10,8 +10,6 @@ import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.regex.Matcher;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ -32,26 +30,47 @@ public class UserControllerTests {
}
@Test
public void testSelectAll2() throws Exception {
public void testGetUser() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/user/get/admin"))
.andExpect(status().isOk())
// .andExpect(content().contentType(MediaType.APPLICATION_JSON))
// .andExpect(jsonPath("$.person.name").value("Jason"))
.andDo(print());
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.data.id").value("admin"));
}
@Test
public void testAddUser() throws Exception {
User user = new User();
UserDTO user = new UserDTO();
user.setId("admin");
user.setName("admin");
user.setCreateUser("system");
user.setSource("LOCAL");
user.setEmail("bin@fit2cloud.com");
user.setStatus("enabled");
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
user.setSeleniumServer("http://localhost:4444");
mockMvc.perform(MockMvcRequestBuilders.post("/user/add")
.content(JSON.toJSONString(user))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void testAddUserFailed() throws Exception {
UserDTO user = new UserDTO();
user.setId("admin2");
user.setCreateTime(System.currentTimeMillis());
user.setUpdateTime(System.currentTimeMillis());
user.setSeleniumServer("http://localhost:4444");
mockMvc.perform(MockMvcRequestBuilders.post("/user/add")
.content(JSON.toJSONString(user))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.code").value("100400"))
.andDo(print());
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
}