|
| 1 | +package com.craftpilot.userservice.controller; |
| 2 | + |
| 3 | +import com.craftpilot.userservice.dto.ai.ModelResponse; |
| 4 | +import com.craftpilot.userservice.service.AIModelService; |
| 5 | +import com.craftpilot.userservice.service.UserService; |
| 6 | +import io.swagger.v3.oas.annotations.Operation; |
| 7 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.security.core.Authentication; |
| 12 | +import org.springframework.security.core.context.ReactiveSecurityContextHolder; |
| 13 | +import org.springframework.security.core.context.SecurityContext; |
| 14 | +import org.springframework.web.bind.annotation.GetMapping; |
| 15 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 16 | +import org.springframework.web.bind.annotation.RestController; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | + |
| 19 | +@RestController |
| 20 | +@RequestMapping("/api/v1/ai") |
| 21 | +@RequiredArgsConstructor |
| 22 | +@Slf4j |
| 23 | +@Tag(name = "AI Models", description = "AI model yönetimi API'leri") |
| 24 | +public class AIModelController { |
| 25 | + |
| 26 | + private final AIModelService modelService; |
| 27 | + private final UserService userService; |
| 28 | + |
| 29 | + @GetMapping("/models/available") |
| 30 | + @Operation(summary = "Kullanıcıya uygun AI modellerini getir", description = "Kullanıcının planına göre erişebileceği tüm AI modellerini getirir") |
| 31 | + public Mono<ResponseEntity<ModelResponse>> getAvailableModels() { |
| 32 | + return ReactiveSecurityContextHolder.getContext() |
| 33 | + .map(SecurityContext::getAuthentication) |
| 34 | + .map(Authentication::getName) |
| 35 | + .flatMap(userId -> { |
| 36 | + log.info("Kullanıcı için erişilebilir AI modelleri getiriliyor: userId={}", userId); |
| 37 | + return userService.getUserPlan(userId) |
| 38 | + .flatMap(userPlan -> |
| 39 | + userService.getUserPreferences(userId) |
| 40 | + .flatMap(preferences -> { |
| 41 | + // Son seçilen model yoksa varsayılan değer kullan |
| 42 | + final String lastSelectedModel = preferences.getLastSelectedModelId() != null ? |
| 43 | + preferences.getLastSelectedModelId() : |
| 44 | + modelService.getDefaultModelForPlan(userPlan); |
| 45 | + |
| 46 | + return modelService.getAvailableModels(userPlan) |
| 47 | + .map(modelsData -> { |
| 48 | + ModelResponse response = ModelResponse.builder() |
| 49 | + .models(modelsData.getModels()) |
| 50 | + .providers(modelsData.getProviders()) |
| 51 | + .defaultModelId(modelService.getDefaultModelForPlan(userPlan)) |
| 52 | + .userPlan(userPlan) |
| 53 | + .lastSelectedModelId(lastSelectedModel) |
| 54 | + .version(modelsData.getVersion()) |
| 55 | + .build(); |
| 56 | + return ResponseEntity.ok(response); |
| 57 | + }); |
| 58 | + }) |
| 59 | + ) |
| 60 | + .doOnSuccess(response -> log.info("AI modelleri başarıyla getirildi: userId={}", userId)) |
| 61 | + .doOnError(e -> log.error("AI modelleri getirilirken hata: userId={}, error={}", userId, e.getMessage())); |
| 62 | + }); |
| 63 | + } |
| 64 | +} |
0 commit comments