I have an endpoint in my RestAPI which can receive 3 optional parameters and then find information in the database with these parameters, also it is possible to combine the parameters. So, in my controller I take a Map<String, Object> which contains these parameters and then pass it to the service level, where I check which parameters were passed and then I call a specific DAO method, depends on which combination of parameters do I have. Params A and B are of type String, and C is a Map. So in the service level it looks like this:
public Set<ResultEntry> getByParameters(Map<String, Object> query) {
Set<ResultEntry> result = new HashSet<>();
if (query.containsKey("a") && query.containsKey("b") && query.containsKey("c")) {
result.addAll(myDao.getByABC(
(String) query.get("a"), (String) query.get("b"),
(Map<String, String>) query.get("c")
));
} else if (query.containsKey("a") && query.containsKey("b") && !query.containsKey("c")) {
result.addAll(myDao.getByAB(
(String) query.get("a"), (String) query.get("b")
));
} else if (query.containsKey("a") && !query.containsKey("b") && query.containsKey("c")) {
result.addAll(myDao.getByAC(
(String) query.get("a"), (Map<String, String>) query.get("c")
));
} else if (!query.containsKey("a") && query.containsKey("b") && query.containsKey("c")) {
result.addAll(myDao.getByBC(
(String) query.get("b"), (Map<String, String>) query.get("c")
));
} else if (query.size() == 1 && query.containsKey("a")) {
result.addAll(myDao.getByA((String) query.get("a")));
} else if (query.size() == 1 && query.containsKey("b")) {
result.addAll(myDao.getByB((String) query.get("b")));
} else if (query.size() == 1 && query.containsKey("c")) {
result.addAll(myDao.getByC((Map<String, String>) query.get("c")));
}
return result;
}
Everything works fine, but I feel like this is not the best way to deal with such situations, using if-statements for each possible combination, so how can I improve it?