2
2
3
3
import io .ebean .DB ;
4
4
import io .ebean .Expr ;
5
- import io .ebean .Expression ;
6
5
import io .ebean .ExpressionList ;
7
- import io .ebean .Query ;
8
6
import models .Pet ;
9
7
import org .slf4j .Logger ;
10
8
import org .slf4j .LoggerFactory ;
@@ -21,7 +19,7 @@ public class SearchController extends Controller {
21
19
final Logger LOGGER = LoggerFactory .getLogger (this .getClass ());
22
20
23
21
// The columns searched during a keyword search
24
- private List <String > KEYWORD_SEARCH_COLUMNS = new ArrayList <>(Arrays .asList ("name" , "petType" , " description" , "color" ));
22
+ private List <String > KEYWORD_SEARCH_COLUMNS = new ArrayList <>(Arrays .asList ("name" , "description" , "color" ));
25
23
26
24
/**
27
25
* @param criteria Key-value pair of what to search for. For example, an entry of <"petType","Dog"> will search for all pets with a type of "Dog".
@@ -40,23 +38,22 @@ public List<Pet> search(Map<String, String> criteria) {
40
38
41
39
public Result searchPetByType (String petType ) {
42
40
// Check that the petType requested is a valid one
43
- Arrays .stream (Pet .PetType .values ())
41
+ Pet . PetType petTypeEnum = Arrays .stream (Pet .PetType .values ())
44
42
.filter (pt -> pt .name ().equalsIgnoreCase (petType ))
45
43
.findFirst ()
46
44
.orElseThrow (IllegalArgumentException ::new );
47
45
48
- Map < String , String > searchCriteria = new HashMap <>();
49
- searchCriteria . put ( "petType" , petType );
50
-
51
- List < Pet > searchResults = search ( searchCriteria );
46
+ List < Pet > searchResults = DB . find ( Pet . class )
47
+ . where ()
48
+ . eq ( "petType" , petTypeEnum )
49
+ . findList ( );
52
50
53
51
return ok (views .html .petList .render (searchResults ));
54
52
}
55
53
56
54
/**
57
55
* Search a number of different columns for pets. Searches:
58
56
* - name
59
- * - petType
60
57
* - description
61
58
* - color
62
59
* @param keyword The keyword to use for searching
@@ -65,21 +62,8 @@ public Result searchPetByType(String petType) {
65
62
public Result searchPetByKeyword (String keyword ) {
66
63
LOGGER .debug ("Initiating keyword search for: " + keyword );
67
64
Map <String , String > searchCriteria = new HashMap <>();
68
- KEYWORD_SEARCH_COLUMNS .forEach ((c ) ->
69
- {
70
- // PetType is an enum, we need to make sure that the keyword is valid if we are going to use it for
71
- // searching on PetType
72
- if (c .equals ("petType" )) {
73
- if (Arrays .stream (Pet .PetType .values ())
74
- .anyMatch (pt -> pt .toString ().equalsIgnoreCase (keyword ))) {
75
- searchCriteria .put (c , keyword );
76
- } else {
77
- LOGGER .debug ("Excluding keyword from petType search, not a valid petType: " + keyword );
78
- }
79
- } else {
80
- // For other non-enum columns, add the keyword
81
- searchCriteria .put (c , keyword );
82
- }
65
+ KEYWORD_SEARCH_COLUMNS .forEach ((c ) -> {
66
+ searchCriteria .put (c , keyword );
83
67
});
84
68
List <Pet > searchResults = search (searchCriteria );
85
69
0 commit comments