how to do proper data validation in service? using enum in if condition? and other questions

hey guys. i have a couple of questions about how to better approach some cases. i have this service:
@Service
public class ItemService {
private ItemRepository itemRepository;
public ItemService(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
public List<Item> getItemsByDistanceAndSeason(double distance, String season) {

if (distance<=0) {
System.out.println("Distance should be positive number");
//todo handle
}
if (season.equals("all") || season.equals("winter") || season.equals("spring") || season.equals("summer") || season.equals("autumn")) {

}

return itemRepository.getAllByDistanceAndSeason(distance, season);
}
}
@Service
public class ItemService {
private ItemRepository itemRepository;
public ItemService(ItemRepository itemRepository) {
this.itemRepository = itemRepository;
}
public List<Item> getItemsByDistanceAndSeason(double distance, String season) {

if (distance<=0) {
System.out.println("Distance should be positive number");
//todo handle
}
if (season.equals("all") || season.equals("winter") || season.equals("spring") || season.equals("summer") || season.equals("autumn")) {

}

return itemRepository.getAllByDistanceAndSeason(distance, season);
}
}
so my questions are: 1. would it be better to throw an excepetion (maybe ArithmeticException) if distance isnt positive. or to just return smth else? for some reason this response looks horrible for me. or maybe im just overthinking?
{
"timestamp": "2024-02-04T18:01:31.859+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "java.lang.ArithmeticException: Distance should be positive number\
{
"timestamp": "2024-02-04T18:01:31.859+00:00",
"status": 500,
"error": "Internal Server Error",
"trace": "java.lang.ArithmeticException: Distance should be positive number\
2. would it be better to check the season with this long and cumbersome if, or would it be better to have enum and check in the if condition, if season falls into an enum. for me enum looks like a cleaner option 3. rn in my db i have items where their distance column is 20 and a couple with 1. so if i do a request where distance is 25, i get nothing. and i want to retrieve all the items, that have less or equal distance of my request distance. i want for spring to do smth like select ... from items where distance from request >= distance col val in the record and idk how to do that. i dont even know how to formulate this thing and how to google it. can smb help me out?
1 Reply
JavaBot
JavaBot11mo ago
This post has been reserved for your question.
Hey @bambyzas! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server