I have a service that before saving some value in the bank, it needs to be checked if it is within some time already saved in the bank. Each period consists of half an hour. ex:
10:00
10:30
11:00
If the user tries to put 10:31, an exception must be triggered.
My service is ready and all logic implemented, but I wanted help to understand why it is not entering if
when I send the same date 2 times or by 10:31.
@Service
public class CalendarService {
@Autowired
private CalendarRepository calendarRepository;
public Calendar create(Calendar calendar)
{
calendar.setId(null);
checkSlotBetweenDate(calendar);
addThirtyMinutes(calendar);
calendar.setName("Crane");
return calendarRepository.save(calendar);
}
public void addThirtyMinutes(Calendar calendar)
{
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(calendar.getStartTime());
cal.add(java.util.Calendar.MINUTE, 30);
calendar.setEndTime(cal.getTime());
}
public void checkSlotBetweenDate(Calendar calendar)
{
Iterable<Calendar> cal = calendarRepository.findAll();
for(Calendar key: cal)
{
System.out.println(calendar.getStartTime()+ " Enviado por mim ");
System.out.println(convertTime(key.getStartTime())+ " Dt inicio e fim do Banco " +convertTime(key.getEndTime()));
if(convertTime(key.getStartTime()).after(calendar.getStartTime()) && convertTime(key.getEndTime()).before(calendar.getStartTime()))
{
throw new BadRequestException("O período selecionado já está ocupado!");
}
}
}
public Date convertTime(Date date)
{
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.setTime(date);
return cal.getTime();
}
}
I have tried several if
's, but nothing falls into the exception. For example:
if (key.getStartTime().getTime() >= calendar.getStartTime().getTime()
&& key.getEndTime().getTime() <= calendar.getStartTime().getTime())
In the console it is printed:
Mon May 07 20:00:00 BRT 2018 Enviado por mim
Mon May 07 20:00:00 BRT 2018 Dt inicio e fim do Banco Mon May 07 20:30:00 BRT 2018
And in fact, it was to fall into the exception because the dates are the same. I ask for help in this logic and if someone has another logic or suggestion, please tell me.