Skip to content

Commit 7f642ee

Browse files
author
Greg Taylor
committed
Implemented search by keyword and search by pet type.
1 parent 2406c49 commit 7f642ee

File tree

5 files changed

+28
-54
lines changed

5 files changed

+28
-54
lines changed

app/controllers/SearchController.java

+8-24
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import io.ebean.DB;
44
import io.ebean.Expr;
5-
import io.ebean.Expression;
65
import io.ebean.ExpressionList;
7-
import io.ebean.Query;
86
import models.Pet;
97
import org.slf4j.Logger;
108
import org.slf4j.LoggerFactory;
@@ -21,7 +19,7 @@ public class SearchController extends Controller {
2119
final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
2220

2321
// 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"));
2523

2624
/**
2725
* @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) {
4038

4139
public Result searchPetByType(String petType) {
4240
// Check that the petType requested is a valid one
43-
Arrays.stream(Pet.PetType.values())
41+
Pet.PetType petTypeEnum = Arrays.stream(Pet.PetType.values())
4442
.filter(pt -> pt.name().equalsIgnoreCase(petType))
4543
.findFirst()
4644
.orElseThrow(IllegalArgumentException::new);
4745

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();
5250

5351
return ok(views.html.petList.render(searchResults));
5452
}
5553

5654
/**
5755
* Search a number of different columns for pets. Searches:
5856
* - name
59-
* - petType
6057
* - description
6158
* - color
6259
* @param keyword The keyword to use for searching
@@ -65,21 +62,8 @@ public Result searchPetByType(String petType) {
6562
public Result searchPetByKeyword(String keyword) {
6663
LOGGER.debug("Initiating keyword search for: " + keyword);
6764
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);
8367
});
8468
List<Pet> searchResults = search(searchCriteria);
8569

app/models/User.java

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ public class User extends BaseModel {
2626
@NotNull
2727
private boolean isActive;
2828

29+
public User(String name, String password, String email, boolean isActive) {
30+
this.name = name;
31+
this.password = password;
32+
this.email = email;
33+
this.isActive = isActive;
34+
}
35+
36+
public User(String name, String password, String email) {
37+
this.name = name;
38+
this.password = password;
39+
this.email = email;
40+
this.isActive = true;
41+
}
42+
2943
public void setId(long id) {
3044
this.id = id;
3145
}

app/views/main.scala.html

+5-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</a>
3131
</a>
3232
<hr>
33-
<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search" action="/search" method="get">
33+
<form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search" action="/pets/search" method="get">
3434
<input type="search" name="keyword" class="form-control form-control-dark text-bg-dark" placeholder="Search..." aria-label="Search">
3535
</form>
3636
<hr>
@@ -54,7 +54,7 @@
5454
</a>
5555
<ul class="dropdown-menu dropdown-menu-dark">
5656
@for(petType <- Pet.PetType.values()) {
57-
<li><a href="#" class="dropdown-item text-white">@petType</a></li>
57+
<li><a href="/pets/type/@petType" class="dropdown-item text-white">@petType</a></li>
5858
}
5959
</ul>
6060
</li>
@@ -78,7 +78,9 @@
7878
</div>
7979
@* And here's where we render the `Html` object containing
8080
* the page content. *@
81-
<div id="content" class="col-md-9 content">@content</div>
81+
<div id="content" class="col-md-9 content">
82+
@content
83+
</div>
8284
</div>
8385
</main>
8486
<div class="container">

app/views/petList.scala.html

-27
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,6 @@
3131
<td><button type="button" class="btn btn-primary btn-sm">Add to Cart</button></td>
3232
</tr>
3333
}
34-
<tr>
35-
<th scope="row">PPS-001-A</th>
36-
<td>Dog</td>
37-
<td>West Highland Terrier</td>
38-
<td>White</td>
39-
<td>Commonly known as the "Westie".</td>
40-
<td>$500.00</td>
41-
<td><button type="button" class="btn btn-primary btn-sm">Add to Cart</button></td>
42-
</tr>
43-
<tr>
44-
<th scope="row">PPS-002-A</th>
45-
<td>Dog</td>
46-
<td>German Shepherd</td>
47-
<td>Brown</td>
48-
<td>Working breed known for its intelligence.</td>
49-
<td>$800.00</td>
50-
<td><button type="button" class="btn btn-primary btn-sm">Add to Cart</button></td>
51-
</tr>
52-
<tr>
53-
<th scope="row">PPS-003-A</th>
54-
<td>Dog</td>
55-
<td>Dachshund</td>
56-
<td>Black</td>
57-
<td>A short-legged, long-bodied hound known as the "wiener dog".</td>
58-
<td>$400.00</td>
59-
<td><button type="button" class="btn btn-primary btn-sm">Add to Cart</button></td>
60-
</tr>
6134
</tbody>
6235
</table>
6336
</div>

build.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ scalaVersion := "2.13.8"
1010
libraryDependencies ++= Seq(
1111
guice,
1212
javaJdbc,
13+
caffeine,
1314
"org.postgresql" % "postgresql" % "42.2.12",
1415
"io.ebean" % "ebean-querybean" % "13.9.2",
1516
"io.ebean" % "ebean-agent" % "13.9.2",

0 commit comments

Comments
 (0)