Skip to content

Commit a383a20

Browse files
committed
user prefences güncelleme yapıldı.
1 parent 75f6659 commit a383a20

8 files changed

Lines changed: 286 additions & 47 deletions

File tree

user-service/src/main/java/com/craftpilot/userservice/controller/UserPreferenceController.java

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import org.springframework.http.HttpStatus;
1212
import org.springframework.http.ResponseEntity;
1313
import org.springframework.web.bind.annotation.*;
14-
import org.springframework.web.server.ResponseStatusException;
1514
import reactor.core.publisher.Mono;
1615

16+
import java.time.Duration;
17+
import java.util.HashMap;
1718
import java.util.List;
1819
import java.util.Map;
19-
import java.time.Duration;
2020
import 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
}

user-service/src/main/java/com/craftpilot/userservice/dto/UserPreferenceRequest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
77
import java.util.List;
8+
import java.util.Map;
89

910
@Data
1011
@Builder
@@ -13,7 +14,9 @@
1314
public class UserPreferenceRequest {
1415
private String theme;
1516
private String language;
16-
private Boolean notifications;
17+
private String themeSchema;
18+
private String layout;
19+
private Map<String, Boolean> notifications;
1720
private Boolean pushEnabled;
1821
private List<String> aiModelFavorites;
1922
}

user-service/src/main/java/com/craftpilot/userservice/dto/UserPreferenceResponse.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import lombok.Builder;
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
7+
import java.util.List;
8+
import java.util.Map;
79

810
@Data
911
@Builder
@@ -13,8 +15,11 @@ public class UserPreferenceResponse {
1315
private String userId;
1416
private String theme;
1517
private String language;
16-
private boolean notifications;
17-
private boolean pushEnabled;
18+
private String themeSchema;
19+
private String layout;
20+
private Map<String, Boolean> notifications;
21+
private Boolean pushEnabled;
22+
private List<String> aiModelFavorites;
1823
private Long createdAt;
1924
private Long updatedAt;
2025
}

user-service/src/main/java/com/craftpilot/userservice/mapper/UserPreferenceMapper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.springframework.stereotype.Component;
66

77
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.Map;
810

911
@Component
1012
public class UserPreferenceMapper {
@@ -16,7 +18,9 @@ public UserPreference toEntity(UserPreferenceRequest dto) {
1618

1719
return UserPreference.builder()
1820
.theme(dto.getTheme())
21+
.themeSchema(dto.getThemeSchema())
1922
.language(dto.getLanguage())
23+
.layout(dto.getLayout())
2024
.notifications(dto.getNotifications())
2125
.pushEnabled(dto.getPushEnabled())
2226
.aiModelFavorites(dto.getAiModelFavorites())
@@ -30,7 +34,9 @@ public UserPreferenceRequest toDto(UserPreference entity) {
3034

3135
return UserPreferenceRequest.builder()
3236
.theme(entity.getTheme())
37+
.themeSchema(entity.getThemeSchema())
3338
.language(entity.getLanguage())
39+
.layout(entity.getLayout())
3440
.notifications(entity.getNotifications())
3541
.pushEnabled(entity.getPushEnabled())
3642
.aiModelFavorites(entity.getAiModelFavorites())

user-service/src/main/java/com/craftpilot/userservice/model/UserPreference.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,53 @@
44
import lombok.Builder;
55
import lombok.Data;
66
import lombok.NoArgsConstructor;
7+
import org.springframework.data.annotation.Id;
8+
import org.springframework.data.annotation.TypeAlias;
9+
import com.google.cloud.firestore.annotation.DocumentId;
10+
11+
import java.util.ArrayList;
12+
import java.util.HashMap;
713
import java.util.List;
14+
import java.util.Map;
815

916
@Data
10-
@Builder
1117
@NoArgsConstructor
1218
@AllArgsConstructor
19+
@Builder
20+
@TypeAlias("userPreference")
1321
public class UserPreference {
22+
@Id
23+
@DocumentId
1424
private String userId;
15-
private String theme;
16-
private String language;
17-
private Boolean notifications;
18-
private Boolean pushEnabled;
19-
private List<String> aiModelFavorites; // Favori modeller için yeni alan
25+
26+
// Tema modu (light, dark, system)
27+
@Builder.Default
28+
private String theme = "system";
29+
30+
// Tema şeması (default, dark, green, purple, orange)
31+
@Builder.Default
32+
private String themeSchema = "default";
33+
34+
// Kullanıcının tercih ettiği dil
35+
@Builder.Default
36+
private String language = "en";
37+
38+
// Layout tercihi (collapsibleSide, framelessSide)
39+
@Builder.Default
40+
private String layout = "collapsibleSide";
41+
42+
// Kullanıcının favori AI modelleri
43+
@Builder.Default
44+
private List<String> aiModelFavorites = new ArrayList<>();
45+
46+
// Bildirim tercihleri
47+
@Builder.Default
48+
private Map<String, Boolean> notifications = new HashMap<>();
49+
50+
@Builder.Default
51+
private Boolean pushEnabled = false;
52+
53+
// Zaman damgaları
2054
private Long createdAt;
2155
private Long updatedAt;
2256
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.craftpilot.userservice.repository;
2+
3+
import com.craftpilot.userservice.model.UserPreference;
4+
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface UserPreferenceRepository extends ReactiveCrudRepository<UserPreference, String> {
9+
// Temel CRUD işlemleri ReactiveCrudRepository tarafından sağlanıyor
10+
}

user-service/src/main/java/com/craftpilot/userservice/service/RedisCacheService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import reactor.core.publisher.Mono;
1010

1111
import java.time.Duration;
12+
import java.util.HashMap;
1213

1314
/**
1415
* Redis önbellekleme işlemlerini yöneten servis.
@@ -53,7 +54,11 @@ public Mono<UserPreference> getUserPreferences(String userId) {
5354
.userId(userId)
5455
.theme("light")
5556
.language("tr")
56-
.notifications(true)
57+
.layout("collapsibleSide")
58+
// Map<String, Boolean> oluştur
59+
.notifications(new HashMap<String, Boolean>() {{
60+
put("general", true);
61+
}})
5762
.pushEnabled(true)
5863
.createdAt(System.currentTimeMillis())
5964
.updatedAt(System.currentTimeMillis())

0 commit comments

Comments
 (0)