|
| 1 | +package com.back.web7_9_codecrete_be.global.initData; |
| 2 | + |
| 3 | +import com.back.web7_9_codecrete_be.domain.users.entity.User; |
| 4 | +import com.back.web7_9_codecrete_be.domain.users.repository.UserRepository; |
| 5 | +import jakarta.annotation.PostConstruct; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import org.springframework.context.annotation.Profile; |
| 8 | +import org.springframework.security.crypto.password.PasswordEncoder; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | + |
| 11 | +import java.time.LocalDate; |
| 12 | + |
| 13 | +@Profile("dev") |
| 14 | +@Component |
| 15 | +@RequiredArgsConstructor |
| 16 | +public class BaseInitData { |
| 17 | + |
| 18 | + private final UserRepository userRepository; |
| 19 | + private final PasswordEncoder passwordEncoder; |
| 20 | + |
| 21 | + @PostConstruct |
| 22 | + public void init() { |
| 23 | + createTestUser(); |
| 24 | + createAdminUser(); |
| 25 | + } |
| 26 | + |
| 27 | + private void createTestUser() { |
| 28 | + if (userRepository.existsByEmail("test@test.com")) { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + User testUser = User.builder() |
| 33 | + .email("test@test.com") |
| 34 | + .nickname("테스트유저") |
| 35 | + .password(passwordEncoder.encode("test1234!")) |
| 36 | + .birth(LocalDate.of(1999, 1, 1)) |
| 37 | + .profileImage("https://example.com/profile.jpg") |
| 38 | + .build(); |
| 39 | + |
| 40 | + userRepository.save(testUser); |
| 41 | + } |
| 42 | + |
| 43 | + private void createAdminUser() { |
| 44 | + if (userRepository.existsByEmail("admin@test.com")) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + User adminUser = User.builder() |
| 49 | + .email("admin@test.com") |
| 50 | + .nickname("어드민") |
| 51 | + .password(passwordEncoder.encode("admin1234!")) |
| 52 | + .birth(LocalDate.of(1990, 1, 1)) |
| 53 | + .profileImage("https://example.com/profile.jpg") |
| 54 | + .build(); |
| 55 | + |
| 56 | + // dev 전용 어드민 권한 부여 |
| 57 | + adminUser.promoteToAdmin(); |
| 58 | + |
| 59 | + userRepository.save(adminUser); |
| 60 | + } |
| 61 | +} |
0 commit comments