Skip to content

Commit

Permalink
Merge pull request #31 from it-s-time-goat/dev/ldy/directory
Browse files Browse the repository at this point in the history
[FEAT] 폴더 관련 미구현 부분 추가 구현
  • Loading branch information
leedy3838 authored Jul 9, 2024
2 parents 5bf53ac + 2ad456b commit 8cba5ff
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.goat.server.mypage.domain.User;
import com.goat.server.review.application.ReviewService;
import com.goat.server.review.dto.response.ReviewSimpleResponse;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void deleteDirectoryPermanent(Long userId, Long directoryId) {
* 폴더 생성
*/
@Transactional
public void initDirectory(Long userId, DirectoryInitRequest directoryInitRequest) {
public void initDirectory(Long userId, @Valid DirectoryInitRequest directoryInitRequest) {

User user = userService.findUser(userId);
Directory parentDirectory = getParentDirectory(directoryInitRequest);
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/goat/server/directory/domain/Directory.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.goat.server.directory.domain;

import com.goat.server.directory.domain.type.DirectoryColor;
import com.goat.server.directory.domain.type.DirectoryIcon;
import com.goat.server.global.domain.BaseTimeEntity;
import com.goat.server.mypage.domain.User;
import com.goat.server.review.domain.Review;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand Down Expand Up @@ -33,8 +37,11 @@ public class Directory extends BaseTimeEntity {
@Column(name = "directory_title", length = 100)
private String title;

@Column(name = "directory_color", length = 50)
private String directoryColor;
@Enumerated(EnumType.STRING)
private DirectoryIcon directoryIcon;

@Enumerated(EnumType.STRING)
private DirectoryColor directoryColor;

@Column(name = "depth")
private Long depth;
Expand All @@ -54,14 +61,20 @@ public class Directory extends BaseTimeEntity {
private List<Review> reviewList = new ArrayList<>();

@Builder
public Directory(String title, String directoryColor, Long depth, User user, Directory parentDirectory) {
public Directory(String title,
DirectoryIcon directoryIcon,
DirectoryColor directoryColor,
Long depth, User user,
Directory parentDirectory) {
this.title = title;
this.directoryIcon = directoryIcon;
this.directoryColor = directoryColor;
this.depth = depth;
this.user = user;
this.parentDirectory = parentDirectory;
}


public void updateParentDirectory(Directory parentDirectory) {
this.parentDirectory = parentDirectory;
this.depth = parentDirectory.getDepth() + 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.goat.server.directory.domain.type;

public enum DirectoryColor {
RED,
ORANGE,
YELLOW,
PINK,
GREEN,
CYAN,
BLUE,
PURPLE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.goat.server.directory.domain.type;

public enum DirectoryIcon {
PENCIL,
BOOK,
INSIGHT,
BOOK2
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package com.goat.server.directory.dto.request;

import com.goat.server.directory.domain.Directory;
import com.goat.server.directory.domain.type.DirectoryColor;
import com.goat.server.directory.domain.type.DirectoryIcon;
import com.goat.server.directory.util.EnumValid;
import com.goat.server.mypage.domain.User;

public record DirectoryInitRequest(
String directoryName,
Long parentDirectoryId,
String directoryColor
@EnumValid(enumClass = DirectoryColor.class) String directoryColor,
@EnumValid(enumClass = DirectoryIcon.class) String directoryIcon,
String directoryDescription
) {
public Directory toEntity(User user, Directory parentDirectory) {
return Directory.builder()
.user(user)
.parentDirectory(parentDirectory)
.depth(parentDirectory == null ? 1 : parentDirectory.getDepth() + 1)
.title(directoryName)
.directoryColor(directoryColor)
.directoryColor(DirectoryColor.valueOf(directoryColor))
.directoryIcon(DirectoryIcon.valueOf(directoryIcon))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.goat.server.directory.dto.response;

import com.goat.server.directory.domain.Directory;
import com.goat.server.directory.domain.type.DirectoryColor;
import com.goat.server.directory.domain.type.DirectoryIcon;
import lombok.Builder;

@Builder
public record DirectoryResponse(
Long directoryId,
String directoryName,
String directoryColor
DirectoryColor directoryColor,
DirectoryIcon directoryIcon
) {

public static DirectoryResponse from(Directory directory) {
return DirectoryResponse.builder()
.directoryId(directory.getId())
.directoryName(directory.getTitle())
.directoryColor(directory.getDirectoryColor())
.directoryIcon(directory.getDirectoryIcon())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ResponseEntity<ResponseTemplate<Object>> getDirectorySubList(
@PostMapping
public ResponseEntity<ResponseTemplate<Object>> initDirectory(
@AuthenticationPrincipal Long userId,
@RequestBody DirectoryInitRequest directoryInitRequest) {
@Valid @RequestBody DirectoryInitRequest directoryInitRequest) {

directoryService.initDirectory(userId, directoryInitRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public List<DirectoryResponse> findAllDirectoryResponseBySortAndSearch(
.select(Projections.constructor(DirectoryResponse.class,
directory.id,
directory.title,
directory.directoryColor))
directory.directoryColor,
directory.directoryIcon))
.from(directory)
.where(directory.user.userId.eq(userId), isSearchExpression(parentDirectoryId, search))
.orderBy(sortExpression(sort))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.goat.server.directory.repository.init;

import com.goat.server.directory.domain.type.DirectoryColor;
import com.goat.server.directory.domain.type.DirectoryIcon;
import com.goat.server.global.util.LocalDummyDataInit;
import com.goat.server.directory.domain.Directory;
import com.goat.server.directory.repository.DirectoryRepository;
Expand Down Expand Up @@ -41,87 +43,98 @@ public void run(ApplicationArguments args) {

Directory DUMMY_TRASH_DIRECTORY1 = Directory.builder()
.title("trash_directory")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(admin)
.build();
Directory DUMMY_TRASH_DIRECTORY2 = Directory.builder()
.title("trash_directory")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.PINK)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(user)
.build();
Directory DUMMY_TRASH_DIRECTORY3 = Directory.builder()
.title("trash_directory")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.depth(1L)
.parentDirectory(null)
.user(guest)
.build();

Directory DUMMY_PARENT_DIRECTORY1 = Directory.builder()
.title("dummyDirectory1")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(user)
.build();
Directory DUMMY_PARENT_DIRECTORY2 = Directory.builder()
.title("dummyDirectory2")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(user)
.build();
Directory DUMMY_PARENT_DIRECTORY3 = Directory.builder()
.title("dummyDirectory3")
.directoryColor("#FF000F")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(admin)
.build();

Directory DUMMY_CHILD_DIRECTORY1 = Directory.builder()
.title("dummyDirectory4")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(2L)
.parentDirectory(DUMMY_PARENT_DIRECTORY1)
.user(user)
.build();
Directory DUMMY_CHILD_DIRECTORY2 = Directory.builder()
.title("dummyDirectory5")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(2L)
.parentDirectory(DUMMY_PARENT_DIRECTORY1)
.user(user)
.build();
Directory DUMMY_CHILD_DIRECTORY3 = Directory.builder()
.title("dummyDirectory6")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(2L)
.parentDirectory(DUMMY_CHILD_DIRECTORY2)
.user(user)
.build();

Directory DUMMY_STORAGE_DIRECTORY1 = Directory.builder()
.title("storage_directory")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(admin)
.build();
Directory DUMMY_STORAGE_DIRECTORY2 = Directory.builder()
.title("storage_directory")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(user)
.build();
Directory DUMMY_STORAGE_DIRECTORY3 = Directory.builder()
.title("storage_directory")
.directoryColor("#FF00FF")
.directoryColor(DirectoryColor.CYAN)
.directoryIcon(DirectoryIcon.BOOK)
.depth(1L)
.parentDirectory(null)
.user(guest)
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/goat/server/directory/util/EnumValid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.goat.server.directory.util;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = EnumValidator.class)
public @interface EnumValid {

Class<? extends Enum<?>> enumClass();
String message() default "지정되지 않은 Enum 값입니다.";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
23 changes: 23 additions & 0 deletions src/main/java/com/goat/server/directory/util/EnumValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.goat.server.directory.util;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class EnumValidator implements ConstraintValidator<EnumValid, String> {
private Set<String> acceptedValues;

@Override
public void initialize(EnumValid enumAnnotation) {
acceptedValues = Arrays.stream(enumAnnotation.enumClass().getEnumConstants())
.map(Enum::name)
.collect(Collectors.toSet());
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return acceptedValues.contains(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ void getDirectoryListTest() {
//given
given(directoryRepository.existsById(PARENT_DIRECTORY1.getId()))
.willReturn(true);
given(directoryRepository.findAllDirectoryResponseBySortAndSearch(eq(USER_USER.getUserId()), eq(PARENT_DIRECTORY1.getId()), any(), any()))
.willReturn(List.of(DirectoryResponse.from(CHILD_DIRECTORY1), DirectoryResponse.from(CHILD_DIRECTORY2)));
given(directoryRepository.findAllDirectoryResponseBySortAndSearch(eq(USER_USER.getUserId()),
eq(PARENT_DIRECTORY1.getId()), any(), any()))
.willReturn(
List.of(DirectoryResponse.from(CHILD_DIRECTORY1), DirectoryResponse.from(CHILD_DIRECTORY2)));
given(reviewService.getReviewSimpleResponseList(any(), eq(PARENT_DIRECTORY1.getId()), any(), any())).willReturn(
List.of(ReviewSimpleResponse.from(DUMMY_REVIEW1), ReviewSimpleResponse.from(DUMMY_REVIEW2)));

Expand All @@ -82,8 +84,10 @@ void getDirectoryListTest() {
@DisplayName("과목과 폴더 가져 오기 테스트 - 루트 폴더")
void getDirectoryListTest2() {
//given
given(directoryRepository.findAllDirectoryResponseBySortAndSearch(eq(USER_USER.getUserId()), eq(0L), any(), any()))
.willReturn(List.of(DirectoryResponse.from(PARENT_DIRECTORY1), DirectoryResponse.from(PARENT_DIRECTORY2)));
given(directoryRepository.findAllDirectoryResponseBySortAndSearch(eq(USER_USER.getUserId()), eq(0L), any(),
any()))
.willReturn(
List.of(DirectoryResponse.from(PARENT_DIRECTORY1), DirectoryResponse.from(PARENT_DIRECTORY2)));

//when
DirectoryTotalShowResponse directorySubList =
Expand Down Expand Up @@ -118,7 +122,7 @@ void deleteDirectoryTemporalTest() {
void initDirectoryTest() {
//given
DirectoryInitRequest directoryInitRequest =
new DirectoryInitRequest("폴더", 0L, "#FF0000");
new DirectoryInitRequest("폴더", 0L, "BLUE", "PENCIL", "description");

given(userService.findUser(USER_USER.getUserId())).willReturn(USER_USER);

Expand All @@ -131,15 +135,15 @@ void initDirectoryTest() {
Directory savedDirectory = directoryCaptor.getValue();
assertThat(savedDirectory.getTitle()).isEqualTo(directoryInitRequest.directoryName());
assertThat(savedDirectory.getParentDirectory()).isNull();
assertThat(savedDirectory.getDirectoryColor()).isEqualTo(directoryInitRequest.directoryColor());
assertThat(savedDirectory.getDirectoryColor().toString()).isEqualTo(directoryInitRequest.directoryColor());
}

@Test
@DisplayName("폴더 생성 테스트 - 루트 폴더 X")
void initDirectoryTest2() {
//given
DirectoryInitRequest directoryInitRequest =
new DirectoryInitRequest("폴더", PARENT_DIRECTORY1.getId(), "#FF0000");
new DirectoryInitRequest("폴더", PARENT_DIRECTORY1.getId(), "BLUE", "PENCIL", "description");

given(userService.findUser(USER_USER.getUserId())).willReturn(USER_USER);
given(directoryRepository.findById(PARENT_DIRECTORY1.getId()))
Expand All @@ -154,7 +158,7 @@ void initDirectoryTest2() {
Directory savedDirectory = directoryCaptor.getValue();
assertThat(savedDirectory.getTitle()).isEqualTo(directoryInitRequest.directoryName());
assertThat(savedDirectory.getParentDirectory()).isEqualTo(PARENT_DIRECTORY1);
assertThat(savedDirectory.getDirectoryColor()).isEqualTo(directoryInitRequest.directoryColor());
assertThat(savedDirectory.getDirectoryColor().toString()).isEqualTo(directoryInitRequest.directoryColor());
}

@Test
Expand Down
Loading

0 comments on commit 8cba5ff

Please sign in to comment.