@@ -3,6 +3,11 @@ package ru.soune.nocopy.handler;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.soune.nocopy.dto.BaseRequest;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
@@ -10,11 +15,14 @@ import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.MessageCode;
|
||||
import ru.soune.nocopy.dto.user.UserAdditionalInfoBody;
|
||||
|
||||
import ru.soune.nocopy.entity.user.User;
|
||||
import ru.soune.nocopy.entity.user.UserAdditionalInfo;
|
||||
import ru.soune.nocopy.entity.user.UserAdditionalInfoAnswer;
|
||||
import ru.soune.nocopy.entity.user.UserAdditionalInfoListAnswer;
|
||||
import ru.soune.nocopy.exception.UserAdditionalInfoNotFound;
|
||||
import ru.soune.nocopy.repository.UserRepository;
|
||||
import ru.soune.nocopy.service.user.AdditionalInfoService;
|
||||
import ru.soune.nocopy.service.user.UserSpecifications;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
@@ -28,8 +36,11 @@ import java.util.stream.Collectors;
|
||||
public class UserInfoHandler implements RequestHandler {
|
||||
|
||||
private final AdditionalInfoService additionalInfoService;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public BaseResponse handle(BaseRequest request) throws Exception {
|
||||
try {
|
||||
@@ -40,6 +51,7 @@ public class UserInfoHandler implements RequestHandler {
|
||||
|
||||
return switch (body.getAction()) {
|
||||
case "getAll" -> getAllAdditionalInfos(request);
|
||||
case "getAllWithPagination" -> getAllAdditionalInfoWithPagination(request, body);
|
||||
case "getById" -> getAdditionalInfoById(request, body.getUserId());
|
||||
case "update" -> updateAdditionalInfo(request, body);
|
||||
case "delete" -> deleteAdditionalInfo(request, body.getUserId());
|
||||
@@ -96,6 +108,44 @@ public class UserInfoHandler implements RequestHandler {
|
||||
);
|
||||
}
|
||||
|
||||
private BaseResponse getAllAdditionalInfoWithPagination(BaseRequest request, UserAdditionalInfoBody req) {
|
||||
Sort.Direction direction =
|
||||
Sort.Direction.fromString(req.getSortDirection() != null ? req.getSortDirection() : "desc");
|
||||
String sortBy = req.getSortBy() != null ? req.getSortBy() : "createdAt";
|
||||
|
||||
Pageable pageable = PageRequest.of(req.getPage(), req.getSize(), Sort.by(direction, sortBy));
|
||||
|
||||
Specification<User> spec = (root, query, cb) -> cb.conjunction();
|
||||
|
||||
if (req.getFullName() != null) {
|
||||
spec = spec.and(UserSpecifications.byFullName(req.getFullName()));
|
||||
}
|
||||
if (req.getEmail() != null) {
|
||||
spec = spec.and(UserSpecifications.byEmail(req.getEmail()));
|
||||
}
|
||||
if (req.getIsActive() != null) {
|
||||
spec = spec.and(UserSpecifications.byIsActive(req.getIsActive()));
|
||||
}
|
||||
if (req.getEmailVerified() != null) {
|
||||
spec = spec.and(UserSpecifications.byEmailVerified(req.getEmailVerified()));
|
||||
}
|
||||
if (req.getSubscriptionType() != null) {
|
||||
spec = spec.and(UserSpecifications.bySubscriptionType(req.getSubscriptionType()));
|
||||
}
|
||||
if (req.getDateFrom() != null || req.getDateTo() != null) {
|
||||
spec = spec.and(UserSpecifications.byCreatedAtRange(req.getDateFrom(), req.getDateTo()));
|
||||
}
|
||||
|
||||
Page<User> allUsers = userRepository.findAll(spec, pageable);
|
||||
|
||||
return new BaseResponse(
|
||||
request.getMsgId(),
|
||||
MessageCode.SUCCESS.getCode(),
|
||||
MessageCode.SUCCESS.getDescription(),
|
||||
allUsers);
|
||||
}
|
||||
|
||||
|
||||
private BaseResponse getAdditionalInfoById(BaseRequest request, Long userId) {
|
||||
log.info("Getting additional info for user: {}", userId);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user