@@ -0,0 +1,54 @@
|
||||
package ru.soune.nocopy.handler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.soune.nocopy.dto.BaseRequest;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.search.GlobalSearchStatisticsRequest;
|
||||
import ru.soune.nocopy.dto.search.GlobalSearchStatisticsResponse;
|
||||
import ru.soune.nocopy.entity.user.AuthToken;
|
||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||
import ru.soune.nocopy.service.search.GlobalSearchStatisticsService;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class GlobalSearchHandler implements RequestHandler {
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final GlobalSearchStatisticsService statisticsService;
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
GlobalSearchStatisticsRequest statRequest = objectMapper.convertValue(
|
||||
request.getMessageBody(), GlobalSearchStatisticsRequest.class);
|
||||
|
||||
if (statRequest.getToken() == null) {
|
||||
throw new IllegalArgumentException("User token is required");
|
||||
}
|
||||
|
||||
Optional<AuthToken> tokenOptional =
|
||||
authTokenRepository.findByToken(statRequest.getToken());
|
||||
|
||||
if (tokenOptional.isPresent()) {
|
||||
throw new IllegalArgumentException("User token is required");
|
||||
}
|
||||
|
||||
Long userId = tokenOptional.orElseThrow().getUser().getId();
|
||||
|
||||
GlobalSearchStatisticsResponse statistics = statisticsService.getStatistics(
|
||||
userId, statRequest.getLimit(), statRequest.getDays());
|
||||
|
||||
return BaseResponse.builder()
|
||||
.msgId(request.getMsgId())
|
||||
.messageBody(statistics)
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,14 @@ import ru.soune.nocopy.dto.BaseRequest;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.violation.ViolationStatisticsRequest;
|
||||
import ru.soune.nocopy.dto.violation.ViolationStatisticsResponse;
|
||||
import ru.soune.nocopy.entity.user.AuthToken;
|
||||
import ru.soune.nocopy.repository.AuthTokenRepository;
|
||||
import ru.soune.nocopy.service.violation.ViolationService;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -22,6 +25,8 @@ public class ViolationStatisticsHandler implements RequestHandler {
|
||||
|
||||
private final ViolationService violationService;
|
||||
|
||||
private final AuthTokenRepository authTokenRepository;
|
||||
|
||||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
@Override
|
||||
@@ -43,8 +48,17 @@ public class ViolationStatisticsHandler implements RequestHandler {
|
||||
}
|
||||
}
|
||||
|
||||
Optional<AuthToken> tokenOptional =
|
||||
authTokenRepository.findByToken(statRequest.getToken());
|
||||
|
||||
if (tokenOptional.isPresent()) {
|
||||
throw new IllegalArgumentException("User token is required");
|
||||
}
|
||||
|
||||
Long userId = tokenOptional.orElseThrow().getUser().getId();
|
||||
|
||||
ViolationStatisticsResponse statistics = violationService.getViolationStatistics(
|
||||
statRequest.getUserId(),
|
||||
userId,
|
||||
statRequest.getFileId(),
|
||||
startDate,
|
||||
endDate
|
||||
@@ -57,8 +71,8 @@ public class ViolationStatisticsHandler implements RequestHandler {
|
||||
}
|
||||
|
||||
private void validateRequest(ViolationStatisticsRequest request) {
|
||||
if (request.getUserId() == null) {
|
||||
throw new IllegalArgumentException("User ID is required");
|
||||
if (request.getToken() == null) {
|
||||
throw new IllegalArgumentException("Token is required");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user