From 777238ff0ffedd2d11b90795102ba5da57d76a5a Mon Sep 17 00:00:00 2001 From: mooncw Date: Thu, 11 Jan 2024 13:40:17 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20Level=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 1차 완료 - data.sql에 postman 테스트를 위한 데이터 추가 --- .../domain/level/dto/CreateLevelRequest.java | 5 ++++- .../domain/level/dto/LevelDto.java | 6 ++++-- .../domain/level/dto/UpdateLevelRequest.java | 18 ++++-------------- .../domain/level/dto/UpdateLevelResponse.java | 8 +++++--- .../domain/level/entity/Level.java | 5 +++-- .../domain/level/service/LevelService.java | 2 +- .../question/dto/UpdateQuestionRequest.java | 10 +--------- .../global/config/SecurityConfig.java | 2 +- src/main/resources/data.sql | 6 ++++++ .../level/controller/LevelControllerTest.java | 13 +++++++++---- .../domain/level/service/LevelServiceTest.java | 4 ++-- 11 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/CreateLevelRequest.java b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/CreateLevelRequest.java index d47dbc47..79b4cee5 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/CreateLevelRequest.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/CreateLevelRequest.java @@ -10,7 +10,9 @@ public record CreateLevelRequest( @NotNull(message = "등급 이름는 필수 항목입니다.") Long name, @NotBlank(message = "등급 이미지는 필수 항목입니다.") - String imageUrl + String imageUrl, + @NotNull(message = "등급 경험치 상한선은 필수 항목입니다.") + Long levelUpperLimit ) { public static Level toEntity( @@ -18,6 +20,7 @@ public static Level toEntity( return Level.builder() .name(createLevelRequest.name()) .imageUrl(ImageUtils.parseFilePath(createLevelRequest.imageUrl())) + .levelUpperLimit(createLevelRequest.levelUpperLimit()) .build(); } } diff --git a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/LevelDto.java b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/LevelDto.java index 87aac34b..42723aff 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/LevelDto.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/LevelDto.java @@ -6,13 +6,15 @@ public record LevelDto( Long id, Long name, - String imageUrl + String imageUrl, + Long leveUpperLimit ) { public static LevelDto from(Level level) { return new LevelDto( level.getId(), level.getName(), - ImageUtils.makeImageUrl(level.getImageUrl()) + ImageUtils.makeImageUrl(level.getImageUrl()), + level.getLevelUpperLimit() ); } diff --git a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelRequest.java b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelRequest.java index ec346ee1..c6f13fc4 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelRequest.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelRequest.java @@ -10,20 +10,10 @@ * DTO for {@link com.kernel360.kernelsquare.domain.level.entity.Level} */ public record UpdateLevelRequest( - @NotNull(message = "등급 아이디는 필수 입력사항입니다.") - Long id, @NotNull(message = "등급은 필수 입력사항입니다.") Long name, @NotBlank(message = "등급 이미지 URL은 필수 입력사항입니다.") - String imageUrl -) { - - public static Level toEntity( - UpdateLevelRequest updateLevelRequest) { - return Level.builder() - .id(updateLevelRequest.id()) - .name(updateLevelRequest.name()) - .imageUrl(ImageUtils.parseFilePath(updateLevelRequest.imageUrl())) - .build(); - } -} \ No newline at end of file + String imageUrl, + @NotNull(message = "등급 경험치 상한선은 필수 입력사항입니다.") + Long levelUpperLimit +) {} \ No newline at end of file diff --git a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelResponse.java b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelResponse.java index 60226c55..50ce8054 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelResponse.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/level/dto/UpdateLevelResponse.java @@ -5,13 +5,15 @@ public record UpdateLevelResponse( Long id, Long name, - String imageUrl) { - + String imageUrl, + Long levelUpperLimit +) { public static UpdateLevelResponse from(Level level) { return new UpdateLevelResponse( level.getId(), level.getName(), - level.getImageUrl() + level.getImageUrl(), + level.getLevelUpperLimit() ); } } \ No newline at end of file diff --git a/src/main/java/com/kernel360/kernelsquare/domain/level/entity/Level.java b/src/main/java/com/kernel360/kernelsquare/domain/level/entity/Level.java index 4e06d7d8..6e9e8e7e 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/level/entity/Level.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/level/entity/Level.java @@ -31,7 +31,7 @@ public class Level extends BaseEntity { private String imageUrl; @Column(nullable = false, name = "level_upper_limit", columnDefinition = "bigint") - private Long levelUpperLimit = 500L; + private Long levelUpperLimit; @Builder public Level(Long id, Long name, String imageUrl, Long levelUpperLimit) { @@ -41,8 +41,9 @@ public Level(Long id, Long name, String imageUrl, Long levelUpperLimit) { this.levelUpperLimit = levelUpperLimit; } - public void update(Long name, String imageUrl) { + public void update(Long name, String imageUrl, Long levelUpperLimit) { this.name = name; this.imageUrl = imageUrl; + this.levelUpperLimit = levelUpperLimit; } } diff --git a/src/main/java/com/kernel360/kernelsquare/domain/level/service/LevelService.java b/src/main/java/com/kernel360/kernelsquare/domain/level/service/LevelService.java index ba22251c..6de7e6ef 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/level/service/LevelService.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/level/service/LevelService.java @@ -51,7 +51,7 @@ public UpdateLevelResponse updateLevel(Long levelId, UpdateLevelRequest updateLe throw new BusinessException(LevelErrorCode.LEVEL_ALREADY_EXISTED); }); - level.update(updateLevelRequest.name(), updateLevelRequest.imageUrl()); + level.update(updateLevelRequest.name(), updateLevelRequest.imageUrl(), updateLevelRequest.levelUpperLimit()); return UpdateLevelResponse.from(level); } diff --git a/src/main/java/com/kernel360/kernelsquare/domain/question/dto/UpdateQuestionRequest.java b/src/main/java/com/kernel360/kernelsquare/domain/question/dto/UpdateQuestionRequest.java index 89b2e48b..39e15bc4 100644 --- a/src/main/java/com/kernel360/kernelsquare/domain/question/dto/UpdateQuestionRequest.java +++ b/src/main/java/com/kernel360/kernelsquare/domain/question/dto/UpdateQuestionRequest.java @@ -13,12 +13,4 @@ public record UpdateQuestionRequest( String imageUrl, @NotNull(message = "최소한 빈 리스트로 들어와야 합니다.") List skills -) { -// public static Question toEntity(UpdateQuestionRequest updateQuestionRequest) { -// return Question.builder() -// .title(updateQuestionRequest.title()) -// .content(updateQuestionRequest.content()) -// .imageUrl(updateQuestionRequest.imageUrl()!=null? ImageUtils.parseFilePath(updateQuestionRequest.imageUrl()): null) -// .build(); -// } -} +) {} diff --git a/src/main/java/com/kernel360/kernelsquare/global/config/SecurityConfig.java b/src/main/java/com/kernel360/kernelsquare/global/config/SecurityConfig.java index fdfecdd8..f16117c9 100644 --- a/src/main/java/com/kernel360/kernelsquare/global/config/SecurityConfig.java +++ b/src/main/java/com/kernel360/kernelsquare/global/config/SecurityConfig.java @@ -52,7 +52,7 @@ public class SecurityConfig { private final String[] hasRoleAdminPatterns = new String[] { "/api/v1/techs/{techStackId}", - "/api/v1/levels/" + "/api/v1/levels/**" }; @Bean diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index cf01f781..09eb1bde 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -6,9 +6,15 @@ VALUES ('2023-12-19T09:00:00', 0, 1, '2023-12-19T09:00:00', 'duck@example.com', INSERT INTO authority (authority_type) VALUES ('ROLE_USER'); +INSERT INTO authority (authority_type) +VALUES ('ROLE_ADMIN'); + INSERT INTO member_authority (member_id, authority_id) VALUES (1, 1); +INSERT INTO member_authority (member_id, authority_id) +VALUES (1, 2); + INSERT INTO question (closed_status, created_date, id, member_id, modified_date, view_count, content, image_url, title) VALUES (1, '2023-12-19T09:00:00', 1, 1, '2023-12-21T09:00:00', 20, 'No content', 'question/d1c27379-2d08-4a6a-9d97-368124a50900thumb.jpg', '시간복잡도 사진'); diff --git a/src/test/java/com/kernel360/kernelsquare/domain/level/controller/LevelControllerTest.java b/src/test/java/com/kernel360/kernelsquare/domain/level/controller/LevelControllerTest.java index 771376d7..a5163a0f 100644 --- a/src/test/java/com/kernel360/kernelsquare/domain/level/controller/LevelControllerTest.java +++ b/src/test/java/com/kernel360/kernelsquare/domain/level/controller/LevelControllerTest.java @@ -48,13 +48,16 @@ void testCreateLevel() throws Exception { // Given Long name = 3L; String imageUrl = "test22"; + Long levelUpperLimit = 2000L; Level level = Level.builder() .id(1L) .name(name) .imageUrl(imageUrl) + .levelUpperLimit(levelUpperLimit) .build(); - CreateLevelRequest createLevelRequest = new CreateLevelRequest(name, imageUrl); + + CreateLevelRequest createLevelRequest = new CreateLevelRequest(name, imageUrl, levelUpperLimit); CreateLevelResponse createLevelResponse = CreateLevelResponse.from(level); doReturn(createLevelResponse) @@ -144,16 +147,18 @@ void testDeleteLevel() throws Exception { @DisplayName("레벨 수정 성공 시 200 OK와 메시지를 반환한다") void testUpdateLevel() throws Exception { // Given - Long id = 1L; Long name = 3L; String imageUrl = "image1.jpg"; + Long levelUpperLimit = 2000L; Level level = Level.builder() - .id(id) + .id(1L) .name(name) .imageUrl(imageUrl) + .levelUpperLimit(levelUpperLimit) .build(); - UpdateLevelRequest updateLevelRequest = new UpdateLevelRequest(id, name, imageUrl); + + UpdateLevelRequest updateLevelRequest = new UpdateLevelRequest(name, imageUrl, levelUpperLimit); UpdateLevelResponse updateLevelResponse = UpdateLevelResponse.from(level); doReturn(updateLevelResponse) diff --git a/src/test/java/com/kernel360/kernelsquare/domain/level/service/LevelServiceTest.java b/src/test/java/com/kernel360/kernelsquare/domain/level/service/LevelServiceTest.java index 8299df0e..14aeed5f 100644 --- a/src/test/java/com/kernel360/kernelsquare/domain/level/service/LevelServiceTest.java +++ b/src/test/java/com/kernel360/kernelsquare/domain/level/service/LevelServiceTest.java @@ -36,7 +36,7 @@ class LevelServiceTest { @DisplayName("등급 생성 테스트") void testCreateLevel() { // Given - CreateLevelRequest createLevelRequest = new CreateLevelRequest(3L, "testurl"); + CreateLevelRequest createLevelRequest = new CreateLevelRequest(3L, "testurl", 2000L); Level level = CreateLevelRequest.toEntity(createLevelRequest); @@ -98,7 +98,7 @@ void testUpdateLevel() { .name(1L) .imageUrl("image1.jpg") .build(); - UpdateLevelRequest updateLevelRequest = new UpdateLevelRequest(1L, 2L, "image2.jpg"); + UpdateLevelRequest updateLevelRequest = new UpdateLevelRequest(2L, "image2.jpg",1100L); given(levelRepository.findById(anyLong())).willReturn(Optional.of(level)); // When