diff --git a/explore/src/main/java/ru/practicum/explore/stat/StatDataService.java b/explore/src/main/java/ru/practicum/explore/stat/StatDataService.java index d112940..316e84a 100644 --- a/explore/src/main/java/ru/practicum/explore/stat/StatDataService.java +++ b/explore/src/main/java/ru/practicum/explore/stat/StatDataService.java @@ -14,7 +14,6 @@ import org.springframework.web.util.DefaultUriBuilderFactory; import ru.practicum.dto.statdata.StatData; -import java.time.LocalDateTime; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,13 +45,16 @@ private List getStat(Map parameters) { }, parameters ); + if (!responseEntity.getStatusCode().is2xxSuccessful()) { + throw new RuntimeException("error saving the log"); + } return responseEntity.getBody(); } public Integer getRequestHits(String requestURI) { Map parameters = new HashMap<>(); - parameters.put("start", LocalDateTime.now().minusMonths(1)); - parameters.put("end", LocalDateTime.now().plusMonths(1)); + parameters.put("start", null); + parameters.put("end", null); parameters.put("unique", true); parameters.put("uris", requestURI); diff --git a/statistic/server/src/main/java/ru/practicum/server/handler/ErrorHandler.java b/statistic/server/src/main/java/ru/practicum/server/handler/ErrorHandler.java new file mode 100644 index 0000000..4d60ea2 --- /dev/null +++ b/statistic/server/src/main/java/ru/practicum/server/handler/ErrorHandler.java @@ -0,0 +1,16 @@ +package ru.practicum.server.handler; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ErrorHandler { + + @ExceptionHandler(ValidationException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public String handleValidationException(final ValidationException e) { + return e.getMessage(); + } +} diff --git a/statistic/server/src/main/java/ru/practicum/server/handler/ValidationException.java b/statistic/server/src/main/java/ru/practicum/server/handler/ValidationException.java new file mode 100644 index 0000000..a6fe2fd --- /dev/null +++ b/statistic/server/src/main/java/ru/practicum/server/handler/ValidationException.java @@ -0,0 +1,7 @@ +package ru.practicum.server.handler; + +public class ValidationException extends RuntimeException { + public ValidationException(String message) { + super(message); + } +} diff --git a/statistic/server/src/main/java/ru/practicum/server/sevice/EventService.java b/statistic/server/src/main/java/ru/practicum/server/sevice/EventService.java index 698589b..f2677b4 100644 --- a/statistic/server/src/main/java/ru/practicum/server/sevice/EventService.java +++ b/statistic/server/src/main/java/ru/practicum/server/sevice/EventService.java @@ -20,11 +20,10 @@ public LogEvent save(LogEvent data) { } public List findByParams(LocalDateTime start, LocalDateTime end, List uris, boolean unique) { - if (start == null || end == null) { - throw new ValidationException("date is null"); - } - if (end.isAfter(start)) { - throw new ValidationException("end date is after start date"); + if (start != null && end != null) { + if (start.isAfter(end)) { + throw new ValidationException("start date is after end date "); + } } if (unique) { diff --git a/statistic/server/src/main/java/ru/practicum/server/storage/ServerRepository.java b/statistic/server/src/main/java/ru/practicum/server/storage/ServerRepository.java index af8c863..39ec4a3 100644 --- a/statistic/server/src/main/java/ru/practicum/server/storage/ServerRepository.java +++ b/statistic/server/src/main/java/ru/practicum/server/storage/ServerRepository.java @@ -27,7 +27,7 @@ List findByParams(@Param("start") LocalDateTime start, SELECT e.app, e.uri, COUNT(distinct e.ip) AS hits FROM log_events e WHERE e.timestamp BETWEEN cast(:start AS timestamp) AND cast(:end AS timestamp) - AND (:uris is null or e.uri IN (:uris)) + AND coalesce(:uris, null) is null or e.uri IN (:uris) GROUP BY e.uri, e.app ORDER BY hits DESC""", nativeQuery = true) List findByParamsUniqueIp(@Param("start") LocalDateTime start,