11package com .back .global .init ;
22
3+ import com .back .domain .cocktail .entity .Cocktail ;
4+ import com .back .domain .cocktail .repository .CocktailRepository ;
35import com .back .domain .notification .entity .Notification ;
46import com .back .domain .notification .enums .NotificationType ;
57import com .back .domain .notification .repository .NotificationRepository ;
6- import com .back .domain .cocktail .entity .Cocktail ;
7- import com .back .domain .cocktail .enums .AlcoholBaseType ;
8- import com .back .domain .cocktail .enums .AlcoholStrength ;
9- import com .back .domain .cocktail .enums .CocktailType ;
10- import com .back .domain .cocktail .repository .CocktailRepository ;
118import com .back .domain .post .category .entity .Category ;
129import com .back .domain .post .category .repository .CategoryRepository ;
1310import com .back .domain .post .comment .entity .Comment ;
1916import com .back .domain .post .post .repository .PostRepository ;
2017import com .back .domain .user .entity .User ;
2118import com .back .domain .user .repository .UserRepository ;
22- import org .springframework .transaction .annotation .Transactional ;
2319import lombok .RequiredArgsConstructor ;
2420import org .springframework .beans .factory .annotation .Autowired ;
2521import org .springframework .boot .ApplicationRunner ;
2622import org .springframework .context .annotation .Bean ;
2723import org .springframework .context .annotation .Configuration ;
2824import org .springframework .context .annotation .Lazy ;
2925import org .springframework .context .annotation .Profile ;
26+ import org .springframework .transaction .annotation .Transactional ;
3027
3128@ Configuration
3229@ Profile ("dev" )
@@ -50,67 +47,14 @@ public class DevInitData {
5047 @ Bean
5148 ApplicationRunner devInitDataApplicationRunner () {
5249 return args -> {
53- self .cocktailInit ();
5450 self .userInit ();
55- self . boardInit ();
51+ // myBar는 사용자 활성 상태 기준으로 초기화가 필요하므로 boardInit(soft delete)보다 먼저 실행
5652 self .myBarInit ();
53+ self .boardInit ();
5754 self .notificationInit ();
5855 };
5956 }
6057
61- @ Transactional
62- public void cocktailInit () {
63- if (cocktailRepository .count () > 0 ) return ;
64-
65- // 1) 하이볼
66- cocktailRepository .save (Cocktail .builder ()
67- .cocktailName ("Highball" )
68- .cocktailNameKo ("하이볼" )
69- .alcoholStrength (AlcoholStrength .LIGHT )
70- .cocktailType (CocktailType .LONG )
71- .alcoholBaseType (AlcoholBaseType .WHISKY )
72- .ingredient ("위스키, 탄산수, 얼음, 레몬피" )
73- .recipe ("잔에 얼음 → 위스키 → 탄산수 → 가볍게 스터" )
74- .cocktailImgUrl ("/img/cocktail/1.jpg" )
75- .build ());
76-
77- // 2) 진토닉
78- cocktailRepository .save (Cocktail .builder ()
79- .cocktailName ("Gin and Tonic" )
80- .cocktailNameKo ("진토닉" )
81- .alcoholStrength (AlcoholStrength .WEAK )
82- .cocktailType (CocktailType .LONG )
83- .alcoholBaseType (AlcoholBaseType .GIN )
84- .ingredient ("진, 토닉워터, 얼음, 라임" )
85- .recipe ("잔에 얼음 → 진 → 토닉워터 → 라임" )
86- .cocktailImgUrl ("/img/cocktail/2.jpg" )
87- .build ());
88-
89- // 3) 올드패션드
90- cocktailRepository .save (Cocktail .builder ()
91- .cocktailName ("Old Fashioned" )
92- .cocktailNameKo ("올드패션드" )
93- .alcoholStrength (AlcoholStrength .STRONG )
94- .cocktailType (CocktailType .SHORT )
95- .alcoholBaseType (AlcoholBaseType .WHISKY )
96- .ingredient ("버번 위스키, 설탕/시럽, 앙고스투라 비터스, 오렌지 필" )
97- .recipe ("시럽+비터스 → 위스키 → 얼음 → 스터 → 오렌지 필" )
98- .cocktailImgUrl ("/img/cocktail/3.jpg" )
99- .build ());
100-
101- // 4) 모히또
102- cocktailRepository .save (Cocktail .builder ()
103- .cocktailName ("Mojito" )
104- .cocktailNameKo ("모히또" )
105- .alcoholStrength (AlcoholStrength .LIGHT )
106- .cocktailType (CocktailType .LONG )
107- .alcoholBaseType (AlcoholBaseType .RUM )
108- .ingredient ("라임, 민트, 설탕/시럽, 화이트 럼, 탄산수, 얼음" )
109- .recipe ("라임+민트+시럽 머들 → 럼 → 얼음 → 탄산수" )
110- .cocktailImgUrl ("/img/cocktail/4.jpg" )
111- .build ());
112- }
113-
11458 @ Transactional
11559 public void userInit () {
11660 userRepository .findByNickname ("사용자A" ).orElseGet (() ->
@@ -270,39 +214,72 @@ public void notificationInit() {
270214 public void myBarInit () {
271215 if (myBarRepository .count () > 0 ) return ;
272216
273- User userA = userRepository .findByNickname ("사용자A" ).orElse (null );
274- User userB = userRepository .findByNickname ("사용자B" ).orElse (null );
275- User userC = userRepository .findByNickname ("사용자C" ).orElse (null );
276-
277- if (userA == null || userC == null ) return ;
217+ User userA = userRepository .findByNickname ("사용자A" ).orElseThrow ();
218+ User userB = userRepository .findByNickname ("사용자B" ).orElseThrow ();
219+ User userC = userRepository .findByNickname ("사용자C" ).orElseThrow ();
278220
279- // 칵테일 참조 준비
280- var cocktails = cocktailRepository .findAll ();
281- Cocktail c1 = cocktails .stream ().filter (c -> "하이볼" .equals (c .getCocktailNameKo ())).findFirst ().orElse (null );
282- Cocktail c2 = cocktails .stream ().filter (c -> "진토닉" .equals (c .getCocktailNameKo ())).findFirst ().orElse (null );
283- Cocktail c3 = cocktails .stream ().filter (c -> "올드패션드" .equals (c .getCocktailNameKo ())).findFirst ().orElse (null );
284- Cocktail c4 = cocktails .stream ().filter (c -> "모히또" .equals (c .getCocktailNameKo ())).findFirst ().orElse (null );
221+ // 칵테일 참조 준비: 이름 우선 매칭, 부족하면 ID 오름차순으로 보충
222+ var all = cocktailRepository .findAll ();
223+ if (all .isEmpty ()) return ; // 칵테일 데이터 없으면 스킵
224+
225+ java .util .List <String > prefer = java .util .List .of ("하이볼" , "진토닉" , "올드패션드" , "모히또" );
226+ java .util .List <Cocktail > selected = new java .util .ArrayList <>();
227+
228+ // 선호 이름 매칭
229+ for (String nameKo : prefer ) {
230+ all .stream ()
231+ .filter (c -> nameKo .equals (c .getCocktailNameKo ()))
232+ .findFirst ()
233+ .ifPresent (c -> {
234+ if (selected .stream ().noneMatch (s -> java .util .Objects .equals (s .getId (), c .getId ()))) {
235+ selected .add (c );
236+ }
237+ });
238+ }
285239
286- // 방어: 칵테일 누락 시 스킵
287- if (c1 == null || c2 == null || c3 == null || c4 == null ) return ;
240+ // 부족분 보충: ID 오름차순으로 정렬 후 채우기
241+ all .stream ()
242+ .sorted (java .util .Comparator .comparingLong (c -> c .getId () == null ? Long .MAX_VALUE : c .getId ()))
243+ .forEach (c -> {
244+ if (selected .size () < 4 && selected .stream ().noneMatch (s -> java .util .Objects .equals (s .getId (), c .getId ()))) {
245+ selected .add (c );
246+ }
247+ });
248+
249+ // 실제 사용에 필요한 인덱스가 없으면 해당 동작을 스킵
250+ Cocktail c1 = selected .size () > 0 ? selected .get (0 ) : null ;
251+ Cocktail c2 = selected .size () > 1 ? selected .get (1 ) : null ;
252+ Cocktail c3 = selected .size () > 2 ? selected .get (2 ) : null ;
253+ Cocktail c4 = selected .size () > 3 ? selected .get (3 ) : null ;
288254
289255 // A: c1(now-2d), c2(now-1d)
290- myBarService .keep (userA .getId (), c1 .getId ());
291- myBarService .keep (userA .getId (), c2 .getId ());
292- myBarRepository .findByUser_IdAndCocktail_Id (userA .getId (), c1 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (2 )));
293- myBarRepository .findByUser_IdAndCocktail_Id (userA .getId (), c2 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (1 )));
256+ if (c1 != null ) {
257+ myBarService .keep (userA .getId (), c1 .getId ());
258+ myBarRepository .findByUser_IdAndCocktail_Id (userA .getId (), c1 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (2 )));
259+ }
260+ if (c2 != null ) {
261+ myBarService .keep (userA .getId (), c2 .getId ());
262+ myBarRepository .findByUser_IdAndCocktail_Id (userA .getId (), c2 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (1 )));
263+ }
294264
295- if (userB != null && !userB .isDeleted ()) {
265+ // B: c3 keep 후 unkeep -> DELETED
266+ if (c3 != null ) {
296267 myBarService .keep (userB .getId (), c3 .getId ());
297268 myBarService .unkeep (userB .getId (), c3 .getId ());
298269 }
299270
300271 // C: c2(now-3d), c3(now-2d), c4(now-1h)
301- myBarService .keep (userC .getId (), c2 .getId ());
302- myBarService .keep (userC .getId (), c3 .getId ());
303- myBarService .keep (userC .getId (), c4 .getId ());
304- myBarRepository .findByUser_IdAndCocktail_Id (userC .getId (), c2 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (3 )));
305- myBarRepository .findByUser_IdAndCocktail_Id (userC .getId (), c3 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (2 )));
306- myBarRepository .findByUser_IdAndCocktail_Id (userC .getId (), c4 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusHours (1 )));
272+ if (c2 != null ) {
273+ myBarService .keep (userC .getId (), c2 .getId ());
274+ myBarRepository .findByUser_IdAndCocktail_Id (userC .getId (), c2 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (3 )));
275+ }
276+ if (c3 != null ) {
277+ myBarService .keep (userC .getId (), c3 .getId ());
278+ myBarRepository .findByUser_IdAndCocktail_Id (userC .getId (), c3 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusDays (2 )));
279+ }
280+ if (c4 != null ) {
281+ myBarService .keep (userC .getId (), c4 .getId ());
282+ myBarRepository .findByUser_IdAndCocktail_Id (userC .getId (), c4 .getId ()).ifPresent (m -> m .setKeptAt (java .time .LocalDateTime .now ().minusHours (1 )));
283+ }
307284 }
308285}
0 commit comments