1111import org .springframework .http .HttpStatus ;
1212import org .springframework .http .ResponseEntity ;
1313import org .springframework .web .bind .annotation .*;
14- import org .springframework .web .server .ResponseStatusException ;
1514import reactor .core .publisher .Mono ;
1615
16+ import java .time .Duration ;
17+ import java .util .HashMap ;
1718import java .util .List ;
1819import java .util .Map ;
19- import java .time .Duration ;
2020import java .util .concurrent .TimeoutException ;
2121
2222@ RestController
@@ -50,6 +50,10 @@ public Mono<ResponseEntity<UserPreference>> createUserPreferences(
5050 UserPreference preference = userPreferenceMapper .toEntity (request );
5151 preference .setUserId (userId );
5252
53+ Map <String , Boolean > notificationsMap = new HashMap <>();
54+ notificationsMap .put ("general" , true ); // veya gerekli değer
55+ preference .setNotifications (notificationsMap );
56+
5357 return userPreferenceService .saveUserPreferences (preference )
5458 .map (saved -> ResponseEntity .status (HttpStatus .CREATED ).body (saved ))
5559 .doOnSuccess (response -> log .info ("Kullanıcı tercihleri başarıyla oluşturuldu: userId={}" , userId ))
@@ -65,32 +69,25 @@ public Mono<ResponseEntity<UserPreference>> createUserPreferences(
6569 @ Operation (summary = "Kullanıcı tercihlerini güncelle" , description = "Kullanıcı tercihlerini günceller" )
6670 public Mono <ResponseEntity <UserPreference >> updateUserPreferences (
6771 @ PathVariable String userId ,
68- @ RequestBody UserPreferenceRequest request ) {
72+ @ RequestBody Map < String , Object > updates ) {
6973 log .info ("Kullanıcı tercihleri güncelleniyor: userId={}" , userId );
7074
71- try {
72- UserPreference preference = userPreferenceMapper .toEntity (request );
73- preference .setUserId (userId );
74-
75- return userPreferenceService .saveUserPreferences (preference )
76- .map (ResponseEntity ::ok )
77- // 8 saniyeden 3 saniyeye indiriyoruz - bu değeri application.yml'den alacak şekilde düzenlenebilir
78- .timeout (Duration .ofMillis (1500 ))
79- .doOnSuccess (response -> log .info ("Kullanıcı tercihleri başarıyla güncellendi: userId={}" , userId ))
80- .doOnError (e -> {
81- if (e instanceof TimeoutException ) {
82- log .error ("Kullanıcı tercihleri güncelleme zaman aşımına uğradı: userId={}" , userId );
83- } else {
84- log .error ("Kullanıcı tercihleri güncellenirken hata: userId={}, error={}" , userId , e .getMessage ());
85- }
86- })
87- .onErrorResume (TimeoutException .class , e -> Mono .just (ResponseEntity .status (HttpStatus .ACCEPTED )
88- .body (preference ))) // Timeout durumunda 202 Accepted dönüyoruz
89- .onErrorResume (e -> Mono .just (ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ()));
90- } catch (Exception e ) {
91- log .error ("Kullanıcı tercihleri güncellenirken istisna: userId={}, error={}" , userId , e .getMessage ());
92- return Mono .just (ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ());
93- }
75+ return userPreferenceService .updateUserPreferences (userId , updates )
76+ .timeout (Duration .ofMillis (1500 ))
77+ .map (ResponseEntity ::ok )
78+ .doOnSuccess (response -> log .info ("Kullanıcı tercihleri başarıyla güncellendi: userId={}" , userId ))
79+ .doOnError (e -> {
80+ if (e instanceof TimeoutException ) {
81+ log .warn ("Kullanıcı tercihleri güncelleme zaman aşımına uğradı: userId={}" , userId );
82+ } else {
83+ log .error ("Kullanıcı tercihleri güncellenirken hata: userId={}, error={}" , userId , e .getMessage ());
84+ }
85+ })
86+ .onErrorResume (TimeoutException .class , e -> {
87+ UserPreference preference = createFallbackPreference (userId , updates );
88+ return Mono .just (ResponseEntity .accepted ().body (preference ));
89+ })
90+ .onErrorResume (e -> Mono .just (ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ()));
9491 }
9592
9693 @ DeleteMapping
@@ -141,7 +138,6 @@ public Mono<ResponseEntity<UserPreference>> updateLanguage(
141138 .doOnError (e -> log .error ("Dil güncellenirken hata: userId={}, error={}" , userId , e .getMessage ()))
142139 .onErrorResume (e -> {
143140 log .error ("Dil güncelleme hatası (fallback çalıştırılıyor): {}" , e .getMessage ());
144- // userPreferenceService içindeki fallback çalışmadığında controller seviyesinde fallback
145141 return createFallbackLanguagePreference (userId , language )
146142 .map (fallback -> ResponseEntity .ok (fallback ))
147143 .onErrorReturn (ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).build ());
@@ -181,11 +177,14 @@ public Mono<ResponseEntity<List<String>>> getAiModelFavorites(@PathVariable Stri
181177
182178 // Fallback metotları
183179 private Mono <UserPreference > createFallbackPreference (String userId , UserPreferenceRequest request ) {
180+ Map <String , Boolean > notificationsMap = new HashMap <>();
181+ notificationsMap .put ("general" , true );
182+
184183 UserPreference fallbackPreference = UserPreference .builder ()
185184 .userId (userId )
186185 .theme (request .getTheme () != null ? request .getTheme () : "light" )
187186 .language (request .getLanguage () != null ? request .getLanguage () : "tr" )
188- .notifications (request .getNotifications () != null ? request .getNotifications () : true )
187+ .notifications (request .getNotifications () != null ? request .getNotifications () : notificationsMap )
189188 .pushEnabled (request .getPushEnabled () != null ? request .getPushEnabled () : true )
190189 .aiModelFavorites (request .getAiModelFavorites () != null ? request .getAiModelFavorites () : List .of ())
191190 .createdAt (System .currentTimeMillis ())
@@ -197,11 +196,14 @@ private Mono<UserPreference> createFallbackPreference(String userId, UserPrefere
197196 }
198197
199198 private Mono <UserPreference > createFallbackThemePreference (String userId , String theme ) {
199+ Map <String , Boolean > notificationsMap = new HashMap <>();
200+ notificationsMap .put ("general" , true );
201+
200202 UserPreference fallbackPreference = UserPreference .builder ()
201203 .userId (userId )
202204 .theme (theme )
203205 .language ("tr" )
204- .notifications (true )
206+ .notifications (notificationsMap )
205207 .pushEnabled (true )
206208 .aiModelFavorites (List .of ())
207209 .createdAt (System .currentTimeMillis ())
@@ -213,11 +215,14 @@ private Mono<UserPreference> createFallbackThemePreference(String userId, String
213215 }
214216
215217 private Mono <UserPreference > createFallbackLanguagePreference (String userId , String language ) {
218+ Map <String , Boolean > notificationsMap = new HashMap <>();
219+ notificationsMap .put ("general" , true );
220+
216221 UserPreference fallbackPreference = UserPreference .builder ()
217222 .userId (userId )
218223 .theme ("light" )
219224 .language (language )
220- .notifications (true )
225+ .notifications (notificationsMap )
221226 .pushEnabled (true )
222227 .aiModelFavorites (List .of ())
223228 .createdAt (System .currentTimeMillis ())
@@ -229,11 +234,14 @@ private Mono<UserPreference> createFallbackLanguagePreference(String userId, Str
229234 }
230235
231236 private Mono <UserPreference > createFallbackFavoritesPreference (String userId , List <String > favorites ) {
237+ Map <String , Boolean > notificationsMap = new HashMap <>();
238+ notificationsMap .put ("general" , true );
239+
232240 UserPreference fallbackPreference = UserPreference .builder ()
233241 .userId (userId )
234242 .theme ("light" )
235243 .language ("tr" )
236- .notifications (true )
244+ .notifications (notificationsMap )
237245 .pushEnabled (true )
238246 .aiModelFavorites (favorites )
239247 .createdAt (System .currentTimeMillis ())
@@ -243,4 +251,29 @@ private Mono<UserPreference> createFallbackFavoritesPreference(String userId, Li
243251 log .info ("Fallback favori modeller tercihi oluşturuldu: userId={}" , userId );
244252 return Mono .just (fallbackPreference );
245253 }
254+
255+ // Fallback metodu - zaman aşımı durumunda
256+ private UserPreference createFallbackPreference (String userId , Map <String , Object > updates ) {
257+ UserPreference fallbackPreference = UserPreference .builder ()
258+ .userId (userId )
259+ .theme (updates .containsKey ("theme" ) ? (String )updates .get ("theme" ) : "system" )
260+ .themeSchema (updates .containsKey ("themeSchema" ) ? (String )updates .get ("themeSchema" ) : "default" )
261+ .language (updates .containsKey ("language" ) ? (String )updates .get ("language" ) : "tr" )
262+ .layout (updates .containsKey ("layout" ) ? (String )updates .get ("layout" ) : "collapsibleSide" )
263+ .pushEnabled (updates .containsKey ("pushEnabled" ) ? (Boolean )updates .get ("pushEnabled" ) : false )
264+ .createdAt (System .currentTimeMillis ())
265+ .updatedAt (System .currentTimeMillis ())
266+ .build ();
267+
268+ if (updates .containsKey ("aiModelFavorites" )) {
269+ fallbackPreference .setAiModelFavorites ((List <String >)updates .get ("aiModelFavorites" ));
270+ }
271+
272+ if (updates .containsKey ("notifications" )) {
273+ fallbackPreference .setNotifications ((Map <String , Boolean >)updates .get ("notifications" ));
274+ }
275+
276+ log .info ("Fallback tercihi oluşturuldu: userId={}" , userId );
277+ return fallbackPreference ;
278+ }
246279}
0 commit comments