|
| 1 | +package com.example.FixLog.mock; |
| 2 | + |
| 3 | +import com.example.FixLog.domain.tag.Tag; |
| 4 | +import com.example.FixLog.repository.tag.TagRepository; |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import org.springframework.boot.CommandLineRunner; |
| 7 | +import org.springframework.core.annotation.Order; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | + |
| 10 | +import java.util.List; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Set; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +import static com.example.FixLog.domain.tag.TagCategory.*; |
| 16 | + |
| 17 | +@Component |
| 18 | +@RequiredArgsConstructor |
| 19 | +@Order(4) |
| 20 | +public class TagMockDataInitializer implements CommandLineRunner { |
| 21 | + |
| 22 | + private final TagRepository tagRepository; |
| 23 | + |
| 24 | + @Override |
| 25 | + public void run(String... args) { |
| 26 | + // 현재 저장된 태그 이름들 (중복 방지용) |
| 27 | + Set<String> existingTagNames = tagRepository.findAll() |
| 28 | + .stream() |
| 29 | + .map(Tag::getTagName) |
| 30 | + .collect(Collectors.toSet()); |
| 31 | + |
| 32 | + List<Tag> tagsToInsert = new ArrayList<>(); |
| 33 | + |
| 34 | + // 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")); |
| 38 | + |
| 39 | + // 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")); |
| 49 | + |
| 50 | + // 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")); |
| 59 | + |
| 60 | + // 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")); |
| 74 | + |
| 75 | + if (!tagsToInsert.isEmpty()) { |
| 76 | + tagRepository.saveAll(tagsToInsert); |
| 77 | + System.out.println("중복 없는 목업 태그 " + tagsToInsert.size() + "개 삽입 완료"); |
| 78 | + } else { |
| 79 | + System.out.println("삽입할 신규 태그가 없습니다."); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void addIfNotExist(List<Tag> list, Set<String> existingNames, Tag tag) { |
| 84 | + if (!existingNames.contains(tag.getTagName())) { |
| 85 | + list.add(tag); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments