Skip to content

Commit dd6793b

Browse files
Adds search options and cuisine page.
1 parent 10e1373 commit dd6793b

File tree

7 files changed

+115
-10
lines changed

7 files changed

+115
-10
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dev.drugowick.jpaqueriesblogpost.infrastructure.repository;
2+
3+
import dev.drugowick.jpaqueriesblogpost.domain.model.Cuisine;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface CuisineRepository extends JpaRepository<Cuisine, Long> {
7+
}

src/main/java/dev/drugowick/jpaqueriesblogpost/infrastructure/repository/RestaurantRepository.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
import dev.drugowick.jpaqueriesblogpost.domain.model.Restaurant;
44
import org.springframework.data.jpa.repository.JpaRepository;
55

6+
import java.math.BigDecimal;
67
import java.util.List;
78

89
public interface RestaurantRepository extends JpaRepository<Restaurant, Long> {
910

1011
List<Restaurant> findAllByNameContaining(String query);
12+
13+
List<Restaurant> findAllByCuisineNameContaining(String cuisine);
14+
15+
List<Restaurant> findAllByDeliveryFeeIsLessThanEqual(BigDecimal deliveryFee);
16+
17+
long countByCuisineName(String cuisine);
18+
19+
Restaurant findTopByCuisineNameOrderByDeliveryFeeAsc(String cuisine);
1120
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dev.drugowick.jpaqueriesblogpost.web.pages;
2+
3+
import dev.drugowick.jpaqueriesblogpost.domain.model.Cuisine;
4+
import dev.drugowick.jpaqueriesblogpost.infrastructure.repository.CuisineRepository;
5+
import dev.drugowick.jpaqueriesblogpost.infrastructure.repository.RestaurantRepository;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.PathVariable;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
11+
@Controller
12+
@RequestMapping("/cuisines")
13+
public class CuisinePage {
14+
15+
private final RestaurantRepository restaurantRepository;
16+
private final CuisineRepository cuisineRepository;
17+
18+
19+
public CuisinePage(RestaurantRepository restaurantRepository, CuisineRepository cuisineRepository) {
20+
this.restaurantRepository = restaurantRepository;
21+
this.cuisineRepository = cuisineRepository;
22+
}
23+
24+
@RequestMapping("/{id}")
25+
public String index(@PathVariable("id") Long id, Model model) {
26+
Cuisine cuisine = cuisineRepository.findById(id)
27+
.orElseThrow(() -> new RuntimeException(
28+
String.format("Could not find cuisine with id %d", id)
29+
));
30+
model.addAttribute("cuisine", cuisine);
31+
model.addAttribute("count", restaurantRepository.countByCuisineName(cuisine.getName()));
32+
model.addAttribute("cheapestRestaurant", restaurantRepository.findTopByCuisineNameOrderByDeliveryFeeAsc(cuisine.getName()));
33+
return "cuisine";
34+
}
35+
}

src/main/java/dev/drugowick/jpaqueriesblogpost/web/pages/IndexPage.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import org.springframework.web.bind.annotation.RequestMapping;
77
import org.springframework.web.bind.annotation.RequestParam;
88

9+
import java.math.BigDecimal;
10+
911
@Controller
1012
public class IndexPage {
1113

@@ -27,6 +29,17 @@ public String indexWithQuery(@RequestParam("query") String query,
2729
Model model) {
2830
if (field.equals("name")) {
2931
model.addAttribute("restaurants", restaurantRepository.findAllByNameContaining(query));
32+
} else if (field.equals("cuisine")) {
33+
model.addAttribute("restaurants", restaurantRepository.findAllByCuisineNameContaining(query));
34+
} else if (field.equals("delivery-fee")) {
35+
BigDecimal deliveryFee;
36+
try {
37+
deliveryFee = new BigDecimal(query);
38+
} catch (NumberFormatException e) {
39+
deliveryFee = new BigDecimal(100.00);
40+
query = "100.00";
41+
}
42+
model.addAttribute("restaurants", restaurantRepository.findAllByDeliveryFeeIsLessThanEqual(deliveryFee));
3043
}
3144
model.addAttribute("field", field);
3245
model.addAttribute("query", query);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Cuisine</title>
6+
</head>
7+
<body>
8+
<h1 th:text="${cuisine.getName()}">Cuisine 1</h1>
9+
10+
<table>
11+
<thead>
12+
<tr bgcolor="#d3d3d3">
13+
<td colspan="2"><b>Cuisine Info</b></td>
14+
</tr>
15+
</thead>
16+
<tr>
17+
<td>Name</td>
18+
<td th:text="${cuisine.getName()}"></td>
19+
</tr>
20+
<tr>
21+
<td>Number of Restaurants</td>
22+
<td th:text="${count}"></td>
23+
</tr>
24+
<tr>
25+
<td>Cheapest Delivery Fee</td>
26+
<td th:text="${cheapestRestaurant.getName() + ' ($ ' + cheapestRestaurant.getDeliveryFee() + ')'}"></td>
27+
</span></td>
28+
</tr>
29+
<tbody>
30+
31+
</tbody>
32+
</table>
33+
34+
</body>
35+
</html>

src/main/resources/templates/index.html

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ <h1>Restaurants</h1>
99

1010
<form th:action="@{/search}">
1111
Search by:
12-
<select name="field">
13-
<option value="name">Name</option>
12+
<select id="field" name="field">
13+
<option th:selected="${field == 'name'}" value="name">Name</option>
14+
<option th:selected="${field == 'cuisine'}" value="cuisine">Cuisine</option>
15+
<option th:selected="${field == 'delivery-fee'}" value="delivery-fee">Delivery Fee</option>
1416
</select>
1517
<input placeholder="Your search query" th:name="query" th:value="${query}" type="text"/>
1618
<input type="submit" value="Search">
@@ -25,14 +27,16 @@ <h1>Restaurants</h1>
2527
<td>Address</td>
2628
<td>Delivery Fee</td>
2729
<td>Cuisine</td>
28-
</tr>
30+
</tr>
2931
</thead>
30-
<tr th:each="restaurant : ${restaurants}" style="cursor: pointer" th:onclick="'window.location.href=\'/restaurants/' + ${restaurant.getId()} + '\''">
31-
<td th:text="${restaurant.getName()}"></td>
32-
<td th:text="${restaurant.getAddress()}"></td>
33-
<td th:text="${restaurant.getDeliveryFee()}"></td>
34-
<td th:text="${restaurant.getCuisine().getName()}"></td>
35-
</tr>
32+
<tr th:each="restaurant : ${restaurants}">
33+
<td style="cursor: pointer; background-color: aquamarine "
34+
th:onclick="'window.location.href=\'/restaurants/' + ${restaurant.getId()} + '\''"
35+
th:text="${restaurant.getName()}"></td>
36+
<td th:text="${restaurant.getAddress()}"></td>
37+
<td th:text="${restaurant.getDeliveryFee()}"></td>
38+
<td th:text="${restaurant.getCuisine().getName()}"></td>
39+
</tr>
3640
<tbody>
3741

3842
</tbody>

src/main/resources/templates/restaurant.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ <h1 th:text="${restaurant.getName()}">Restaurant 1</h1>
2727
</tr>
2828
<tr>
2929
<td>Cuisine</td>
30-
<td th:text="${restaurant.getCuisine().getName()}"></td>
30+
<td style="cursor: pointer; background-color: aquamarine "
31+
th:onclick="'window.location.href=\'/cuisines/' + ${restaurant.getCuisine().getId()} + '\''"
32+
th:text="${restaurant.getCuisine().getName()}"></td>
3133
</tr>
3234
<tr>
3335
<td>Updated Date</td>

0 commit comments

Comments
 (0)