Filter business blocks by page scope
This commit is contained in:
@@ -17,6 +17,7 @@ import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.utils.file.FileUtils;
|
||||
import com.ruoyi.generator.domain.front.FrontProject;
|
||||
import com.ruoyi.generator.domain.front.dto.DatabaseDesignResponse;
|
||||
import com.ruoyi.generator.domain.front.dto.AiGenerationTaskCreateRequest;
|
||||
@@ -25,10 +26,14 @@ import com.ruoyi.generator.domain.front.dto.FrontProjectUpdateRequest;
|
||||
import com.ruoyi.generator.domain.front.dto.GenerateAppBlueprintRequest;
|
||||
import com.ruoyi.generator.domain.front.dto.GenerateBusinessBlueprintRequest;
|
||||
import com.ruoyi.generator.domain.front.dto.GenerateDatabaseRequest;
|
||||
import com.ruoyi.generator.domain.front.dto.FrontendPageDesignRequest;
|
||||
import com.ruoyi.generator.domain.front.dto.PreviewFileRequest;
|
||||
import com.ruoyi.generator.service.front.IAiGenerateService;
|
||||
import com.ruoyi.generator.service.front.IAiGenerationTaskService;
|
||||
import com.ruoyi.generator.service.front.BusinessBlockRegistryService;
|
||||
import com.ruoyi.generator.service.front.FrontendPageDesignService;
|
||||
import com.ruoyi.generator.service.front.IFrontProjectPreviewService;
|
||||
import com.ruoyi.generator.service.front.IFrontProjectRunPreviewService;
|
||||
import com.ruoyi.generator.service.front.IFrontProjectService;
|
||||
|
||||
@RestController
|
||||
@@ -42,7 +47,13 @@ public class FrontProjectController extends BaseController
|
||||
@Autowired
|
||||
private IAiGenerationTaskService aiGenerationTaskService;
|
||||
@Autowired
|
||||
private FrontendPageDesignService frontendPageDesignService;
|
||||
@Autowired
|
||||
private BusinessBlockRegistryService businessBlockRegistryService;
|
||||
@Autowired
|
||||
private IFrontProjectPreviewService frontProjectPreviewService;
|
||||
@Autowired
|
||||
private IFrontProjectRunPreviewService frontProjectRunPreviewService;
|
||||
|
||||
@PostMapping("/create")
|
||||
public AjaxResult create(@RequestBody FrontProjectCreateRequest request)
|
||||
@@ -99,6 +110,66 @@ public class FrontProjectController extends BaseController
|
||||
return AjaxResult.success(frontProjectService.saveDatabase(userId, projectId, request));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectId}/page-designs")
|
||||
public AjaxResult pageDesigns(@PathVariable Long projectId,
|
||||
@RequestParam(defaultValue = "frontend") String scope)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return AjaxResult.success(frontendPageDesignService.listPageDesigns(userId, projectId, scope));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectId}/business-blocks")
|
||||
public AjaxResult businessBlocks(@PathVariable Long projectId,
|
||||
@RequestParam(defaultValue = "frontend") String scope)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
frontProjectService.getProject(userId, projectId);
|
||||
return AjaxResult.success(businessBlockRegistryService.listDefinitions(scope));
|
||||
}
|
||||
|
||||
@PostMapping("/{projectId}/page-designs/init")
|
||||
public AjaxResult initPageDesigns(@PathVariable Long projectId,
|
||||
@RequestParam(defaultValue = "frontend") String scope)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return AjaxResult.success(frontendPageDesignService.initializePageDesigns(userId, projectId, scope));
|
||||
}
|
||||
|
||||
@PostMapping("/{projectId}/page-designs")
|
||||
public AjaxResult createPageDesign(@PathVariable Long projectId,
|
||||
@RequestParam(defaultValue = "frontend") String scope,
|
||||
@RequestBody FrontendPageDesignRequest request)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return AjaxResult.success(frontendPageDesignService.createPageDesign(userId, projectId, request, scope));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectId}/page-designs/{designId}")
|
||||
public AjaxResult pageDesign(@PathVariable Long projectId, @PathVariable Long designId,
|
||||
@RequestParam(defaultValue = "frontend") String scope)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return AjaxResult.success(frontendPageDesignService.getPageDesign(userId, projectId, designId, scope));
|
||||
}
|
||||
|
||||
@PutMapping("/{projectId}/page-designs/{designId}")
|
||||
public AjaxResult savePageDesign(@PathVariable Long projectId, @PathVariable Long designId,
|
||||
@RequestParam(defaultValue = "frontend") String scope,
|
||||
@RequestBody FrontendPageDesignRequest request)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return AjaxResult.success(frontendPageDesignService.updatePageDesign(userId, projectId, designId, request, scope));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{projectId}/page-designs/{designId}")
|
||||
public AjaxResult deletePageDesign(@PathVariable Long projectId, @PathVariable Long designId,
|
||||
@RequestParam(defaultValue = "frontend") String scope)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
frontendPageDesignService.deletePageDesign(userId, projectId, designId, scope);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
@PostMapping("/{projectId}/generate-database")
|
||||
public AjaxResult generateDatabase(@PathVariable Long projectId, @RequestBody GenerateDatabaseRequest request)
|
||||
{
|
||||
@@ -173,9 +244,31 @@ public class FrontProjectController extends BaseController
|
||||
frontProjectPreviewService.getStructure(userId, projectId, "frontend");
|
||||
}
|
||||
frontProjectPreviewService.getStructure(userId, projectId, "admin_frontend");
|
||||
frontProjectPreviewService.getStructure(userId, projectId, "sql");
|
||||
return toAjax(frontProjectPreviewService.markPreviewReady(userId, projectId));
|
||||
}
|
||||
|
||||
@PostMapping("/{projectId}/run-preview")
|
||||
public AjaxResult startRunPreview(@PathVariable Long projectId)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return success(frontProjectRunPreviewService.start(userId, projectId));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectId}/run-preview")
|
||||
public AjaxResult runPreviewStatus(@PathVariable Long projectId)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return success(frontProjectRunPreviewService.status(userId, projectId));
|
||||
}
|
||||
|
||||
@PostMapping("/{projectId}/run-preview/stop")
|
||||
public AjaxResult stopRunPreview(@PathVariable Long projectId)
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
return success(frontProjectRunPreviewService.stop(userId, projectId));
|
||||
}
|
||||
|
||||
@GetMapping("/{projectId}/structure")
|
||||
public AjaxResult structure(@PathVariable Long projectId, @RequestParam String templateType)
|
||||
{
|
||||
@@ -194,13 +287,28 @@ public class FrontProjectController extends BaseController
|
||||
public void download(@PathVariable Long projectId, @RequestParam(required = false) String templateType, HttpServletResponse response) throws IOException
|
||||
{
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
FrontProject project = frontProjectService.getProject(userId, projectId);
|
||||
byte[] data = StringUtils.isNotEmpty(templateType)
|
||||
? frontProjectPreviewService.download(userId, projectId, templateType)
|
||||
: frontProjectPreviewService.downloadAll(userId, projectId);
|
||||
response.reset();
|
||||
response.setHeader("Content-Disposition", "attachment; filename=\"project.zip\"");
|
||||
FileUtils.setAttachmentResponseHeader(response, sourceZipName(project));
|
||||
response.addHeader("Content-Length", "" + data.length);
|
||||
response.setContentType("application/octet-stream; charset=UTF-8");
|
||||
IOUtils.write(data, response.getOutputStream());
|
||||
}
|
||||
|
||||
private String sourceZipName(FrontProject project)
|
||||
{
|
||||
String fallback = project != null && project.getProjectId() != null
|
||||
? "project-" + project.getProjectId()
|
||||
: "easycode-project";
|
||||
String rawName = project == null ? null : project.getProjectName();
|
||||
String safeName = StringUtils.isEmpty(rawName) ? fallback : rawName.trim().replaceAll("[\\\\/:*?\"<>|]", "_");
|
||||
if (StringUtils.isEmpty(safeName))
|
||||
{
|
||||
safeName = fallback;
|
||||
}
|
||||
return safeName + ".zip";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.ruoyi.web.controller.front;
|
||||
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.generator.domain.front.dto.block.BusinessBlockDefinition;
|
||||
import com.ruoyi.generator.service.front.BusinessBlockRegistryService;
|
||||
import com.ruoyi.generator.domain.front.dto.ProjectRunPreviewStatus;
|
||||
import com.ruoyi.generator.service.front.IFrontProjectService;
|
||||
import com.ruoyi.generator.service.front.IFrontProjectRunPreviewService;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class FrontProjectControllerRunPreviewTest
|
||||
{
|
||||
private FrontProjectController controller;
|
||||
|
||||
@Mock
|
||||
private IFrontProjectRunPreviewService frontProjectRunPreviewService;
|
||||
@Mock
|
||||
private BusinessBlockRegistryService businessBlockRegistryService;
|
||||
@Mock
|
||||
private IFrontProjectService frontProjectService;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
MockitoAnnotations.initMocks(this);
|
||||
controller = new FrontProjectController();
|
||||
setField("frontProjectRunPreviewService", frontProjectRunPreviewService);
|
||||
setField("businessBlockRegistryService", businessBlockRegistryService);
|
||||
setField("frontProjectService", frontProjectService);
|
||||
SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(loginUser(), null));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown()
|
||||
{
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startRunPreviewDelegatesToCurrentFrontUser()
|
||||
{
|
||||
ProjectRunPreviewStatus status = status(ProjectRunPreviewStatus.RUNNING);
|
||||
when(frontProjectRunPreviewService.start(7L, 20L)).thenReturn(status);
|
||||
|
||||
AjaxResult result = controller.startRunPreview(20L);
|
||||
|
||||
assertEquals(status, result.get(AjaxResult.DATA_TAG));
|
||||
verify(frontProjectRunPreviewService).start(7L, 20L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getRunPreviewStatusDelegatesToCurrentFrontUser()
|
||||
{
|
||||
ProjectRunPreviewStatus status = status(ProjectRunPreviewStatus.NOT_STARTED);
|
||||
when(frontProjectRunPreviewService.status(7L, 20L)).thenReturn(status);
|
||||
|
||||
AjaxResult result = controller.runPreviewStatus(20L);
|
||||
|
||||
assertEquals(status, result.get(AjaxResult.DATA_TAG));
|
||||
verify(frontProjectRunPreviewService).status(7L, 20L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void stopRunPreviewDelegatesToCurrentFrontUser()
|
||||
{
|
||||
ProjectRunPreviewStatus status = status(ProjectRunPreviewStatus.STOPPED);
|
||||
when(frontProjectRunPreviewService.stop(7L, 20L)).thenReturn(status);
|
||||
|
||||
AjaxResult result = controller.stopRunPreview(20L);
|
||||
|
||||
assertEquals(status, result.get(AjaxResult.DATA_TAG));
|
||||
verify(frontProjectRunPreviewService).stop(7L, 20L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void businessBlocksChecksOwnershipAndFiltersScope()
|
||||
{
|
||||
BusinessBlockDefinition definition = new BusinessBlockDefinition();
|
||||
definition.setCode("admin_line_chart");
|
||||
when(businessBlockRegistryService.listDefinitions("admin"))
|
||||
.thenReturn(Collections.singletonList(definition));
|
||||
|
||||
AjaxResult result = controller.businessBlocks(20L, "admin");
|
||||
|
||||
assertEquals(Collections.singletonList(definition), result.get(AjaxResult.DATA_TAG));
|
||||
verify(frontProjectService).getProject(7L, 20L);
|
||||
verify(businessBlockRegistryService).listDefinitions("admin");
|
||||
}
|
||||
|
||||
private ProjectRunPreviewStatus status(String value)
|
||||
{
|
||||
ProjectRunPreviewStatus status = new ProjectRunPreviewStatus();
|
||||
status.setProjectId(20L);
|
||||
status.setStatus(value);
|
||||
return status;
|
||||
}
|
||||
|
||||
private LoginUser loginUser()
|
||||
{
|
||||
SysUser user = new SysUser();
|
||||
user.setUserName("front");
|
||||
user.setPassword("password");
|
||||
LoginUser loginUser = new LoginUser(7L, 1L, user, Collections.<String>emptySet());
|
||||
loginUser.setLoginType("front");
|
||||
return loginUser;
|
||||
}
|
||||
|
||||
private void setField(String name, Object value) throws Exception
|
||||
{
|
||||
Field field = FrontProjectController.class.getDeclaredField(name);
|
||||
field.setAccessible(true);
|
||||
field.set(controller, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user