-
Notifications
You must be signed in to change notification settings - Fork 55
fix: Modify block create and update api #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThis pull request introduces a comprehensive refactoring of the block-related components in the application. The primary changes involve replacing Changes
Sequence DiagramsequenceDiagram
participant Controller as BlockController
participant Service as BlockService
participant Repository as BlockRepository
Controller->>Service: createBlock(BlockParam)
Service->>Repository: Save block data
Repository-->>Service: Return saved block
Service-->>Controller: Return BlockDto
Possibly related PRs
Suggested reviewers
Poem
✨ Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 5
🔭 Outside diff range comments (9)
base/src/main/java/com/tinyengine/it/controller/BlockController.java (2)
Line range hint
154-171
: Add input validation for BlockParam fields.While the method signature has been updated correctly, consider adding validation annotations on the BlockParam fields to ensure data integrity.
Line range hint
382-403
: Consider adding transaction boundary.The update operation involves multiple database operations (updating block and group associations). Consider adding transaction boundaries to ensure data consistency.
+ @Transactional public Result<BlockDto> updateBlocks(@Valid @RequestBody BlockParam blockParam, @PathVariable Integer id, @RequestParam(value = "appId", required = false) Integer appId) { blockParam.setId(id); return blockService.updateBlockById(blockParam, appId); }
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java (7)
Line range hint
154-204
: Add null checks and validation.The method should validate the BlockParam object before processing to prevent NullPointerException.
public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) { + if (blockParam == null || blockParam.getId() == null) { + return Result.failed(ExceptionEnum.CM001); + } Block blockResult = blockMapper.queryBlockById(blockParam.getId());
Line range hint
210-239
: Add error handling for group creation failure.The method should handle potential failures when creating block group associations.
if (groups != null && !groups.isEmpty()) { Integer groupId = groups.get(0); BlockGroupBlock blockGroupBlock = new BlockGroupBlock(); blockGroupBlock.setBlockGroupId(groupId); blockGroupBlock.setBlockId(id); - blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock); + int groupResult = blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock); + if (groupResult < 1) { + return Result.failed(ExceptionEnum.CM001); + } }
Line range hint
154-203
: Transaction management missing.The method performs multiple database operations without transaction management.
Add transaction management to ensure data consistency:
+ @Transactional @Override public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) { Block blockResult = blockMapper.queryBlockById(blockParam.getId());
Also, the method has potential null pointer issues:
Add null checks:
public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) { + if (blockParam == null || blockParam.getId() == null) { + return Result.failed(ExceptionEnum.CM001); + } Block blockResult = blockMapper.queryBlockById(blockParam.getId()); + if (blockResult == null) { + return Result.failed(ExceptionEnum.CM001); + }
Line range hint
210-239
: Hardcoded values and missing validation.The method contains hardcoded values and lacks input validation.
Add validation and extract constants:
+ private static final int DEFAULT_PLATFORM_ID = 1; + private static final String DEFAULT_USER_ID = "1"; + @Override + @Transactional public Result<BlockDto> createBlock(BlockParam blockParam) { + if (blockParam == null) { + return Result.failed(ExceptionEnum.CM001); + } Block blocks = new Block(); if (blockParam.getOccupier() != null) { blocks.setOccupierBy(String.valueOf(blockParam.getOccupier().getId())); } BeanUtils.copyProperties(blockParam, blocks); blocks.setIsDefault(false); blocks.setIsOfficial(false); - blocks.setPlatformId(1); // 新建区块给默认值 + blocks.setPlatformId(DEFAULT_PLATFORM_ID);
Line range hint
505-543
: Error handling needs improvement.The deploy method lacks proper error handling for edge cases.
Add error handling:
public Result<BlockDto> deploy(BlockBuildDto blockBuildDto) { + if (blockBuildDto == null || blockBuildDto.getBlock() == null) { + return Result.failed(ExceptionEnum.CM001); + } Map<String, Object> content = blockBuildDto.getBlock().getContent(); if (content.isEmpty()) { return Result.failed(ExceptionEnum.CM204); } + try { BlockParam blockParam = blockBuildDto.getBlock(); List<I18nEntryDto> i18nList = i18nEntryMapper.findI18nEntriesByHostandHostType(id, "block"); // ... rest of the method return updateBlockById(blockParam, blockParam.getAppId()); + } catch (Exception e) { + log.error("Failed to deploy block: {}", e.getMessage(), e); + return Result.failed(ExceptionEnum.CM008); + } }
Line range hint
154-204
: Implement transaction management for block updates.The method performs multiple database operations:
- Updating block data
- Managing block group associations
These operations should be wrapped in a transaction to maintain data consistency.
Line range hint
210-239
: Add null checks and validation.The method should validate:
- Required fields in BlockParam
- Business rules for block creation
- Proper error handling for database operations
🧹 Nitpick comments (15)
.github/workflows/checkstyle.yml (1)
28-30
: Optimize the Maven command for Checkstyle.The current
mvn clean install
command runs the full build cycle including tests, which is more than needed for style checking. Consider using a more focused command.- mvn clean install + mvn checkstyle:checkbase/src/main/java/com/tinyengine/it/model/dto/BlockParam.java (2)
152-155
: Consider adding validation for publicStatus values.The
getPublic()
method returnspublicStatus
directly. Since the field is documented to accept values 0,1,2, consider adding validation.@JsonProperty("public") public Integer getPublic() { + if (this.publicStatus != null && !Arrays.asList(0, 1, 2).contains(this.publicStatus)) { + throw new IllegalStateException("Invalid public status value: " + this.publicStatus); + } return this.publicStatus; }
1-157
: Document the relationship between BlockDto and BlockParam.Since this is a new class that seems to be replacing BlockDto in some contexts, consider adding documentation to explain:
- The purpose of having both DTOs
- When to use each one
- How they relate to each other
Add the following to the class-level documentation:
/** * <p> * 区块param * </p> * + * This class serves as a parameter object for block creation and updates, + * working in conjunction with {@link BlockDto}. While BlockDto represents + * the full block data model, BlockParam is specifically designed for + * handling input parameters during block operations. * * @author lu-yg * @since 2024-01-27 */base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java (4)
184-191
: Consider adding more test scenarios.While the basic test case is correct, consider adding test scenarios for:
- Invalid block parameters
- Missing required fields
- Edge cases with different group configurations
101-106
: Test needs additional assertions.The test only verifies the returned data but doesn't validate if the BlockParam properties were correctly passed to the service layer.
Add assertions to verify the BlockParam properties:
@Test void testCreateBlocks() { BlockParam mockParam = new BlockParam(); + mockParam.setName("Test Block"); + mockParam.setLabel("test-block"); BlockDto mockData = new BlockDto(); + mockData.setName("Test Block"); + mockData.setLabel("test-block"); when(blockService.createBlock(any(BlockParam.class))).thenReturn(Result.success(mockData)); Result<BlockDto> result = blockController.createBlocks(mockParam); Assertions.assertEquals(mockData, result.getData()); + verify(blockService).createBlock(argThat(param -> + param.getName().equals("Test Block") && + param.getLabel().equals("test-block") + )); }
184-191
: Test needs additional assertions and error case coverage.The test only covers the happy path but doesn't verify error scenarios or parameter validation.
Add test cases for error scenarios:
@Test void testUpdateBlocks() { BlockParam blockParam = new BlockParam(); + blockParam.setName("Updated Block"); BlockDto returnData = new BlockDto(); + returnData.setName("Updated Block"); when(blockService.updateBlockById(any(BlockParam.class), anyInt())).thenReturn(Result.success(returnData)); when(blockService.queryBlockById(anyInt())).thenReturn(returnData); Result<BlockDto> result = blockController.updateBlocks(blockParam, Integer.valueOf(0), Integer.valueOf(1)); Assertions.assertEquals(returnData, result.getData()); + verify(blockService).updateBlockById(argThat(param -> + param.getName().equals("Updated Block") && + param.getId().equals(0) + ), eq(1)); } +@Test +void testUpdateBlocksWithInvalidId() { + BlockParam blockParam = new BlockParam(); + when(blockService.updateBlockById(any(BlockParam.class), anyInt())) + .thenReturn(Result.failed(ExceptionEnum.CM007)); + + Result<BlockDto> result = blockController.updateBlocks(blockParam, -1, 1); + Assertions.assertTrue(result.isFailed()); + Assertions.assertEquals(ExceptionEnum.CM007.getCode(), result.getCode()); +}
184-191
: Verify test coverage for error scenarios.While the happy path is tested, consider adding test cases for:
- Invalid block parameters
- Non-existent block ID
- Permission denied scenarios
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java (4)
Line range hint
505-543
: Consider extracting build info creation to a separate method.The build info creation logic can be extracted to improve readability and reusability.
+ private Map<String, Object> createBuildInfo(String version, LocalDateTime buildTime) { + Map<String, Object> buildInfo = new HashMap<>(); + buildInfo.put("result", true); + buildInfo.put("versions", Arrays.asList(version)); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + buildInfo.put("endTime", buildTime.format(formatter)); + return buildInfo; + }
669-685
: Add logging for block creation.Consider adding debug logging to track block creation and updates.
public int ensureBlockId(BlockParam blockParam) { + log.debug("Ensuring block ID for label: {}", blockParam.getLabel()); if (blockParam.getId() != null) { + log.debug("Block ID already exists: {}", blockParam.getId()); return blockParam.getId(); }
Line range hint
505-543
: Consider extracting deployment logic.The deploy method handles multiple responsibilities:
- Block creation/update
- History tracking
- I18n handling
- Build info management
Consider extracting these into separate methods for better maintainability.
669-685
: Add logging for block creation events.Consider adding debug logs to track:
- Block creation attempts
- Existing block lookups
- Creation success/failure
base/src/main/java/com/tinyengine/it/controller/BlockController.java (2)
Line range hint
154-171
: API documentation needs improvement.The method documentation lacks details about the BlockParam structure and validation requirements.
Add detailed documentation:
/** * 创建block * - * @param blockParam the blockParam + * @param blockParam The block parameters containing: + * - name: Block name (required) + * - label: Unique identifier (required) + * - framework: Framework type (required) + * - groups: List of group IDs (optional) + * - content: Block content (optional) + * - screenshot: Block screenshot (optional) * @return BlockDto with created block details + * @throws ValidationException if required fields are missing */
Line range hint
154-171
: Update API documentation to reflect parameter changes.The Swagger documentation should include:
- Detailed description of required fields in BlockParam
- Example request payload
- Possible error responses
base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java (2)
118-121
: Add validation for required fields.The test initializes an empty
BlockParam
object. Consider adding test cases that validate the required fields and their constraints.
126-134
: Enhance test coverage for block update scenarios.The test should verify:
- Block group associations are correctly updated
- History tracking is maintained
- Proper error handling when block doesn't exist
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (11)
.github/workflows/checkstyle.yml
(1 hunks).github/workflows/maven.yml
(0 hunks)app/src/main/resources/sql/mysql/create_all_tables_ddl_v1.mysql.sql
(0 hunks)base/src/main/java/com/tinyengine/it/controller/BlockController.java
(6 hunks)base/src/main/java/com/tinyengine/it/model/dto/BlockBuildDto.java
(1 hunks)base/src/main/java/com/tinyengine/it/model/dto/BlockDto.java
(2 hunks)base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java
(1 hunks)base/src/main/java/com/tinyengine/it/service/material/BlockService.java
(2 hunks)base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java
(9 hunks)base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java
(3 hunks)base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java
(2 hunks)
💤 Files with no reviewable changes (2)
- app/src/main/resources/sql/mysql/create_all_tables_ddl_v1.mysql.sql
- .github/workflows/maven.yml
👮 Files not reviewed due to content moderation or server errors (4)
- base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java
- base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java
- base/src/main/java/com/tinyengine/it/controller/BlockController.java
- base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java
🔇 Additional comments (11)
.github/workflows/checkstyle.yml (2)
25-25
: LGTM! Good practice specifying the JDK distribution.Explicitly setting the distribution to 'temurin' improves reproducibility.
37-37
: Verify the Checkstyle report path.Let's verify if the Checkstyle report is generated at the specified path.
✅ Verification successful
The Checkstyle report path is correctly configured
The path
target/checkstyle-result.xml
is the default output location for Maven Checkstyle plugin when using thecheck
goal, and the configuration in your pom.xml follows this convention without any custom output path overrides.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Check Maven's default Checkstyle report location # and any custom configurations in pom.xml # Check if pom.xml specifies a custom output directory for Checkstyle echo "Checking for custom Checkstyle configuration in pom.xml..." rg -A 5 '<plugin>.*checkstyle' # Look for any existing Checkstyle XML configurations echo "Looking for Checkstyle configuration files..." fd -e xml -e properties checkstyleLength of output: 335
Script:
#!/bin/bash # Check Maven Checkstyle plugin configuration in pom.xml echo "Checking Maven Checkstyle plugin configuration..." rg -A 10 'maven-checkstyle-plugin' # Also check if there's a parent pom that might define the configuration echo "Checking parent pom references..." rg '<parent>' -A 5Length of output: 1605
base/src/main/java/com/tinyengine/it/model/dto/BlockBuildDto.java (1)
26-26
: Verify the impact of changing BlockDto to BlockParam.The field type change from
BlockDto
toBlockParam
might affect existing code that creates or processesBlockBuildDto
instances.✅ Verification successful
Type change from BlockDto to BlockParam is correct and intentional
The change aligns with the codebase's pattern where BlockParam is consistently used for block modifications while BlockDto is used for querying. The BlockBuildDto.block field is correctly typed as BlockParam since it's used in the deploy() method for block updates.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for BlockBuildDto usage to verify compatibility echo "Searching for BlockBuildDto instantiations and field access..." rg -l "new BlockBuildDto|getBlock\(\)|setBlock\(" # Search for any test files that might need updates echo "Searching for related test files..." fd -g "*Test.java" --exec rg -l "BlockBuildDto"Length of output: 396
Script:
#!/bin/bash # Examine the implementation file echo "Content of BlockServiceImpl.java:" cat base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java echo -e "\nSearching for BlockDto and BlockParam definitions:" rg -l "class BlockDto|class BlockParam" echo -e "\nChecking for inheritance relationships:" ast-grep --pattern 'class BlockParam extends $_' ast-grep --pattern 'class BlockDto extends $_'Length of output: 26340
base/src/main/java/com/tinyengine/it/service/material/BlockService.java (2)
78-81
: Verify that all implementations of createBlock are updated.The parameter type change from
BlockDto
toBlockParam
requires updates to all implementing classes.✅ Verification successful
All implementations are correctly updated ✅
The only implementation
BlockServiceImpl
is already using the newBlockParam
parameter type, matching the interface change. No other implementations were found in the codebase.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for implementations of createBlock ast-grep --pattern 'class $_ implements BlockService { $$$ Result<BlockDto> createBlock($_) { $$$ } $$$ }'Length of output: 120
Script:
#!/bin/bash # Find classes implementing BlockService echo "=== Classes implementing BlockService ===" rg "class\s+\w+\s+implements\s+.*BlockService" -l echo -e "\n=== createBlock implementations with BlockDto parameter ===" rg "createBlock\s*\(\s*BlockDto\s+" -A 2 echo -e "\n=== createBlock implementations with BlockParam parameter ===" rg "createBlock\s*\(\s*BlockParam\s+" -A 2Length of output: 1263
70-73
: Verify that all implementations of updateBlockById are updated.The parameter type change from
BlockDto
toBlockParam
requires updates to all implementing classes.✅ Verification successful
Implementation is properly updated to match the interface change
The implementation in BlockServiceImpl.java correctly uses the new parameter types (BlockParam and Integer appId) and return type (Result).
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for implementations of updateBlockById ast-grep --pattern 'class $_ implements BlockService { $$$ Result<BlockDto> updateBlockById($_, $_) { $$$ } $$$ }'Length of output: 128
Script:
#!/bin/bash # Find files containing classes that implement BlockService rg "implements\s+BlockService" -l # Find updateBlockById method implementations ast-grep --pattern 'updateBlockById($$$) { $$$ }'Length of output: 170
Script:
#!/bin/bash # View the implementation file cat base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.javaLength of output: 25719
base/src/main/java/com/tinyengine/it/model/dto/BlockDto.java (1)
137-137
: Type safety improvement: List to List.The change improves type safety by using a specific type instead of Object. However, verify that all existing data is compatible with BlockGroup.
✅ Verification successful
Type safety change is correct and safe
The change from
List<Object>
toList<BlockGroup>
is verified to be safe. The codebase consistently uses BlockGroup objects in this context, with proper service layer handling for conversions between IDs and full BlockGroup objects. The existing type checks and usage patterns confirm BlockGroup was already the intended type.🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for places where groups field is accessed or modified rg -A 2 "getGroups\(\)|setGroups\(" # Search for BlockGroup usage to understand the expected structure ast-grep --pattern 'class BlockGroup { $$$ }'Length of output: 2523
Script:
#!/bin/bash # Search for BlockGroup class definition ast-grep --pattern 'class BlockGroup { $$$ }' # Search for BlockParam class definition ast-grep --pattern 'class BlockParam { $$$ }' # Search for any conversion between Integer and BlockGroup rg -A 3 "new BlockGroup|BlockGroup\("Length of output: 16446
base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java (1)
133-135
: Verify the type difference in groups field between BlockDto and BlockParam.BlockDto uses
List<BlockGroup>
while BlockParam usesList<Integer>
. This difference might cause issues during conversion between the two types.✅ Verification successful
Type difference between BlockDto and BlockParam is intentional and properly handled
The difference in types is a common pattern where:
- BlockParam uses
List<Integer>
to accept group IDs as input- BlockDto uses
List<BlockGroup>
to return full group objects- BlockServiceImpl handles the conversion by fetching full BlockGroup objects using the IDs
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for places where conversion between BlockDto and BlockParam occurs rg -A 5 "new BlockDto|new BlockParam" # Search for places where groups field is accessed ast-grep --pattern '$_.getGroups()'Length of output: 10180
Script:
#!/bin/bash # Find BlockDto class definition ast-grep --pattern 'class BlockDto { $$$ }' # Search for conversion methods between BlockDto and BlockParam rg -A 10 "BlockDto.*BlockParam|BlockParam.*BlockDto" # Search for BlockGroup usage in BlockDto rg -A 3 "List<BlockGroup>.*groups"Length of output: 10313
base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java (2)
101-106
: LGTM! Test setup correctly reflects the API changes.The test case has been properly updated to use
BlockParam
instead ofBlockDto
, maintaining consistency with the controller's method signature changes.
101-106
: LGTM! Test setup correctly reflects the API changes.The test correctly uses the new
BlockParam
class and properly mocks the service layer response.base/src/main/java/com/tinyengine/it/controller/BlockController.java (2)
Line range hint
382-403
: Method needs input validation.The method accepts nullable appId but doesn't document the behavior when appId is null.
Add validation and improve documentation:
/** * 修改block * - * @param blockParam blockParam - * @param id id + * @param blockParam Block parameters to update + * @param id Block ID to update + * @param appId Application ID (optional) - If null, the block's existing appId will be used * @return block dto + * @throws ValidationException if block ID doesn't exist + * @throws SecurityException if user doesn't have permission to update the block */ @Operation(summary = "修改区块", description = "修改区块", parameters = { @Parameter(name = "blockParam", description = "入参对象"), @Parameter(name = "id", description = "区块id") },Also, verify that the block exists before updating:
Line range hint
382-403
: Consider adding request validation.The endpoint should validate:
- Block existence before update
- User permissions
- Business rules for block updates
base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java
Outdated
Show resolved
Hide resolved
base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java
Show resolved
Hide resolved
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
♻️ Duplicate comments (3)
base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java (2)
104-116
: 🛠️ Refactor suggestionAdd more test cases for block creation.
The test only verifies basic properties. Consider adding test cases for validation and error scenarios.
Enhance the test coverage:
@Test void testCreateBlocks() { BlockParam mockParam = new BlockParam(); mockParam.setName("Test Block"); mockParam.setLabel("test-block"); + mockParam.setFramework("vue"); + mockParam.setDescription("Test Description"); + mockParam.setIsOfficial(false); + mockParam.setIsDefault(false); + mockParam.setGroups(Arrays.asList(1, 2)); + BlockDto mockData = new BlockDto(); mockData.setName("Test Block"); mockData.setLabel("test-block"); + mockData.setFramework("vue"); + mockData.setDescription("Test Description"); when(blockService.createBlock(any(BlockParam.class))).thenReturn(Result.success(mockData)); Result<BlockDto> result = blockController.createBlocks(mockParam); Assertions.assertEquals(mockData, result.getData()); verify(blockService).createBlock(argThat(param -> param.getName().equals("Test Block") && param.getLabel().equals("test-block") && + param.getFramework().equals("vue") && + param.getDescription().equals("Test Description") && + !param.getIsOfficial() && + !param.getIsDefault() && + param.getGroups().containsAll(Arrays.asList(1, 2)) )); + + // Test validation failure + BlockParam invalidParam = new BlockParam(); + when(blockService.createBlock(invalidParam)).thenReturn(Result.failed("Validation failed")); + Result<BlockDto> invalidResult = blockController.createBlocks(invalidParam); + Assertions.assertFalse(invalidResult.isSuccess()); }
195-207
: 🛠️ Refactor suggestionAdd test cases for block update validation.
The test should verify error handling and validation during block updates.
Enhance the test coverage:
@Test void testUpdateBlocks() { BlockParam blockParam = new BlockParam(); blockParam.setName("Updated Block"); + blockParam.setId(0); + blockParam.setLabel("updated-block"); + blockParam.setFramework("vue"); + blockParam.setGroups(Arrays.asList(1, 2)); + BlockDto returnData = new BlockDto(); returnData.setName("Updated Block"); when(blockService.updateBlockById(any(BlockParam.class), anyInt())).thenReturn(Result.success(returnData)); when(blockService.queryBlockById(anyInt())).thenReturn(returnData); Result<BlockDto> result = blockController.updateBlocks(blockParam, Integer.valueOf(0), Integer.valueOf(1)); Assertions.assertEquals(returnData, result.getData()); verify(blockService).updateBlockById(argThat(param -> param.getName().equals("Updated Block") && - param.getId().equals(0) + param.getId().equals(0) && + param.getLabel().equals("updated-block") && + param.getFramework().equals("vue") && + param.getGroups().containsAll(Arrays.asList(1, 2)) ), eq(1)); + + // Test null ID validation + BlockParam invalidParam = new BlockParam(); + when(blockService.updateBlockById(invalidParam, 1)).thenReturn(Result.failed("Invalid ID")); + Result<BlockDto> invalidResult = blockController.updateBlocks(invalidParam, null, 1); + Assertions.assertFalse(invalidResult.isSuccess()); }base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java (1)
118-129
: 🛠️ Refactor suggestionAdd test cases for block creation validation.
The test should verify validation of required fields and error scenarios.
Enhance the test coverage:
@Test void testCreateBlock() { when(blockMapper.createBlock(any())).thenReturn(1); BlockDto t = new BlockDto(); t.setName("test"); when(blockMapper.findBlockAndGroupAndHistoByBlockId(any())).thenReturn(t); BlockParam blockParam = new BlockParam(); blockParam.setId(0); blockParam.setName("test"); blockParam.setLabel("test-label"); blockParam.setFramework("vue"); + blockParam.setDescription("Test Description"); + blockParam.setIsOfficial(false); + blockParam.setIsDefault(false); + blockParam.setGroups(Arrays.asList(1, 2)); + Result<BlockDto> result = blockServiceImpl.createBlock(blockParam); Assertions.assertEquals("test", result.getData().getName()); + verify(blockMapper).createBlock(argThat(block -> + block.getName().equals("test") && + block.getLabel().equals("test-label") && + block.getFramework().equals("vue") && + block.getDescription().equals("Test Description") && + !block.getIsOfficial() && + !block.getIsDefault() + )); // Test validation failure BlockParam invalidParam = new BlockParam(); Result<BlockDto> invalidResult = blockServiceImpl.createBlock(invalidParam); Assertions.assertFalse(invalidResult.isSuccess()); + + // Test duplicate label validation + when(blockMapper.queryBlockByCondition(any())).thenReturn(Arrays.asList(new Block())); + Result<BlockDto> duplicateResult = blockServiceImpl.createBlock(blockParam); + Assertions.assertFalse(duplicateResult.isSuccess()); }
🧹 Nitpick comments (4)
base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java (1)
157-162
: Enhance error handling in getPublic() method.The method could be improved with more descriptive error messages and null handling.
Consider this enhancement:
@JsonProperty("public") public Integer getPublic() { + if (this.publicStatus == null) { + return null; + } if (this.publicStatus != null && !Arrays.asList(0, 1, 2).contains(this.publicStatus)) { - throw new IllegalStateException("Invalid public status value: " + this.publicStatus); + throw new IllegalStateException( + String.format("Invalid public status value: %d. Expected values are: 0 (private), 1 (public), 2 (tenant)", + this.publicStatus) + ); } return this.publicStatus; }base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java (3)
96-97
: Consider making constants configurable.Hard-coded values should be moved to configuration.
Move these constants to configuration:
- private static final int DEFAULT_PLATFORM_ID = 1; - private static final String DEFAULT_USER_ID = "1"; + @Value("${block.default.platform-id}") + private int defaultPlatformId; + @Value("${block.default.user-id}") + private String defaultUserId;
158-165
: Add logging for error scenarios.The error handling could be improved with proper logging.
Add logging statements:
public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) { if (blockParam == null || blockParam.getId() == null) { + log.error("Invalid block parameter: blockParam={}, appId={}", blockParam, appId); return Result.failed(ExceptionEnum.CM002); } Block blockResult = blockMapper.queryBlockById(blockParam.getId()); if (blockResult == null) { + log.error("Block not found: id={}", blockParam.getId()); return Result.failed(ExceptionEnum.CM001); }
725-732
: Add input validation to createBuildInfo method.The method should validate its input parameters.
Add validation:
private Map<String, Object> createBuildInfo(String version, LocalDateTime buildTime) { + if (version == null || buildTime == null) { + throw new IllegalArgumentException("Version and buildTime cannot be null"); + } + if (!version.matches("^\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9]+)?$")) { + throw new IllegalArgumentException("Invalid version format. Expected: x.y.z or x.y.z-suffix"); + } Map<String, Object> buildInfo = new HashMap<>(); buildInfo.put("result", true); buildInfo.put("versions", Collections.singletonList(version)); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); buildInfo.put("endTime", buildTime.format(formatter)); return buildInfo; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
.github/workflows/checkstyle.yml
(1 hunks)base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java
(1 hunks)base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java
(12 hunks)base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java
(4 hunks)base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java
(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- .github/workflows/checkstyle.yml
🔇 Additional comments (1)
base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java (1)
31-41
: LGTM! Well-documented class with clear purpose.The class documentation clearly explains its role as a parameter object for block operations, working alongside BlockDto.
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java (4)
96-97
: Consider externalizing hardcoded constants.These values should be configurable through application properties or environment variables to support different deployment environments and improve maintainability.
- private static final int DEFAULT_PLATFORM_ID = 1; - private static final String DEFAULT_USER_ID = "1"; + @Value("${app.default.platform-id}") + private int defaultPlatformId; + @Value("${app.default.user-id}") + private String defaultUserId;
189-204
: Consider extracting group handling logic to a separate method.The block group handling logic could be moved to a dedicated method to improve readability and maintainability.
+ private void deleteBlockGroups(Integer blockId, List<BlockGroup> blockGroups) { + if(blockGroups != null && !blockGroups.isEmpty()){ + List<Integer> blockGroupIds = blockGroups.stream() + .map(BlockGroup::getId) + .collect(Collectors.toList()); + for (Integer id : blockGroupIds) { + blockGroupBlockMapper.deleteByGroupIdAndBlockId(id, blockId); + } + } + }
702-703
: Remove unnecessary semicolon.There's a stray semicolon that should be removed.
- List<Integer> groups = blockDto.getGroups().stream().map(BlockGroup::getId).collect(Collectors.toList()); - ; + List<Integer> groups = blockDto.getGroups().stream().map(BlockGroup::getId).collect(Collectors.toList());
734-748
: Consider making the date format configurable.The date format pattern is hardcoded. Consider making it configurable through application properties to support different date format requirements.
+ @Value("${app.date-format.pattern:yyyy-MM-dd HH:mm:ss}") + private String dateFormatPattern; private Map<String, Object> createBuildInfo(String version, LocalDateTime buildTime) { Map<String, Object> buildInfo = new HashMap<>(); buildInfo.put("result", true); buildInfo.put("versions", Collections.singletonList(version)); - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormatPattern); buildInfo.put("endTime", buildTime.format(formatter)); return buildInfo; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java
(12 hunks)
🔇 Additional comments (1)
base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java (1)
158-165
: LGTM! Proper null checks added.The null checks for blockParam and its ID are well implemented, with appropriate error handling.
English | 简体中文
PR
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
Background and solution
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information
Summary by CodeRabbit
Release Notes
New Features
BlockParam
data transfer object for enhanced block managementBug Fixes
Refactor
BlockParam
block_group_id
column from block-related database tablesChores