2
I have a service that before saving some value in the bank, it is necessary to be checked if it is within some period 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 the logic implemented, but I wanted help to understand why is not entering the if
when I send the same date 2 times or for ex 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’ve tried several if
's, but nothing fall into the exception. As for example:
if (key.getStartTime().getTime() >= calendar.getStartTime().getTime()
&& key.getEndTime().getTime() <= calendar.getStartTime().getTime())
On the console 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 supposed to be the exception because the dates are the same. I ask for help in this logic and if anyone has any other logic or suggestion, please tell me.
I did not quite understand the logic. If the periods are 10:00, 10:30 and 11:00, should only accept these times? (so 10:31 is not accepted?) And in your example, the times are 20:00 and 20:30, I’m not understanding what are the criteria to accept a value as valid or make an exception. Another detail is that method
convertTime
is not converting anything (compare the value ofdate.getTime()
before and after the method and see that in fact it changes nothing - better, see thatconvertTime(date).equals(date)
returns true, i.e., the method is returning anotherDate
of the same value)– hkotsubo
That 10:00, 10:30 and 11:00 was just one example. The rule would be like this, if I make a registration at 10:30, the system will add + 30 min and make this time unavailable for another registration. In other words, from 10:30 to 11:00 it is not possible to register + nd within this range, example would be 10:31. Without convertTime the date was in a +/- format like this: "2018-05-07T20:31:00" and there was an error in If, because of that I had to use this resource. You’ve come to understand the agr logic, my friend?
– Crane
Picked up here, without the convertTime returns 2018-05-07 20:00.0.
– Crane