fix(系统设置): 编辑视图报错

--task=1016128 --user=陈建星 高级搜索-视图增删改查-后端 https://www.tapd.cn/55049933/s/1573254
This commit is contained in:
AgAngle 2024-09-05 16:09:07 +08:00 committed by Craftsman
parent 04eb54b3e6
commit af918239d2
7 changed files with 32 additions and 53 deletions

View File

@ -29,7 +29,7 @@ public enum InternalUserView {
MY_CREATE(() -> {
UserViewDTO userViewDTO = getUserViewDTO("my_create");
CombineCondition condition = new CombineCondition();
condition.setName("create_user");
condition.setName("createUser");
condition.setValue(getCurrentUserValue());
condition.setOperator(CombineCondition.CombineConditionOperator.IN.name());
userViewDTO.setConditions(List.of(condition));
@ -38,7 +38,7 @@ public enum InternalUserView {
MY_REVIEW(() -> {
UserViewDTO userViewDTO = getUserViewDTO("my_review");
CombineCondition condition = new CombineCondition();
condition.setName("review_user");
condition.setName("reviewUser");
condition.setValue(getCurrentUserValue());
condition.setOperator(CombineCondition.CombineConditionOperator.IN.name());
userViewDTO.setConditions(List.of(condition));
@ -50,7 +50,6 @@ public enum InternalUserView {
userViewDTO.setSearchMode(CombineSearch.SearchMode.AND.name());
userViewDTO.setName(name);
userViewDTO.setId(name);
userViewDTO.setInternalViewKey(name.toUpperCase());
return userViewDTO;
}

View File

@ -1,8 +1,8 @@
package io.metersphere.system.controller;
import io.metersphere.system.constants.UserViewType;
import io.metersphere.system.domain.UserView;
import io.metersphere.system.dto.UserViewDTO;
import io.metersphere.system.dto.UserViewListDTO;
import io.metersphere.system.dto.UserViewListGroupedDTO;
import io.metersphere.system.dto.request.UserViewAddRequest;
import io.metersphere.system.dto.request.UserViewUpdateRequest;
@ -29,7 +29,7 @@ public class UserViewController {
@GetMapping("/{viewType}/list")
@Operation(summary = "视图列表")
public List<UserViewListDTO> list(@RequestParam String scopeId, @PathVariable String viewType) {
public List<UserView> list(@RequestParam String scopeId, @PathVariable String viewType) {
UserViewType userViewType = UserViewType.getByValue(viewType);
return userViewService.list(scopeId, userViewType, SessionUtils.getUserId());
}

View File

@ -17,8 +17,6 @@ import java.util.List;
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class UserViewDTO extends UserView {
@Schema(description = "内置视图的 key")
private String internalViewKey;
@Schema(description = "筛选条件")
private List<CombineCondition> conditions;
}

View File

@ -1,15 +0,0 @@
package io.metersphere.system.dto;
import io.metersphere.system.domain.UserView;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* @Author: jianxing
* @CreateTime: 2024-09-02 10:15
*/
@Data
public class UserViewListDTO extends UserView {
@Schema(description = "内置视图的 key")
private String internalViewKey;
}

View File

@ -1,5 +1,6 @@
package io.metersphere.system.dto;
import io.metersphere.system.domain.UserView;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ -12,7 +13,7 @@ import java.util.List;
@Data
public class UserViewListGroupedDTO {
@Schema(description = "系统视图")
private List<UserViewListDTO> internalViews;
private List<UserView> internalViews;
@Schema(description = "自定义视图")
private List<UserViewListDTO> customViews;
private List<UserView> customViews;
}

View File

@ -13,7 +13,6 @@ import io.metersphere.system.domain.UserViewCondition;
import io.metersphere.system.domain.UserViewConditionExample;
import io.metersphere.system.domain.UserViewExample;
import io.metersphere.system.dto.UserViewDTO;
import io.metersphere.system.dto.UserViewListDTO;
import io.metersphere.system.dto.UserViewListGroupedDTO;
import io.metersphere.system.dto.request.UserViewAddRequest;
import io.metersphere.system.dto.request.UserViewUpdateRequest;
@ -50,7 +49,7 @@ public class UserViewService {
public static final Long POS_STEP = 5000L;
public List<UserViewListDTO> list(String scopeId, UserViewType viewType, String userId) {
public List<UserView> list(String scopeId, UserViewType viewType, String userId) {
UserViewListGroupedDTO userViews = groupedList(scopeId, viewType, userId);
userViews.getCustomViews().addAll(userViews.getInternalViews());
return userViews.getCustomViews();
@ -129,16 +128,17 @@ public class UserViewService {
}
}
private void checkUpdateExist(String name, String scopeId, String viewType, String userId) {
private void checkUpdateExist(String name, UserView orginUserView, String userId) {
if (StringUtils.isBlank(name)) {
return;
}
UserViewExample example = new UserViewExample();
example.createCriteria()
.andUserIdEqualTo(userId)
.andScopeIdEqualTo(scopeId)
.andViewTypeEqualTo(viewType)
.andNameEqualTo(name);
.andScopeIdEqualTo(orginUserView.getScopeId())
.andViewTypeEqualTo(orginUserView.getViewType())
.andNameEqualTo(name)
.andIdNotEqualTo(orginUserView.getId());
if (userViewMapper.countByExample(example) > 0) {
throw new MSException(USER_VIEW_EXIST);
}
@ -221,7 +221,7 @@ public class UserViewService {
UserView originUserView = userViewMapper.selectByPrimaryKey(request.getId());
// 校验权限只能修改自己的视图
checkOwner(userId, originUserView);
checkUpdateExist(request.getName(), originUserView.getScopeId(), viewType, userId);
checkUpdateExist(request.getName(), originUserView, userId);
UserView userView = BeanUtils.copyBean(new UserView(), request);
userView.setViewType(viewType);
@ -260,14 +260,14 @@ public class UserViewService {
public UserViewListGroupedDTO groupedList(String scopeId, UserViewType viewType, String userId) {
// 查询系统内置视图
List<UserViewListDTO> internalViews = viewType.getInternalViews().stream().map(userViewEnum -> {
UserViewDTO userView = userViewEnum.getUserView();
UserViewListDTO userViewListDTO = BeanUtils.copyBean(new UserViewListDTO(), userView);
userViewListDTO.setName(translateInternalView(userView.getName()));
userViewListDTO.setViewType(viewType.name());
userViewListDTO.setScopeId(scopeId);
userViewListDTO.setUserId(userId);
return userViewListDTO;
List<UserView> internalViews = viewType.getInternalViews().stream().map(userViewEnum -> {
UserViewDTO userViewDTO = userViewEnum.getUserView();
UserView userView = BeanUtils.copyBean(new UserView(), userViewDTO);
userView.setName(translateInternalView(userViewDTO.getName()));
userView.setViewType(viewType.name());
userView.setScopeId(scopeId);
userView.setUserId(userId);
return userView;
}).collect(Collectors.toList());
// 查询用户自定义视图
@ -276,9 +276,8 @@ public class UserViewService {
.andUserIdEqualTo(userId)
.andScopeIdEqualTo(scopeId)
.andViewTypeEqualTo(viewType.name());
List<UserViewListDTO> customUserViews = userViewMapper.selectByExample(example).stream()
List<UserView> customUserViews = userViewMapper.selectByExample(example).stream()
.sorted(Comparator.comparing(UserView::getPos))
.map(userView -> BeanUtils.copyBean(new UserViewListDTO(), userView))
.collect(Collectors.toList());
UserViewListGroupedDTO groupedDTO = new UserViewListGroupedDTO();

View File

@ -5,8 +5,8 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.constants.InternalUserView;
import io.metersphere.system.constants.UserViewType;
import io.metersphere.system.domain.UserView;
import io.metersphere.system.dto.UserViewDTO;
import io.metersphere.system.dto.UserViewListDTO;
import io.metersphere.system.dto.UserViewListGroupedDTO;
import io.metersphere.system.dto.request.UserViewAddRequest;
import io.metersphere.system.dto.request.UserViewUpdateRequest;
@ -52,24 +52,21 @@ public class UserViewControllerTests extends BaseTest {
public void emptyList() throws Exception {
// @@请求成功
MvcResult mvcResult = this.requestGetWithOkAndReturn(LIST, DEFAULT_PROJECT_ID);
List<UserViewListDTO> result = getResultDataArray(mvcResult, UserViewListDTO.class);
List<UserView> result = getResultDataArray(mvcResult, UserView.class);
Assertions.assertEquals(result.size(), 3);
UserViewListDTO allData = result.get(0);
UserView allData = result.get(0);
Assertions.assertEquals(allData.getName(), "全部数据");
Assertions.assertEquals(allData.getInternalViewKey(), InternalUserView.ALL_DATA.name());
Assertions.assertEquals(allData.getId(), InternalUserView.ALL_DATA.name().toLowerCase());
UserViewListDTO myFollow = result.get(1);
UserView myFollow = result.get(1);
Assertions.assertEquals(myFollow.getName(), "我关注的");
Assertions.assertEquals(myFollow.getInternalViewKey(), InternalUserView.MY_FOLLOW.name());
Assertions.assertEquals(myFollow.getId(), InternalUserView.MY_FOLLOW.name().toLowerCase());
UserViewListDTO myCreate = result.get(2);
UserView myCreate = result.get(2);
Assertions.assertEquals(myCreate.getName(), "我创建的");
Assertions.assertEquals(myCreate.getInternalViewKey(), InternalUserView.MY_CREATE.name());
Assertions.assertEquals(myCreate.getId(), InternalUserView.MY_CREATE.name().toLowerCase());
for (UserViewListDTO item : result) {
for (UserView item : result) {
Assertions.assertEquals(item.getScopeId(), DEFAULT_PROJECT_ID);
Assertions.assertEquals(item.getUserId(), InternalUser.ADMIN.getValue());
Assertions.assertEquals(item.getSearchMode(), CombineSearch.SearchMode.AND.name());
@ -134,12 +131,13 @@ public class UserViewControllerTests extends BaseTest {
@Order(3)
public void update() throws Exception {
UserViewUpdateRequest request = BeanUtils.copyBean(new UserViewUpdateRequest(), addUserViewDTO);
request.setName(UUID.randomUUID().toString());
request.setSearchMode(CombineSearch.SearchMode.OR.name());
request.getConditions().get(0).setName(UUID.randomUUID().toString());
// @@请求成功
MvcResult mvcResult = this.requestPostWithOkAndReturn(DEFAULT_UPDATE, request);
request.setName(UUID.randomUUID().toString());
mvcResult = this.requestPostWithOkAndReturn(DEFAULT_UPDATE, request);
addUserViewDTO = getResultData(mvcResult, UserViewDTO.class);
addUserViewDTO.setConditions(request.getConditions());
UserViewDTO userViewDTO = userViewService.get(addUserViewDTO.getId(), UserViewType.FUNCTIONAL_CASE, InternalUser.ADMIN.getValue());
@ -167,7 +165,6 @@ public class UserViewControllerTests extends BaseTest {
mvcResult = this.requestGetWithOkAndReturn(DEFAULT_GET, InternalUserView.ALL_DATA.name());
resultData = getResultData(mvcResult, UserViewDTO.class);
Assertions.assertEquals(resultData.getName(), "全部数据");
Assertions.assertEquals(resultData.getInternalViewKey(), InternalUserView.ALL_DATA.name());
Assertions.assertEquals(resultData.getId(), InternalUserView.ALL_DATA.name().toLowerCase());
Assertions.assertEquals(resultData.getConditions().size(), 0);
}
@ -179,7 +176,7 @@ public class UserViewControllerTests extends BaseTest {
UserViewListGroupedDTO result = getResultData(mvcResult, UserViewListGroupedDTO.class);
Assertions.assertEquals(result.getInternalViews().size(), 3);
Assertions.assertEquals(result.getCustomViews().size(), 2);
Assertions.assertEquals(result.getCustomViews().get(0), BeanUtils.copyBean(new UserViewListDTO(), addUserViewDTO));
Assertions.assertEquals(result.getCustomViews().get(0), BeanUtils.copyBean(new UserView(), addUserViewDTO));
}
@Test