Skip to content

Commit

Permalink
fix 8
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasnodoom committed Sep 4, 2024
1 parent b36cc8b commit 1eb2316
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,13 +45,16 @@ private List<StatData> getStat(Map<String, Object> parameters) {
},
parameters
);
if (!responseEntity.getStatusCode().is2xxSuccessful()) {
throw new RuntimeException("error saving the log");
}
return responseEntity.getBody();
}

public Integer getRequestHits(String requestURI) {
Map<String, Object> 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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.practicum.server.handler;

public class ValidationException extends RuntimeException {
public ValidationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public LogEvent save(LogEvent data) {
}

public List<ViewStats> findByParams(LocalDateTime start, LocalDateTime end, List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ List<ViewStats> 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<ViewStats> findByParamsUniqueIp(@Param("start") LocalDateTime start,
Expand Down

0 comments on commit 1eb2316

Please sign in to comment.