Commit bb041e74d0891a24c6f41673a2d99de22c10588a
1 parent
81daeb96
feat(mod): PUT /api/mod/modules/{id} controller REQ-MOD-002
Showing
2 changed files
with
60 additions
and
0 deletions
backend/src/main/java/com/xly/erp/module/mod/controller/ModuleController.java
| ... | ... | @@ -2,9 +2,12 @@ package com.xly.erp.module.mod.controller; |
| 2 | 2 | |
| 3 | 3 | import com.xly.erp.common.response.Result; |
| 4 | 4 | import com.xly.erp.module.mod.dto.CreateModuleDTO; |
| 5 | +import com.xly.erp.module.mod.dto.UpdateModuleDTO; | |
| 5 | 6 | import com.xly.erp.module.mod.service.ModuleService; |
| 6 | 7 | import jakarta.validation.Valid; |
| 8 | +import org.springframework.web.bind.annotation.PathVariable; | |
| 7 | 9 | import org.springframework.web.bind.annotation.PostMapping; |
| 10 | +import org.springframework.web.bind.annotation.PutMapping; | |
| 8 | 11 | import org.springframework.web.bind.annotation.RequestBody; |
| 9 | 12 | import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | 13 | import org.springframework.web.bind.annotation.RestController; |
| ... | ... | @@ -26,4 +29,11 @@ public class ModuleController { |
| 26 | 29 | Integer id = moduleService.create(dto); |
| 27 | 30 | return Result.ok(Map.of("iIncrement", id)); |
| 28 | 31 | } |
| 32 | + | |
| 33 | + @PutMapping("/modules/{id}") | |
| 34 | + public Result<Map<String, Integer>> update(@PathVariable Integer id, | |
| 35 | + @Valid @RequestBody UpdateModuleDTO dto) { | |
| 36 | + Integer updated = moduleService.update(id, dto); | |
| 37 | + return Result.ok(Map.of("iIncrement", updated)); | |
| 38 | + } | |
| 29 | 39 | } | ... | ... |
backend/src/test/java/com/xly/erp/module/mod/controller/ModuleControllerIT.java
| ... | ... | @@ -167,6 +167,56 @@ class ModuleControllerIT { |
| 167 | 167 | assertThat(jb.get("code").asInt()).isEqualTo(20001); |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | + @Test | |
| 171 | + void putValidBody_with_jwt_returns200_andUpdatesEditableFields() throws Exception { | |
| 172 | + Integer id = insertOriginal("sp_test_put_orig", "原名", "ORIG_USER"); | |
| 173 | + String token = testJwtHelper.signFor("ADMIN001"); | |
| 174 | + HttpHeaders headers = jsonHeaders(); | |
| 175 | + headers.set("Authorization", "Bearer " + token); | |
| 176 | + Map<String, Object> body = updateBody(); | |
| 177 | + body.put("sModuleNameZh", "新名"); | |
| 178 | + body.put("sDisplayType", "前端业务"); | |
| 179 | + | |
| 180 | + ResponseEntity<String> resp = rest.exchange( | |
| 181 | + idUrl(id), HttpMethod.PUT, new HttpEntity<>(body, headers), String.class); | |
| 182 | + | |
| 183 | + assertThat(resp.getStatusCode().value()).isEqualTo(200); | |
| 184 | + JsonNode jb = objectMapper.readTree(resp.getBody()); | |
| 185 | + assertThat(jb.get("code").asInt()).isZero(); | |
| 186 | + assertThat(jb.get("data").get("iIncrement").asInt()).isEqualTo(id); | |
| 187 | + | |
| 188 | + Map<String, Object> row = jdbcTemplate.queryForMap( | |
| 189 | + "SELECT sModuleNameZh, sDisplayType, sProcedureName, sCreatedBy FROM tModule WHERE iIncrement = ?", id); | |
| 190 | + assertThat(row.get("sModuleNameZh")).isEqualTo("新名"); | |
| 191 | + assertThat(row.get("sDisplayType")).isEqualTo("前端业务"); | |
| 192 | + assertThat(row.get("sProcedureName")).isEqualTo("sp_test_put_orig"); | |
| 193 | + assertThat(row.get("sCreatedBy")).isEqualTo("ORIG_USER"); | |
| 194 | + } | |
| 195 | + | |
| 196 | + private Integer insertOriginal(String procedureName, String nameZh, String createdBy) { | |
| 197 | + jdbcTemplate.update( | |
| 198 | + "INSERT INTO tModule (sBrandsId, sSubsidiaryId, tCreateDate, sDisplayType, sProcedureName, " | |
| 199 | + + "sModuleType, sManageDeptEn, bShowPermission, sModuleNameZh, iSortOrder, sCreatedBy, bDeleted) " | |
| 200 | + + "VALUES ('XLY','XLY', NOW(), '手机端', ?, '业务模块', 'IT', 0, ?, 0, ?, 0)", | |
| 201 | + procedureName, nameZh, createdBy); | |
| 202 | + return jdbcTemplate.queryForObject( | |
| 203 | + "SELECT iIncrement FROM tModule WHERE sProcedureName = ?", Integer.class, procedureName); | |
| 204 | + } | |
| 205 | + | |
| 206 | + private static Map<String, Object> updateBody() { | |
| 207 | + Map<String, Object> m = new HashMap<>(); | |
| 208 | + m.put("sDisplayType", "手机端"); | |
| 209 | + m.put("sModuleType", "业务模块"); | |
| 210 | + m.put("sManageDeptEn", "IT"); | |
| 211 | + m.put("bShowPermission", false); | |
| 212 | + m.put("sModuleNameZh", "更新后"); | |
| 213 | + return m; | |
| 214 | + } | |
| 215 | + | |
| 216 | + private String idUrl(Integer id) { | |
| 217 | + return "http://localhost:" + port + "/api/mod/modules/" + id; | |
| 218 | + } | |
| 219 | + | |
| 170 | 220 | private String url() { |
| 171 | 221 | return "http://localhost:" + port + "/api/mod/modules"; |
| 172 | 222 | } | ... | ... |