Untitled
public List<InboundResponseDTO> getInboundDetailsByDate(LocalDateTime dateTime) throws AppException, InvalidArgumentException { // Convert to LocalDate to compare without time LocalDate dateOnly = dateTime.toLocalDate(); // Fetching all inbound details based on the provided date range List<InboundDetails> inboundDetailsList = inboundDetailsRepo.findByCreatedAtBetween( dateOnly.atStartOfDay(), dateOnly.plusDays(1).atStartOfDay()); // Mapping the inbound details to DTO List<InboundingDTO> inboundingDTOList = inboundDetailsList.stream() .map(inboundDetails -> { InboundingDTO dto = modelMapper.map(inboundDetails, InboundingDTO.class); return mapRelatedEntitiesToDTO(inboundDetails, dto); }).collect(Collectors.toList()); // Grouping the InboundingDTO list by Sauda ID Map<Long, List<InboundingDTO>> groupedBySaudaId = inboundingDTOList.stream() .collect(Collectors.groupingBy(InboundingDTO::getSaudaId)); // Preparing the response list of InboundResponseDTO based on Sauda ID grouping List<InboundResponseDTO> responseList = groupedBySaudaId.entrySet().stream() .map(entry -> { SaudaBookDTO saudaBookDTO = null; try { saudaBookDTO = saudaBookRepo.findById(entry.getKey()) .map(saudaBook -> modelMapper.map(saudaBook, SaudaBookDTO.class)) .orElseThrow(() -> { try { return new EntityNotFoundException( massagesUtil.languageProcess("InboundService.getInboundDetailsByDate.SaudaNotFound.message", null) ); } catch (InvalidArgumentException e) { throw new RuntimeException(e); } }); } catch (EntityNotFoundException e) { throw new RuntimeException(e); } return new InboundResponseDTO(saudaBookDTO, entry.getValue()); }).collect(Collectors.toList()); return responseList; }
Leave a Comment