@Test void saveExceptionTest() { when(habitStatisticRepo.findHabitStatByDate(addhs.getCreatedOn(), addhs.getHabitId())).thenReturn(Optional.of(new HabitStatistic())); assertThrows(NotSavedException.class, () -> habitStatisticService.save(addhs) ); }
@Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override public AddHabitStatisticDto save(AddHabitStatisticDto dto) { if (habitStatisticRepo.findHabitStatByDate(dto.getCreatedOn(), dto.getHabitId()).isPresent()) { throw new NotSavedException(ErrorMessage.HABIT_STATISTIC_ALREADY_EXISTS); } boolean proceed = isTodayOrYesterday( dateService .convertToDatasourceTimezone(dto.getCreatedOn()) .toLocalDate() ); if (proceed) { HabitStatistic habitStatistic = modelMapper.map(dto, HabitStatistic.class); habitStatistic.setHabit(habitService.getById(dto.getHabitId())); return modelMapper.map(habitStatisticRepo.save(habitStatistic), AddHabitStatisticDto.class); } throw new BadRequestException(ErrorMessage.WRONG_DATE); }
HabitStatisticServiceImpl implements HabitStatisticService { @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override public AddHabitStatisticDto save(AddHabitStatisticDto dto) { if (habitStatisticRepo.findHabitStatByDate(dto.getCreatedOn(), dto.getHabitId()).isPresent()) { throw new NotSavedException(ErrorMessage.HABIT_STATISTIC_ALREADY_EXISTS); } boolean proceed = isTodayOrYesterday( dateService .convertToDatasourceTimezone(dto.getCreatedOn()) .toLocalDate() ); if (proceed) { HabitStatistic habitStatistic = modelMapper.map(dto, HabitStatistic.class); habitStatistic.setHabit(habitService.getById(dto.getHabitId())); return modelMapper.map(habitStatisticRepo.save(habitStatistic), AddHabitStatisticDto.class); } throw new BadRequestException(ErrorMessage.WRONG_DATE); } }
HabitStatisticServiceImpl implements HabitStatisticService { @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override public AddHabitStatisticDto save(AddHabitStatisticDto dto) { if (habitStatisticRepo.findHabitStatByDate(dto.getCreatedOn(), dto.getHabitId()).isPresent()) { throw new NotSavedException(ErrorMessage.HABIT_STATISTIC_ALREADY_EXISTS); } boolean proceed = isTodayOrYesterday( dateService .convertToDatasourceTimezone(dto.getCreatedOn()) .toLocalDate() ); if (proceed) { HabitStatistic habitStatistic = modelMapper.map(dto, HabitStatistic.class); habitStatistic.setHabit(habitService.getById(dto.getHabitId())); return modelMapper.map(habitStatisticRepo.save(habitStatistic), AddHabitStatisticDto.class); } throw new BadRequestException(ErrorMessage.WRONG_DATE); } @Autowired  HabitStatisticServiceImpl(HabitStatisticRepo habitStatisticRepo,
                                     HabitRepo habitRepo,
                                     HabitService habitService, ModelMapper modelMapper,
                                     DateService dateService); }
HabitStatisticServiceImpl implements HabitStatisticService { @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override public AddHabitStatisticDto save(AddHabitStatisticDto dto) { if (habitStatisticRepo.findHabitStatByDate(dto.getCreatedOn(), dto.getHabitId()).isPresent()) { throw new NotSavedException(ErrorMessage.HABIT_STATISTIC_ALREADY_EXISTS); } boolean proceed = isTodayOrYesterday( dateService .convertToDatasourceTimezone(dto.getCreatedOn()) .toLocalDate() ); if (proceed) { HabitStatistic habitStatistic = modelMapper.map(dto, HabitStatistic.class); habitStatistic.setHabit(habitService.getById(dto.getHabitId())); return modelMapper.map(habitStatisticRepo.save(habitStatistic), AddHabitStatisticDto.class); } throw new BadRequestException(ErrorMessage.WRONG_DATE); } @Autowired  HabitStatisticServiceImpl(HabitStatisticRepo habitStatisticRepo,
                                     HabitRepo habitRepo,
                                     HabitService habitService, ModelMapper modelMapper,
                                     DateService dateService); @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override AddHabitStatisticDto save(AddHabitStatisticDto dto); @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override UpdateHabitStatisticDto update(Long habitStatisticId, UpdateHabitStatisticDto dto); @Override HabitStatistic findById(Long id); @Override List<Habit> findAllHabitsByUserId(Long userId); @Override List<Habit> findAllHabitsByStatus(Long userId, Boolean status); @Override List<HabitDto> findAllHabitsAndTheirStatistics(Long id, Boolean status, String language); @Override CalendarUsefulHabitsDto getInfoAboutUserHabits(Long userId); @Override List<HabitStatisticDto> findAllByHabitId(Long habitId); @Cacheable(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, key = "#language") @Override List<HabitItemsAmountStatisticDto> getTodayStatisticsForAllHabitItems(String language); @Override Long getAmountOfHabitsInProgressByUserId(Long id); @Override Long getAmountOfAcquiredHabitsByUserId(Long id); }
HabitStatisticServiceImpl implements HabitStatisticService { @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override public AddHabitStatisticDto save(AddHabitStatisticDto dto) { if (habitStatisticRepo.findHabitStatByDate(dto.getCreatedOn(), dto.getHabitId()).isPresent()) { throw new NotSavedException(ErrorMessage.HABIT_STATISTIC_ALREADY_EXISTS); } boolean proceed = isTodayOrYesterday( dateService .convertToDatasourceTimezone(dto.getCreatedOn()) .toLocalDate() ); if (proceed) { HabitStatistic habitStatistic = modelMapper.map(dto, HabitStatistic.class); habitStatistic.setHabit(habitService.getById(dto.getHabitId())); return modelMapper.map(habitStatisticRepo.save(habitStatistic), AddHabitStatisticDto.class); } throw new BadRequestException(ErrorMessage.WRONG_DATE); } @Autowired  HabitStatisticServiceImpl(HabitStatisticRepo habitStatisticRepo,
                                     HabitRepo habitRepo,
                                     HabitService habitService, ModelMapper modelMapper,
                                     DateService dateService); @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override AddHabitStatisticDto save(AddHabitStatisticDto dto); @Transactional @CacheEvict(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, allEntries = true) @Override UpdateHabitStatisticDto update(Long habitStatisticId, UpdateHabitStatisticDto dto); @Override HabitStatistic findById(Long id); @Override List<Habit> findAllHabitsByUserId(Long userId); @Override List<Habit> findAllHabitsByStatus(Long userId, Boolean status); @Override List<HabitDto> findAllHabitsAndTheirStatistics(Long id, Boolean status, String language); @Override CalendarUsefulHabitsDto getInfoAboutUserHabits(Long userId); @Override List<HabitStatisticDto> findAllByHabitId(Long habitId); @Cacheable(value = CacheConstants.HABIT_ITEM_STATISTIC_CACHE, key = "#language") @Override List<HabitItemsAmountStatisticDto> getTodayStatisticsForAllHabitItems(String language); @Override Long getAmountOfHabitsInProgressByUserId(Long id); @Override Long getAmountOfAcquiredHabitsByUserId(Long id);  }