From ead4b145f16181d5a0145520f57594add18827bb Mon Sep 17 00:00:00 2001 From: gimdonghyeon Date: Wed, 15 May 2024 17:50:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=EB=A7=A4=EA=B1=B0=EC=A7=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=EC=8B=9C=20slug=EA=B0=80=20=ED=95=84?= =?UTF-8?q?=EC=88=98=20=EA=B0=92=EC=9D=B4=20=EC=95=84=EB=8B=88=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/codebase/domain/magazine/dto/MagazineRequest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/example/codebase/domain/magazine/dto/MagazineRequest.java b/src/main/java/com/example/codebase/domain/magazine/dto/MagazineRequest.java index 0808db07..f9ecbd88 100644 --- a/src/main/java/com/example/codebase/domain/magazine/dto/MagazineRequest.java +++ b/src/main/java/com/example/codebase/domain/magazine/dto/MagazineRequest.java @@ -62,7 +62,6 @@ public static class Update { @NotEmpty private String content; - @NotNull private String categorySlug; private Map metadata; From 99a8df26c8aa7bbcc21a7c7361a917c9ded7dbbb Mon Sep 17 00:00:00 2001 From: gimdonghyeon Date: Wed, 15 May 2024 17:51:21 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=EB=A7=A4=EA=B1=B0=EC=A7=84=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=EC=8B=9C=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20slug=EA=B0=80=20null=EC=9D=BC=20=EA=B2=BD=EC=9A=B0?= =?UTF-8?q?=20=EA=B8=B0=EC=A1=B4=EC=9D=98=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codebase/controller/MagazineController.java | 3 +-- .../magazine/service/MagazineService.java | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/codebase/controller/MagazineController.java b/src/main/java/com/example/codebase/controller/MagazineController.java index 106b038f..a696a2d4 100644 --- a/src/main/java/com/example/codebase/controller/MagazineController.java +++ b/src/main/java/com/example/codebase/controller/MagazineController.java @@ -163,8 +163,7 @@ public ResponseEntity updateMagazine( String loginUsername = SecurityUtil.getCurrentUsername() .orElseThrow(LoginRequiredException::new); - MagazineCategory category = magazineCategoryService.getEntity(magazineRequest.getCategorySlug()); - MagazineResponse.Get magazine = magazineService.update(id, loginUsername, magazineRequest, category); + MagazineResponse.Get magazine = magazineService.update(id, loginUsername, magazineRequest); return new ResponseEntity(magazine, HttpStatus.OK); } diff --git a/src/main/java/com/example/codebase/domain/magazine/service/MagazineService.java b/src/main/java/com/example/codebase/domain/magazine/service/MagazineService.java index 303ec8b9..e4d92860 100644 --- a/src/main/java/com/example/codebase/domain/magazine/service/MagazineService.java +++ b/src/main/java/com/example/codebase/domain/magazine/service/MagazineService.java @@ -33,12 +33,15 @@ public class MagazineService { private final MagazineMediaRepository magazineMediaRepository; + private final MagazineCategoryRepository magazineCategoryRepository; + @Autowired - public MagazineService(MagazineRepository magazineRepository, MagazineCommentRepository magazineCommentRepository, MagazineMediaRepository magazineMediaRepository) { + public MagazineService(MagazineRepository magazineRepository, MagazineCommentRepository magazineCommentRepository, MagazineMediaRepository magazineMediaRepository, MagazineCategoryRepository magazineCategoryRepository) { this.magazineRepository = magazineRepository; this.magazineCommentRepository = magazineCommentRepository; this.magazineMediaRepository = magazineMediaRepository; + this.magazineCategoryRepository = magazineCategoryRepository; } @Transactional @@ -81,10 +84,12 @@ public void delete(String loginUsername, Long id) { } @Transactional - public MagazineResponse.Get update(Long id, String loginUsername, MagazineRequest.Update magazineRequest, MagazineCategory magazineCategory) { + public MagazineResponse.Get update(Long id, String loginUsername, MagazineRequest.Update magazineRequest) { Magazine magazine = magazineRepository.findById(id) .orElseThrow(() -> new NotFoundException("해당 매거진이 존재하지 않습니다.")); + MagazineCategory magazineCategory = getCategory(magazineRequest.getCategorySlug(), magazine.getCategory()); + validateOwner(loginUsername, magazine); magazine.update(magazineRequest, magazineCategory); @@ -92,6 +97,14 @@ public MagazineResponse.Get update(Long id, String loginUsername, MagazineReques return MagazineResponse.Get.from(magazine); } + private MagazineCategory getCategory(String categorySlug, MagazineCategory defaultCategory) { + if (categorySlug != null) { + return magazineCategoryRepository.findBySlug(categorySlug) + .orElseThrow(() -> new NotFoundException("해당 카테고리가 존재하지 않습니다.")); + } + return defaultCategory; + } + private void validateOwner(String loginUsername, Magazine magazine) { if (!magazine.isOwner(loginUsername)) { throw new IllegalArgumentException("해당 매거진의 소유자가 아닙니다."); From 1468edfb08053d7054868258cfe64a2c950f10e5 Mon Sep 17 00:00:00 2001 From: gimdonghyeon Date: Wed, 15 May 2024 17:51:37 +0900 Subject: [PATCH 3/3] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MagazineControllerTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/test/java/com/example/codebase/controller/MagazineControllerTest.java b/src/test/java/com/example/codebase/controller/MagazineControllerTest.java index 209c0e1a..cd57b402 100644 --- a/src/test/java/com/example/codebase/controller/MagazineControllerTest.java +++ b/src/test/java/com/example/codebase/controller/MagazineControllerTest.java @@ -1025,4 +1025,37 @@ public void createAndInviteMember(TeamUser loginUser, Member inviteMember) { //then assertTrue(response.contains("잠시 후 다시 시도해주세요.")); } + + @WithMockCustomUser(username = "testid", role = "USER") + @DisplayName("매거진 수정시 slug가 없는 경우") + @Test + void 매거진_수정시_slug가_없는경우() throws Exception { + // given + Member author = createMember("testid"); + MagazineResponse.Get magazine = createMagaizne(author); + String originalCategorySlug = magazine.getCategorySlug(); + + MagazineRequest.Update magazineRequest = new MagazineRequest.Update(); + magazineRequest.setTitle("수정된 제목"); + magazineRequest.setContent("수정된 내용"); + magazineRequest.setCategorySlug(null); + + // when + String response = mockMvc.perform( + put("/api/magazines/" + magazine.getId()) + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(magazineRequest)) + ) + .andDo(print()) + .andExpect(status().isOk()) + .andReturn().getResponse().getContentAsString(StandardCharsets.UTF_8); + + // then + MagazineResponse.Get magazineResponse = objectMapper.readValue(response, MagazineResponse.Get.class); + assertEquals(magazine.getId(), magazineResponse.getId()); + assertEquals(magazineRequest.getTitle(), magazineResponse.getTitle()); + assertEquals(magazineRequest.getContent(), magazineResponse.getContent()); + assertEquals(originalCategorySlug, magazineResponse.getCategorySlug()); + } + }