-
Notifications
You must be signed in to change notification settings - Fork 20
feat: userSeeder 추가 #1021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: userSeeder 추가 #1021
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d74527
feat: userSeeder 추가
bingle625 85209d5
Update app-main/src/main/java/net/causw/app/main/shared/seed/UserSeed…
bingle625 9af0637
feat: Sys out 대신 log 추가
bingle625 2d85a33
docs: seed application 파일 추가
bingle625 bcf16c2
refac: userSeeder 리팩토링
bingle625 eae8b61
Merge remote-tracking branch 'origin/dev' into chore/#1019/user-seed
bingle625 f34b885
Merge branch 'dev' into chore/#1019/user-seed
bingle625 7a8a947
Merge branch 'dev' into chore/#1019/user-seed
bingle625 4247a2e
style: spotless 포매팅
bingle625 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app-main/src/main/java/net/causw/app/main/shared/seed/UserSeedRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package net.causw.app.main.shared.seed; | ||
|
|
||
| import org.springframework.boot.CommandLineRunner; | ||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Component | ||
| @Profile("seed") | ||
| public class UserSeedRunner implements CommandLineRunner { | ||
|
|
||
| private final UserSeeder userSeeder; | ||
|
|
||
| public UserSeedRunner(UserSeeder userSeeder) { | ||
| this.userSeeder = userSeeder; | ||
| } | ||
|
|
||
| @Override | ||
| public void run(String... args) { | ||
| int count = args.length > 0 ? Integer.parseInt(args[0]) : 10_000; | ||
bingle625 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| userSeeder.seed(count); | ||
| } | ||
| } | ||
150 changes: 150 additions & 0 deletions
150
app-main/src/main/java/net/causw/app/main/shared/seed/UserSeeder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| package net.causw.app.main.shared.seed; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
|
|
||
| import org.springframework.context.annotation.Profile; | ||
| import org.springframework.security.crypto.password.PasswordEncoder; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import net.causw.app.main.api.dto.user.UserCreateRequestDto; | ||
| import net.causw.app.main.domain.asset.file.entity.UuidFile; | ||
| import net.causw.app.main.domain.asset.file.entity.joinEntity.UserAcademicRecordApplicationAttachImage; | ||
| import net.causw.app.main.domain.asset.file.entity.joinEntity.UserAdmissionAttachImage; | ||
bingle625 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import net.causw.app.main.domain.asset.file.entity.joinEntity.UserProfileImage; | ||
| import net.causw.app.main.domain.asset.file.enums.FilePath; | ||
| import net.causw.app.main.domain.user.academic.entity.userAcademicRecord.UserAcademicRecordApplication; | ||
| import net.causw.app.main.domain.user.academic.enums.userAcademicRecord.AcademicRecordRequestStatus; | ||
| import net.causw.app.main.domain.user.academic.enums.userAcademicRecord.AcademicStatus; | ||
| import net.causw.app.main.domain.user.account.entity.user.User; | ||
| import net.causw.app.main.domain.user.account.entity.user.UserAdmission; | ||
| import net.causw.app.main.domain.user.account.entity.user.UserAdmissionLog; | ||
| import net.causw.app.main.domain.user.account.enums.user.Department; | ||
| import net.causw.app.main.domain.user.account.enums.user.Role; | ||
| import net.causw.app.main.domain.user.account.enums.user.UserAdmissionLogAction; | ||
| import net.causw.app.main.domain.user.account.enums.user.UserState; | ||
| import net.causw.app.main.domain.user.account.repository.user.UserRepository; | ||
|
|
||
| import jakarta.persistence.EntityManager; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Component | ||
| @Profile("seed") | ||
| @RequiredArgsConstructor | ||
| public class UserSeeder { | ||
|
|
||
| private final EntityManager em; | ||
| private final PasswordEncoder passwordEncoder; | ||
| private final UserRepository userRepository; | ||
|
|
||
| @Transactional | ||
| public void seed(int count) { | ||
|
|
||
| boolean exist = userRepository.existsBy(); | ||
bingle625 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (exist) { | ||
| System.out.println("🚫 Seed skipped: users already exist"); | ||
bingle625 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
| process(count); | ||
| } | ||
|
|
||
| private void process(int count) { | ||
| int batchSize = 500; | ||
|
|
||
| String encodedPassword = passwordEncoder.encode("password00!!"); | ||
bingle625 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for (int i = 1; i <= count; i++) { | ||
|
|
||
| UserCreateRequestDto dto = UserCreateRequestDto.builder() | ||
| .email("seed" + i + "@cau.ac.kr") | ||
| .name("시드유저" + i) | ||
| .password("password00!!") // 정규식 통과 | ||
| .studentId("2020" + String.format("%04d", i)) | ||
| .admissionYear(2020) | ||
| .nickname("seed_" + i) | ||
| .major("소프트웨어학부") | ||
| .department(Department.SCHOOL_OF_SW) | ||
| .phoneNumber("010-9" + String.format("%03d-%04d", i / 10, i % 10000)) | ||
bingle625 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .build(); | ||
|
|
||
| User user = User.from(dto, encodedPassword); | ||
| user.setRoles(Set.of(Role.COMMON)); | ||
| user.setState(UserState.ACTIVE); | ||
|
|
||
| em.persist(user); | ||
|
|
||
| // UUID File (가짜 파일) | ||
| UuidFile file = UuidFile.of(UUID.randomUUID().toString(), "seed/profile/" + i + ".png", | ||
| "https://cdn.seed.test/profile/" + i + ".png", "profile_" + i + ".png", "png", FilePath.ETC); | ||
| em.persist(file); | ||
|
|
||
| // User File 관계 테이블 | ||
| UserProfileImage profile = UserProfileImage.of(user, file); | ||
|
|
||
| em.persist(profile); | ||
|
|
||
| UserAdmissionLog log = UserAdmissionLog.of( | ||
| user.getEmail(), | ||
| user.getName(), | ||
| "admin@seed.test", | ||
| "시드 관리자", | ||
| UserAdmissionLogAction.ACCEPT, | ||
| List.of(), | ||
| "시드 데이터 - 가입 승인 완료", | ||
| null | ||
| ); | ||
| em.persist(log); | ||
|
|
||
| createEnrolledAcademicRecord(user, 2026); | ||
|
|
||
| // batch flush | ||
| if (i % batchSize == 0) { | ||
| em.flush(); | ||
| em.clear(); | ||
| System.out.println("Seeded users: " + i); | ||
| } | ||
| } | ||
|
|
||
| em.flush(); | ||
| em.clear(); | ||
| } | ||
|
|
||
| private void createEnrolledAcademicRecord( | ||
| User user, | ||
| int completedSemester | ||
| ) { | ||
| // 1. User 상태 확정 | ||
| user.setAcademicStatus(AcademicStatus.ENROLLED); | ||
| user.setCurrentCompletedSemester(completedSemester); | ||
| user.setState(UserState.ACTIVE); | ||
|
|
||
| // 2. 학적 증빙 신청서 (이미 ACCEPT 된 상태) | ||
| UserAcademicRecordApplication application = | ||
| UserAcademicRecordApplication.create( | ||
| user, | ||
| AcademicRecordRequestStatus.ACCEPT, | ||
| AcademicStatus.ENROLLED, | ||
| completedSemester, | ||
| "시드 데이터 - 재학 인증 완료" | ||
| ); | ||
|
|
||
| em.persist(application); | ||
|
|
||
| // 3. 첨부 이미지 1개 | ||
| UuidFile recordFile = UuidFile.of( | ||
| UUID.randomUUID().toString(), | ||
| "seed/academic-record/" + user.getId() + ".png", | ||
| "https://cdn.seed.test/academic-record/" + user.getId() + ".png", | ||
| "academic_record.png", | ||
| "png", | ||
| FilePath.ETC | ||
| ); | ||
| em.persist(recordFile); | ||
|
|
||
| UserAcademicRecordApplicationAttachImage attach = | ||
| UserAcademicRecordApplicationAttachImage.of(application, recordFile); | ||
| em.persist(attach); | ||
| } | ||
bingle625 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.