|
| 1 | +package devkor.ontime_back.controller; |
| 2 | + |
| 3 | +import devkor.ontime_back.dto.AnalyticsPreferenceResponseDto; |
| 4 | +import devkor.ontime_back.dto.AnalyticsPreferenceUpdateDto; |
| 5 | +import devkor.ontime_back.response.ApiResponseForm; |
| 6 | +import devkor.ontime_back.service.AnalyticsPreferenceService; |
| 7 | +import devkor.ontime_back.service.UserAuthService; |
| 8 | +import io.swagger.v3.oas.annotations.Operation; |
| 9 | +import io.swagger.v3.oas.annotations.media.Content; |
| 10 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 11 | +import io.swagger.v3.oas.annotations.responses.ApiResponse; |
| 12 | +import io.swagger.v3.oas.annotations.responses.ApiResponses; |
| 13 | +import jakarta.servlet.http.HttpServletRequest; |
| 14 | +import jakarta.validation.Valid; |
| 15 | +import lombok.RequiredArgsConstructor; |
| 16 | +import org.springframework.http.HttpStatus; |
| 17 | +import org.springframework.http.ResponseEntity; |
| 18 | +import org.springframework.web.bind.annotation.GetMapping; |
| 19 | +import org.springframework.web.bind.annotation.PutMapping; |
| 20 | +import org.springframework.web.bind.annotation.RequestBody; |
| 21 | +import org.springframework.web.bind.annotation.RestController; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequiredArgsConstructor |
| 25 | +public class AnalyticsPreferenceController { |
| 26 | + |
| 27 | + private final UserAuthService userAuthService; |
| 28 | + private final AnalyticsPreferenceService analyticsPreferenceService; |
| 29 | + |
| 30 | + @Operation(summary = "Get current user's analytics preference") |
| 31 | + @ApiResponses(value = { |
| 32 | + @ApiResponse(responseCode = "200", description = "Analytics preference lookup succeeded", content = @Content( |
| 33 | + mediaType = "application/json", |
| 34 | + schema = @Schema(example = "{\n \"status\": \"success\",\n \"code\": 200,\n \"message\": \"OK\",\n \"data\": {\n \"enabled\": false,\n \"updatedAt\": \"2026-05-26T12:00:00Z\"\n }\n}") |
| 35 | + )), |
| 36 | + @ApiResponse(responseCode = "4XX", description = "Analytics preference lookup failed", content = @Content(mediaType = "application/json", schema = @Schema(example = "Failure message"))) |
| 37 | + }) |
| 38 | + @GetMapping("/users/me/analytics-preference") |
| 39 | + public ResponseEntity<ApiResponseForm<AnalyticsPreferenceResponseDto>> getAnalyticsPreference(HttpServletRequest request) { |
| 40 | + Long userId = userAuthService.getUserIdFromToken(request); |
| 41 | + return ResponseEntity.status(HttpStatus.OK) |
| 42 | + .body(ApiResponseForm.success(analyticsPreferenceService.getAnalyticsPreference(userId))); |
| 43 | + } |
| 44 | + |
| 45 | + @Operation( |
| 46 | + summary = "Update current user's analytics preference", |
| 47 | + requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody( |
| 48 | + description = "Account-scoped analytics preference update.", |
| 49 | + required = true, |
| 50 | + content = @Content(schema = @Schema( |
| 51 | + type = "object", |
| 52 | + example = "{\"enabled\": false}" |
| 53 | + )) |
| 54 | + ) |
| 55 | + ) |
| 56 | + @ApiResponses(value = { |
| 57 | + @ApiResponse(responseCode = "200", description = "Analytics preference update succeeded", content = @Content( |
| 58 | + mediaType = "application/json", |
| 59 | + schema = @Schema(example = "{\n \"status\": \"success\",\n \"code\": 200,\n \"message\": \"OK\",\n \"data\": {\n \"enabled\": false,\n \"updatedAt\": \"2026-05-26T12:00:05Z\"\n }\n}") |
| 60 | + )), |
| 61 | + @ApiResponse(responseCode = "4XX", description = "Analytics preference update failed", content = @Content(mediaType = "application/json", schema = @Schema(example = "Failure message"))) |
| 62 | + }) |
| 63 | + @PutMapping("/users/me/analytics-preference") |
| 64 | + public ResponseEntity<ApiResponseForm<AnalyticsPreferenceResponseDto>> updateAnalyticsPreference( |
| 65 | + HttpServletRequest request, |
| 66 | + @Valid @RequestBody AnalyticsPreferenceUpdateDto requestDto) { |
| 67 | + Long userId = userAuthService.getUserIdFromToken(request); |
| 68 | + return ResponseEntity.status(HttpStatus.OK) |
| 69 | + .body(ApiResponseForm.success(analyticsPreferenceService.updateAnalyticsPreference(userId, requestDto))); |
| 70 | + } |
| 71 | +} |
0 commit comments