From 8c7dcf33f98e8e5b3cbe936477c3f109846e4147 Mon Sep 17 00:00:00 2001 From: Muvvekk Date: Fri, 5 Sep 2025 15:56:42 +0300 Subject: [PATCH 1/3] chore: create statistics controller --- .../statistics/StatisticsController.java | 52 +++++++++++++++++++ .../features/statistics/StatisticsFormat.java | 6 +++ .../statistics/StatisticsService.java | 6 +++ 3 files changed, 64 insertions(+) create mode 100644 src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java create mode 100644 src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsFormat.java create mode 100644 src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsService.java diff --git a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java new file mode 100644 index 0000000..207298f --- /dev/null +++ b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java @@ -0,0 +1,52 @@ +package lv.ctco.springboottemplate.features.statistics; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDate; + +@RestController +@RequestMapping("/api/statistics") +@Tag(name = "Statistics Controller", description = "Statistics endpoints") +public class StatisticsController { + private final StatisticsService statisticsService; + + public StatisticsController(StatisticsService statisticsService) { + this.statisticsService = statisticsService; + } + + @GetMapping + @Operation(summary = "Get Insights About Todo Items") + public Boolean getInsightsAboutTodoItems( + @RequestParam(required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @io.swagger.v3.oas.annotations.Parameter( + description = "2025-01-01", + schema = @io.swagger.v3.oas.annotations.media.Schema(type = "string", pattern = "\\d{4}-\\d{2}-\\d{2}", example = "2025-01-01") + ) LocalDate from, + @RequestParam(required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @io.swagger.v3.oas.annotations.Parameter( + description = "2025-01-31", + schema = @io.swagger.v3.oas.annotations.media.Schema(type = "string", pattern = "\\d{4}-\\d{2}-\\d{2}", example = "2025-01-31") + ) LocalDate to, + @RequestParam(required = true) String format + ) { + if (from.isAfter(to)) { + throw new org.springframework.web.server.ResponseStatusException( + org.springframework.http.HttpStatus.BAD_REQUEST, + "Parameter 'from' must be before or equal to 'to'" + ); + } + + if (!java.util.EnumSet.allOf(StatisticsFormat.class).contains(format)) { + throw new org.springframework.web.server.ResponseStatusException( + org.springframework.http.HttpStatus.BAD_REQUEST, + "Parameter 'format' must be one of " + java.util.Arrays.toString(StatisticsFormat.values()) + ); + } + + return true; + } +} diff --git a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsFormat.java b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsFormat.java new file mode 100644 index 0000000..f7e45b0 --- /dev/null +++ b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsFormat.java @@ -0,0 +1,6 @@ +package lv.ctco.springboottemplate.features.statistics; + +public enum StatisticsFormat { + SUMMARY, + DETAILED +} diff --git a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsService.java b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsService.java new file mode 100644 index 0000000..02bf17b --- /dev/null +++ b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsService.java @@ -0,0 +1,6 @@ +package lv.ctco.springboottemplate.features.statistics; + +import org.springframework.stereotype.Service; + +@Service +public class StatisticsService {} From b69b51d015a7dbc7912efb30715abd94d362d1b4 Mon Sep 17 00:00:00 2001 From: Muvvekk Date: Thu, 11 Sep 2025 23:12:12 +0300 Subject: [PATCH 2/3] chore: update --- .../features/statistics/StatisticsController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java index 207298f..add85d5 100644 --- a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java +++ b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java @@ -31,7 +31,7 @@ public Boolean getInsightsAboutTodoItems( description = "2025-01-31", schema = @io.swagger.v3.oas.annotations.media.Schema(type = "string", pattern = "\\d{4}-\\d{2}-\\d{2}", example = "2025-01-31") ) LocalDate to, - @RequestParam(required = true) String format + @RequestParam(required = true) StatisticsFormat format ) { if (from.isAfter(to)) { throw new org.springframework.web.server.ResponseStatusException( From 47f40742845f005e9f35054dfbe25d36b3bab762 Mon Sep 17 00:00:00 2001 From: Muvvekk Date: Mon, 15 Sep 2025 21:28:31 +0300 Subject: [PATCH 3/3] chore: update --- .../features/statistics/StatisticsController.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java index add85d5..88ab111 100644 --- a/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java +++ b/src/main/java/lv/ctco/springboottemplate/features/statistics/StatisticsController.java @@ -23,16 +23,23 @@ public StatisticsController(StatisticsService statisticsService) { @GetMapping @Operation(summary = "Get Insights About Todo Items") public Boolean getInsightsAboutTodoItems( - @RequestParam(required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @io.swagger.v3.oas.annotations.Parameter( + @RequestParam() @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @io.swagger.v3.oas.annotations.Parameter( description = "2025-01-01", schema = @io.swagger.v3.oas.annotations.media.Schema(type = "string", pattern = "\\d{4}-\\d{2}-\\d{2}", example = "2025-01-01") ) LocalDate from, - @RequestParam(required = true) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @io.swagger.v3.oas.annotations.Parameter( + @RequestParam() @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @io.swagger.v3.oas.annotations.Parameter( description = "2025-01-31", schema = @io.swagger.v3.oas.annotations.media.Schema(type = "string", pattern = "\\d{4}-\\d{2}-\\d{2}", example = "2025-01-31") ) LocalDate to, @RequestParam(required = true) StatisticsFormat format ) { + if (from == null || to == null) { + throw new org.springframework.web.server.ResponseStatusException( + org.springframework.http.HttpStatus.BAD_REQUEST, + "At least one of the required parameters 'from' or 'to' is missing" + ); + } + if (from.isAfter(to)) { throw new org.springframework.web.server.ResponseStatusException( org.springframework.http.HttpStatus.BAD_REQUEST,