Skip to content

Commit 7276827

Browse files
committed
fix(post) : 태그 등록 및 태그 목업 데이터 수정
1 parent 6212480 commit 7276827

7 files changed

Lines changed: 62 additions & 45 deletions

File tree

src/main/java/com/example/FixLog/domain/tag/Tag.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ public class Tag {
2121
@Column(length = 20, nullable = false)
2222
private String tagName;
2323

24-
@Column(nullable = false)
2524
private String tagInfo;
2625

26+
public static Tag of(TagCategory tagCategory, String tagName) {
27+
Tag tag = new Tag();
28+
tag.tagCategory = tagCategory;
29+
tag.tagName = tagName;
30+
return tag;
31+
}
32+
2733
public static Tag of(TagCategory tagCategory, String tagName, String tagInfo) {
2834
Tag tag = new Tag();
2935
tag.tagCategory = tagCategory;

src/main/java/com/example/FixLog/dto/post/NewPostRequestDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ public class NewPostRequestDto {
1717
private String referenceLink;
1818
private String extraContent;
1919

20-
private List<Long> tags;
20+
private List<String> tags;
2121
}

src/main/java/com/example/FixLog/dto/post/PostRequestDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ public class PostRequestDto {
1717
private String referenceLink;
1818
private String extraContent;
1919

20-
private List<Long> tags;
20+
private List<String> tags;
2121
}

src/main/java/com/example/FixLog/exception/CustomException.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
import lombok.RequiredArgsConstructor;
55

66
@Getter
7-
@RequiredArgsConstructor
87
public class CustomException extends RuntimeException {
98
private final ErrorCode errorCode;
9+
10+
public CustomException(ErrorCode errorCode) {
11+
super(errorCode.getMessage());
12+
this.errorCode = errorCode;
13+
}
14+
15+
public CustomException(ErrorCode errorCode, String detailMessage) {
16+
super(detailMessage);
17+
this.errorCode = errorCode;
18+
}
1019
}

src/main/java/com/example/FixLog/mock/TagMockDataInitializer.java

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,46 @@ public void run(String... args) {
3232
List<Tag> tagsToInsert = new ArrayList<>();
3333

3434
// BIG_CATEGORY
35-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "백엔드", "backend"));
36-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "머신러닝", "machine learning"));
37-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "프론트엔드", "frontend"));
35+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "backend"));
36+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "machine-learning"));
37+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(BIG_CATEGORY, "frontend"));
3838

3939
// MAJOR_CATEGORY
40-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "장고", "django"));
41-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "스프링부트", "spring-boot"));
42-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "넥스트", "next.js"));
43-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "케라스", "keras"));
44-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "파이토치", "pytorch"));
45-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "사이킷런", "scikit-learn"));
46-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "노드", "node.js"));
47-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "리액트", "react"));
48-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "리액트 네이티브", "react-native"));
40+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "django"));
41+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "spring-boot"));
42+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "next.js"));
43+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "keras"));
44+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "pytorch"));
45+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "scikit-learn"));
46+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "node.js"));
47+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "react"));
48+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MAJOR_CATEGORY, "react-native"));
4949

5050
// MIDDLE_CATEGORY
51-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "CSS", "css"));
52-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "자바스크립트", "javascript"));
53-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "R", "r"));
54-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "JSON", "json"));
55-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "자바", "java"));
56-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "Haskell", "haskell"));
57-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "파이썬", "python"));
58-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "C", "c"));
51+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "css"));
52+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "javascript"));
53+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "r"));
54+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "json"));
55+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "java"));
56+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "haskell"));
57+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "python"));
58+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MIDDLE_CATEGORY, "c"));
5959

6060
// MINOR_CATEGORY
61-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "NullPointerException", "null-pointer-exception"));
62-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "500 Internal Server Error", "500-error"));
63-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "CORS 정책 오류", "cors-error"));
64-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "Database connection timeout", "db-timeout"));
65-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "ClassNotFoundException", "class-not-found"));
66-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "Cannot read property of undefined", "undefined-property"));
67-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "상태(state) 업데이트 누락", "state-missing"));
68-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "HTTP 에러", "http-error"));
69-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "렌더링 무한 루프", "render-loop"));
70-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "스타일 깨짐", "style-break"));
71-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "404 Not Found", "404-error"));
72-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "Permission Error", "permission-error"));
73-
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "OutOfMemoryError", "out-of-memory"));
61+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "null-pointer-exception", "NullPointerException"));
62+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "500-error", "500 Internal Server Error"));
63+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "cors-error", "CORS 정책 오류"));
64+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "db-timeout", "Database connection timeout"));
65+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "class-not-found", "ClassNotFoundException"));
66+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "undefined-property", "Cannot read property of undefined"));
67+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "state-missing", "상태(state) 업데이트 누락"));
68+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "http-error", "HTTP 에러"));
69+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "render-loop", "렌더링 무한 루프"));
70+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "style-break", "스타일 깨짐"));
71+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "404-error", "404 Not Found"));
72+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "permission-error", "Permission Error"));
73+
addIfNotExist(tagsToInsert, existingTagNames, Tag.of(MINOR_CATEGORY, "out-of-memory", "OutOfMemoryError"));
74+
7475

7576
if (!tagsToInsert.isEmpty()) {
7677
tagRepository.saveAll(tagsToInsert);

src/main/java/com/example/FixLog/repository/tag/TagRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import org.springframework.data.domain.Pageable;
66
import org.springframework.data.jpa.repository.JpaRepository;
77

8+
import java.util.Optional;
9+
810
public interface TagRepository extends JpaRepository<Tag, Long> {
911
Page<Tag> findAll(Pageable pageable);
12+
Optional<Tag> findByTagName(String tagName);
1013
}

src/main/java/com/example/FixLog/service/PostService.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ public void createPost(PostRequestDto postRequestDto){
108108
}
109109

110110
// 태그 다 선택 했는지
111-
private List<Tag> fetchAndValidateTags(List<Long> tagIds){
112-
// 태그 ID로 Tag 엔티티 조회
113-
List<Tag> tags = tagIds.stream()
114-
.map(tagId -> tagRepository.findById(tagId)
111+
private List<Tag> fetchAndValidateTags(List<String> tagNames){
112+
// 태그 이름으로 Tag 엔티티 조회
113+
List<Tag> tags = tagNames.stream()
114+
.map(tagName -> tagRepository.findByTagName(tagName)
115115
.orElseThrow(() -> new CustomException(ErrorCode.TAG_NOT_FOUND)))
116116
.toList();
117117

@@ -141,10 +141,8 @@ else if (categories.size() > 1)
141141
}
142142

143143
if (!issues.isEmpty()) {
144-
throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING);
145-
// throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING, String.join(", ", issues));
146-
// throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING.withDetail(missingTypes.toString()));
147-
// Todo 어떤 태그가 선택 안된건지 보여지도록 수정
144+
String message = String.join(" / ", issues);
145+
throw new CustomException(ErrorCode.REQUIRED_TAGS_MISSING, message);
148146
}
149147
return tags;
150148
}

0 commit comments

Comments
 (0)