55 lines
1.9 KiB
Java
55 lines
1.9 KiB
Java
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();
|
|
}
|
|
}
|