|
| 1 | +package com.zenfulcode.commercify.api.system; |
| 2 | + |
| 3 | +import com.zenfulcode.commercify.api.system.dto.MetricsRequest; |
| 4 | +import com.zenfulcode.commercify.api.system.dto.MetricsResponse; |
| 5 | +import com.zenfulcode.commercify.metrics.application.MetricsApplicationService; |
| 6 | +import com.zenfulcode.commercify.metrics.application.dto.MetricsQuery; |
| 7 | +import com.zenfulcode.commercify.shared.interfaces.ApiResponse; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.format.annotation.DateTimeFormat; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.security.access.prepost.PreAuthorize; |
| 13 | +import org.springframework.validation.annotation.Validated; |
| 14 | +import org.springframework.web.bind.annotation.*; |
| 15 | + |
| 16 | +import java.time.LocalDate; |
| 17 | + |
| 18 | +@Slf4j |
| 19 | +@RestController |
| 20 | +@RequestMapping("/api/v2/metrics") |
| 21 | +@RequiredArgsConstructor |
| 22 | +public class MetricsController { |
| 23 | + |
| 24 | + private final MetricsApplicationService metricsService; |
| 25 | + |
| 26 | + @GetMapping |
| 27 | + @PreAuthorize("hasRole('ADMIN')") |
| 28 | + public ResponseEntity<ApiResponse<MetricsResponse>> getMetrics( |
| 29 | + @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate, |
| 30 | + @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate, |
| 31 | + @RequestParam(required = false) Integer lastDays) { |
| 32 | + |
| 33 | + log.info("Getting metrics"); |
| 34 | + |
| 35 | + MetricsRequest request = new MetricsRequest(startDate, endDate, lastDays); |
| 36 | + |
| 37 | + // Handle either date range or "last X days" |
| 38 | + if (lastDays != null) { |
| 39 | + request.setLastDays(lastDays); |
| 40 | + } else { |
| 41 | + request.setStartDate(startDate); |
| 42 | + request.setEndDate(endDate); |
| 43 | + } |
| 44 | + |
| 45 | + final MetricsQuery metricsQuery = MetricsQuery.of(request); |
| 46 | + |
| 47 | + MetricsResponse metrics = metricsService.getMetrics(metricsQuery); |
| 48 | + return ResponseEntity.ok(ApiResponse.success(metrics)); |
| 49 | + } |
| 50 | + |
| 51 | + @PostMapping |
| 52 | + @PreAuthorize("hasRole('ADMIN')") |
| 53 | + public ResponseEntity<ApiResponse<MetricsResponse>> getMetricsWithFilters( |
| 54 | + @Validated @RequestBody MetricsRequest request) { |
| 55 | + |
| 56 | + final MetricsQuery metricsQuery = MetricsQuery.of(request); |
| 57 | + |
| 58 | + MetricsResponse metrics = metricsService.getMetrics(metricsQuery); |
| 59 | + return ResponseEntity.ok(ApiResponse.success(metrics)); |
| 60 | + } |
| 61 | +} |
0 commit comments