File tree Expand file tree Collapse file tree 3 files changed +31
-5
lines changed
java/dev/drugowick/jpaqueriesblogpost
infrastructure/repository Expand file tree Collapse file tree 3 files changed +31
-5
lines changed Original file line number Diff line number Diff line change 3
3
import dev .drugowick .jpaqueriesblogpost .domain .model .Restaurant ;
4
4
import org .springframework .data .jpa .repository .JpaRepository ;
5
5
6
+ import java .util .List ;
7
+
6
8
public interface RestaurantRepository extends JpaRepository <Restaurant , Long > {
9
+
10
+ List <Restaurant > findAllByNameContaining (String query );
7
11
}
Original file line number Diff line number Diff line change 4
4
import org .springframework .stereotype .Controller ;
5
5
import org .springframework .ui .Model ;
6
6
import org .springframework .web .bind .annotation .RequestMapping ;
7
+ import org .springframework .web .bind .annotation .RequestParam ;
7
8
8
9
@ Controller
9
10
public class IndexPage {
@@ -19,4 +20,16 @@ public String index(Model model) {
19
20
model .addAttribute ("restaurants" , restaurantRepository .findAll ());
20
21
return "index" ;
21
22
}
23
+
24
+ @ RequestMapping ("/search" )
25
+ public String indexWithQuery (@ RequestParam ("query" ) String query ,
26
+ @ RequestParam ("field" ) String field ,
27
+ Model model ) {
28
+ if (field .equals ("name" )) {
29
+ model .addAttribute ("restaurants" , restaurantRepository .findAllByNameContaining (query ));
30
+ }
31
+ model .addAttribute ("field" , field );
32
+ model .addAttribute ("query" , query );
33
+ return "index" ;
34
+ }
22
35
}
Original file line number Diff line number Diff line change 7
7
< body >
8
8
< h1 > Restaurants</ h1 >
9
9
10
+ < form th:action ="@{/search} ">
11
+ Search by:
12
+ < select name ="field ">
13
+ < option value ="name "> Name</ option >
14
+ </ select >
15
+ < input placeholder ="Your search query " th:name ="query " th:value ="${query} " type ="text "/>
16
+ < input type ="submit " value ="Search ">
17
+ </ form >
18
+
10
19
< br > < br >
11
20
12
21
< table >
13
22
< thead >
14
- < tr bgcolor ="#d3d3d3 ">
15
- < td > Name</ td >
16
- < td > Address</ td >
17
- < td > Delivery Fee</ td >
18
- < td > Cuisine</ td >
23
+ < tr bgcolor ="#d3d3d3 ">
24
+ < td > Name</ td >
25
+ < td > Address</ td >
26
+ < td > Delivery Fee</ td >
27
+ < td > Cuisine</ td >
19
28
</ tr >
20
29
</ thead >
21
30
< tr th:each ="restaurant : ${restaurants} " style ="cursor: pointer " th:onclick ="'window.location.href=\'/restaurants/' + ${restaurant.getId()} + '\'' ">
You can’t perform that action at this time.
0 commit comments