Skip to content

Commit 8758bfb

Browse files
Refactors to JPQL instead of Query Methods.
1 parent ab7a2e4 commit 8758bfb

File tree

6 files changed

+17
-10
lines changed

6 files changed

+17
-10
lines changed

src/main/java/dev/drugowick/jpaqueriesblogpost/domain/model/Restaurant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Restaurant {
3131

3232
private String address;
3333
private String city;
34-
private boolean grabAndGo;
34+
private boolean grabngo;
3535
private boolean active;
3636

3737
@CreationTimestamp

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import dev.drugowick.jpaqueriesblogpost.domain.model.Restaurant;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
56

67
import java.math.BigDecimal;
78
import java.util.List;
@@ -17,4 +18,7 @@ public interface RestaurantRepository extends JpaRepository<Restaurant, Long> {
1718
long countByCuisineName(String cuisine);
1819

1920
Restaurant findTopByCuisineNameOrderByDeliveryFeeAsc(String cuisine);
21+
22+
@Query("from Restaurant r where r.active = true and r.grabngo = true and r.city like %:city%")
23+
List<Restaurant> activeGrabngoByCity(String city);
2024
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public String indexWithQuery(@RequestParam("query") String query,
4040
query = "100.00";
4141
}
4242
model.addAttribute("restaurants", restaurantRepository.findAllByDeliveryFeeIsLessThanEqual(deliveryFee));
43+
} else if (field.equals("local-grabngo")) {
44+
model.addAttribute("restaurants", restaurantRepository.activeGrabngoByCity(query));
4345
}
4446
model.addAttribute("field", field);
4547
model.addAttribute("query", query);

src/main/resources/import.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ insert into cuisine (name) values ('Brazilian');
33
insert into cuisine (name) values ('American');
44
insert into cuisine (name) values ('French');
55

6-
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grab_and_go, active) values ('Mamma Mia', 'Mamma Street, 14', 10.0, 1, current_timestamp, current_timestamp, 'Venice', false, true);
7-
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grab_and_go, active) values ('Churrascaria', 'Disorder Avenue, 5000', 8.0, 2, current_timestamp, current_timestamp, 'Sertãozinho', true, true);
8-
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grab_and_go, active) values ('Burguer Place', 'Clueless Alley, 10', 5.0, 3, current_timestamp, current_timestamp, 'San Francisco', true, false);
9-
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grab_and_go, active) values ('La Maison du Croissant ', 'Rue Paris, 7', 15.0, 4, current_timestamp, current_timestamp, 'Paris', false, true);
6+
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grabngo, active) values ('Mamma Mia', 'Mamma Street, 14', 10.0, 1, current_timestamp, current_timestamp, 'Venice', false, true);
7+
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grabngo, active) values ('Churrascaria', 'Disorder Avenue, 5000', 8.0, 2, current_timestamp, current_timestamp, 'Sertãozinho', true, true);
8+
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grabngo, active) values ('Burguer Place', 'Clueless Alley, 10', 5.0, 3, current_timestamp, current_timestamp, 'San Francisco', true, false);
9+
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grabngo, active) values ('La Maison du Croissant ', 'Rue Paris, 7', 15.0, 4, current_timestamp, current_timestamp, 'Paris', false, true);
1010

11-
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grab_and_go, active) values ('Pasta Buona', 'Pasta Street, 4', 2.0, 1, current_timestamp, current_timestamp, 'Roma', true, true);
12-
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grab_and_go, active) values ('Marcante Pizzaria', 'Bricks Street, 21', 9.0, 2, current_timestamp, current_timestamp, 'São Paulo', true, false);
11+
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grabngo, active) values ('Pasta Buona', 'Pasta Street, 4', 2.0, 1, current_timestamp, current_timestamp, 'Roma', true, true);
12+
insert into restaurant (name, address, delivery_fee, cuisine_id, created_date, updated_date, city, grabngo, active) values ('Marcante Pizzaria', 'Bricks Street, 21', 9.0, 2, current_timestamp, current_timestamp, 'São Paulo', true, false);

src/main/resources/templates/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ <h4>Filter Restaurants</h4>
2323
<option th:selected="${field == 'name'}" value="name">Name</option>
2424
<option th:selected="${field == 'cuisine'}" value="cuisine">Cuisine</option>
2525
<option th:selected="${field == 'delivery-fee'}" value="delivery-fee">Delivery Fee</option>
26+
<option th:selected="${field == 'local-grabngo'}" value="local-grabngo">Active and Grab'n'Go by City</option>
2627
</select>
2728
</div>
2829
<div class="form-group row">
@@ -61,7 +62,7 @@ <h4>Filter Restaurants</h4>
6162
<td th:text="${restaurant.getDeliveryFee()}"></td>
6263
<td th:text="${restaurant.getCuisine().getName()}"></td>
6364
<td th:text="${restaurant.getCity()}"></td>
64-
<td><input type="checkbox" th:checked="${restaurant.isGrabAndGo()}" disabled="true"></td>
65+
<td><input type="checkbox" th:checked="${restaurant.isGrabngo()}" disabled="true"></td>
6566
<td><input type="checkbox" th:checked="${restaurant.isActive()}" disabled="true"></td>
6667
</tr>
6768
<tbody>

src/main/resources/templates/restaurant.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ <h1 th:text="${restaurant.getName()}">Restaurant 1</h1>
4545
</tr>
4646
<tr>
4747
<td>Grab'n'Go</td>
48-
<td th:if="${restaurant.isGrabAndGo()}">Yes</td>
49-
<td th:if="${!restaurant.isGrabAndGo()}">No</td>
48+
<td th:if="${restaurant.isGrabngo()}">Yes</td>
49+
<td th:if="${!restaurant.isGrabngo()}">No</td>
5050
</tr>
5151
<tr>
5252
<td>Active</td>

0 commit comments

Comments
 (0)