diff --git a/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java b/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java index 6c4de81f8d..b3ca423ec4 100644 --- a/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java +++ b/backend/services/dashboard/src/main/java/io/metersphere/dashboard/service/DashboardService.java @@ -236,13 +236,21 @@ public class DashboardService { public List editLayout(String organizationId, String userId, List layoutDTO) { + UserLayoutExample userLayoutExample = new UserLayoutExample(); + userLayoutExample.createCriteria().andUserIdEqualTo(userId).andOrgIdEqualTo(organizationId); + List userLayouts = userLayoutMapper.selectByExample(userLayoutExample); UserLayout userLayout = new UserLayout(); - userLayout.setId(IDGenerator.nextStr()); userLayout.setUserId(userId); userLayout.setOrgId(organizationId); String configuration = JSON.toJSONString(layoutDTO); userLayout.setConfiguration(configuration.getBytes()); - userLayoutMapper.insert(userLayout); + if (CollectionUtils.isEmpty(userLayouts)) { + userLayout.setId(IDGenerator.nextStr()); + userLayoutMapper.insert(userLayout); + } else { + userLayout.setId(userLayouts.getFirst().getId()); + userLayoutMapper.updateByPrimaryKeyWithBLOBs(userLayout); + } return layoutDTO; } diff --git a/backend/services/dashboard/src/test/java/io/metersphere/dashboard/controller/DashboardFrontPageControllerTests.java b/backend/services/dashboard/src/test/java/io/metersphere/dashboard/controller/DashboardFrontPageControllerTests.java index 3c45bcedf3..935f5e96a9 100644 --- a/backend/services/dashboard/src/test/java/io/metersphere/dashboard/controller/DashboardFrontPageControllerTests.java +++ b/backend/services/dashboard/src/test/java/io/metersphere/dashboard/controller/DashboardFrontPageControllerTests.java @@ -9,7 +9,6 @@ import io.metersphere.dashboard.service.DashboardService; import io.metersphere.sdk.util.JSON; import io.metersphere.system.base.BaseTest; import io.metersphere.system.controller.handler.ResultHolder; -import io.metersphere.system.mapper.UserLayoutMapper; import jakarta.annotation.Resource; import org.junit.jupiter.api.*; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -32,9 +31,6 @@ public class DashboardFrontPageControllerTests extends BaseTest { @Resource private DashboardService dashboardService; - @Resource - private UserLayoutMapper userLayoutMapper; - private static final String EDIT_LAYOUT = "/dashboard/layout/edit/"; private static final String GET_LAYOUT = "/dashboard/layout/get/"; @@ -113,6 +109,22 @@ public class DashboardFrontPageControllerTests extends BaseTest { resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); layoutDTOS = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), LayoutDTO.class); Assertions.assertNotNull(layoutDTOS); + + LayoutDTO layoutDTO2 = new LayoutDTO(); + layoutDTO2.setId(UUID.randomUUID().toString()); + layoutDTO2.setPos(2); + layoutDTO2.setKey(DashboardUserLayoutKeys.ASSOCIATE_CASE_COUNT.toString()); + layoutDTO2.setLabel("关联用例数量"); + layoutDTO2.setProjectIds(new ArrayList<>()); + layoutDTO2.setHandleUsers(new ArrayList<>()); + layoutDTO2.setFullScreen(false); + layoutDTO.add(layoutDTO1); + + mvcResult = this.requestPostWithOkAndReturn(EDIT_LAYOUT+DEFAULT_ORGANIZATION_ID, layoutDTO); + contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); + layoutDTOS = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), LayoutDTO.class); + Assertions.assertNotNull(layoutDTOS); } }