Skip to content

Commit

Permalink
Merge pull request Kernel360#98 from mooncw/feature/Kernel360#97-leve…
Browse files Browse the repository at this point in the history
…l-refactor

[refactor] Level 로직 리팩토링
  • Loading branch information
Hju95 authored Jan 11, 2024
2 parents 9e28819 + 777238f commit a25d51e
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ public record CreateLevelRequest(
@NotNull(message = "등급 이름는 필수 항목입니다.")
Long name,
@NotBlank(message = "등급 이미지는 필수 항목입니다.")
String imageUrl
String imageUrl,
@NotNull(message = "등급 경험치 상한선은 필수 항목입니다.")
Long levelUpperLimit
) {

public static Level toEntity(
CreateLevelRequest createLevelRequest) {
return Level.builder()
.name(createLevelRequest.name())
.imageUrl(ImageUtils.parseFilePath(createLevelRequest.imageUrl()))
.levelUpperLimit(createLevelRequest.levelUpperLimit())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
String imageUrl,
@NotNull(message = "등급 경험치 상한선은 필수 입력사항입니다.")
Long levelUpperLimit
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,4 @@ public record UpdateQuestionRequest(
String imageUrl,
@NotNull(message = "최소한 빈 리스트로 들어와야 합니다.")
List<String> 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();
// }
}
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class SecurityConfig {

private final String[] hasRoleAdminPatterns = new String[] {
"/api/v1/techs/{techStackId}",
"/api/v1/levels/"
"/api/v1/levels/**"
};

@Bean
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ VALUES ('2023-12-19T09:00:00', 0, 1, '2023-12-19T09:00:00', '[email protected]',
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', '시간복잡도 사진');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a25d51e

Please sign in to comment.