我的服务上有几个方法可以调用不同的Spring Data自动存储库方法。我该如何重构这段代码?它闻到无处不在!
public List<SupportMessages> findByProduct(Product product, Status status) {
Date date = new Date();
switch(status) {
case EXPIRED:
return supportMessageRepository.findByProductAndDateAfter(product, date);
case VALID:
return supportMessageRepository.findByProductAndDateBefore(product, date);
case ALL:
return supportMessageRepository.findByProduct(product);
}
public List<SupportMessages> findByRegion(Region region, Status status) {
Date date = new Date();
switch(status) {
case EXPIRED:
return supportMessageRepository.findByRegionAndDateAfter(region, date);
case VALID:
return supportMessageRepository.findByRegionAndDateBefore(region, date);
case ALL:
return supportMessageRepository.findByRegion(region);
}
public List<SupportMessages> findByClient(Client client, Status status) {
Date date = new Date();
switch(status) {
case EXPIRED:
return supportMessageRepository.findByClientAndDateAfter(client, date);
case VALID:
return supportMessageRepository.findByClientAndDateBefore(client, date);
case ALL:
return supportMessageRepository.findByClient(client);
}