Skip to content

Commit 2520eee

Browse files
authored
Merge pull request #34 from TAMULib/note-filtering
ability to filter and sort note page requests
2 parents 45be0ff + b5cab63 commit 2520eee

6 files changed

Lines changed: 62 additions & 33 deletions

File tree

src/main/java/edu/tamu/app/controller/NoteController.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class NoteController {
2828
@Autowired
2929
private NoteRepo noteRepo;
3030

31-
@ApiMapping("/query")
31+
@ApiMapping("/page")
3232
@Auth(role = "ROLE_ANONYMOUS")
3333
public ApiResponse getAllNotesByService(@ApiData FilteredPageRequest filteredPageRequest) {
3434
return new ApiResponse(SUCCESS, noteRepo.findAll(filteredPageRequest.getSpecification(), filteredPageRequest.getPageRequest()));
@@ -61,10 +61,4 @@ public ApiResponse remove(@ApiValidatedModel Note note) {
6161
return new ApiResponse(SUCCESS);
6262
}
6363

64-
@ApiMapping("/page")
65-
@Auth(role = "ROLE_ANONYMOUS")
66-
public ApiResponse page(@ApiData FilteredPageRequest filteredPageRequest) {
67-
return new ApiResponse(SUCCESS, noteRepo.findAllByOrderByServiceNameAscLastModifiedDesc(filteredPageRequest.getPageRequest()));
68-
}
69-
7064
}

src/main/java/edu/tamu/app/model/repo/NoteRepo.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public interface NoteRepo extends JpaRepository<Note, Long>, NoteRepoCustom, Jpa
1515

1616
public Page<Note> findAll(Specification<Note> specification, Pageable pageable);
1717

18-
public Page<Note> findAllByOrderByServiceNameAscLastModifiedDesc(Pageable pageable);
19-
2018
public List<Note> findAllByServiceId(Long id);
2119

2220
public void delete(Note note);

src/main/java/edu/tamu/app/model/repo/specification/NoteSpecification.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public NoteSpecification(Map<String, String[]> filters) {
2222
@Override
2323
public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
2424

25-
List<Predicate> activeOrPinnedPredicates = new ArrayList<Predicate>();
25+
List<Predicate> activeAndPinnedPredicates = new ArrayList<Predicate>();
2626
List<Predicate> servicePredicates = new ArrayList<Predicate>();
2727

2828
for (Map.Entry<String, String[]> entry : filters.entrySet()) {
@@ -32,12 +32,12 @@ public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuild
3232
switch (key) {
3333
case "active":
3434
for (String value : values) {
35-
activeOrPinnedPredicates.add(cb.like(cb.lower(root.get(key).as(String.class)), "%" + value.toLowerCase() + "%"));
35+
activeAndPinnedPredicates.add(cb.like(cb.lower(root.get(key).as(String.class)), "%" + value.toLowerCase() + "%"));
3636
}
3737
break;
3838
case "pinned":
3939
for (String value : values) {
40-
activeOrPinnedPredicates.add(cb.like(cb.lower(root.get(key).as(String.class)), "%" + value.toLowerCase() + "%"));
40+
activeAndPinnedPredicates.add(cb.like(cb.lower(root.get(key).as(String.class)), "%" + value.toLowerCase() + "%"));
4141
}
4242
break;
4343
case "service":
@@ -50,13 +50,13 @@ public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuild
5050
}
5151

5252
}
53-
53+
5454
query.orderBy(cb.desc(root.get("lastModified")));
5555

5656
Predicate predicate;
5757

58-
if (activeOrPinnedPredicates.size() > 0) {
59-
predicate = cb.and(cb.or(activeOrPinnedPredicates.toArray(new Predicate[activeOrPinnedPredicates.size()])), cb.and(servicePredicates.toArray(new Predicate[servicePredicates.size()])));
58+
if (activeAndPinnedPredicates.size() > 0) {
59+
predicate = cb.and(cb.and(activeAndPinnedPredicates.toArray(new Predicate[activeAndPinnedPredicates.size()])), cb.and(servicePredicates.toArray(new Predicate[servicePredicates.size()])));
6060
} else {
6161
predicate = cb.and(servicePredicates.toArray(new Predicate[servicePredicates.size()]));
6262
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package edu.tamu.app.model.request;
2+
3+
import org.springframework.data.domain.Sort;
4+
5+
public class DirectionSort {
6+
7+
private String property;
8+
9+
private Sort.Direction direction;
10+
11+
public DirectionSort() {
12+
13+
}
14+
15+
public DirectionSort(String property, Sort.Direction direction) {
16+
this();
17+
this.property = property;
18+
this.direction = direction;
19+
}
20+
21+
public String getProperty() {
22+
return property;
23+
}
24+
25+
public void setProperty(String property) {
26+
this.property = property;
27+
}
28+
29+
public Sort.Direction getDirection() {
30+
return direction;
31+
}
32+
33+
public void setDirection(Sort.Direction direction) {
34+
this.direction = direction;
35+
}
36+
37+
}

src/main/java/edu/tamu/app/model/request/FilteredPageRequest.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package edu.tamu.app.model.request;
22

3+
import java.util.ArrayList;
4+
import java.util.HashMap;
35
import java.util.List;
46
import java.util.Map;
57

@@ -17,14 +19,13 @@ public class FilteredPageRequest {
1719

1820
private int pageSize;
1921

20-
private String direction;
21-
22-
private List<String> properties;
22+
private List<DirectionSort> sort;
2323

2424
private Map<String, String[]> filters;
2525

2626
public FilteredPageRequest() {
27-
27+
sort = new ArrayList<DirectionSort>();
28+
filters = new HashMap<String, String[]>();
2829
}
2930

3031
@JsonIgnore
@@ -34,7 +35,14 @@ public NoteSpecification<Note> getSpecification() {
3435

3536
@JsonIgnore
3637
public PageRequest getPageRequest() {
37-
return new PageRequest(pageNumber > 0 ? pageNumber - 1 : 0, pageSize > 0 ? pageSize : 10, new Sort(Sort.Direction.fromString(direction), properties));
38+
List<Sort.Order> orders = new ArrayList<Sort.Order>();
39+
if (orders.isEmpty()) {
40+
return new PageRequest(pageNumber > 0 ? pageNumber - 1 : 0, pageSize > 0 ? pageSize : 10);
41+
}
42+
sort.forEach(sort -> {
43+
orders.add(new Sort.Order(sort.getDirection(), sort.getProperty()));
44+
});
45+
return new PageRequest(pageNumber > 0 ? pageNumber - 1 : 0, pageSize > 0 ? pageSize : 10, new Sort(orders));
3846
}
3947

4048
public int getPageNumber() {
@@ -53,20 +61,12 @@ public void setPageSize(int pageSize) {
5361
this.pageSize = pageSize;
5462
}
5563

56-
public String getDirection() {
57-
return direction;
58-
}
59-
60-
public void setDirection(String direction) {
61-
this.direction = direction;
62-
}
63-
64-
public List<String> getProperties() {
65-
return properties;
64+
public List<DirectionSort> getSort() {
65+
return sort;
6666
}
6767

68-
public void setProperties(List<String> properties) {
69-
this.properties = properties;
68+
public void setSort(List<DirectionSort> sort) {
69+
this.sort = sort;
7070
}
7171

7272
public Map<String, String[]> getFilters() {

src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ app.reporting.address: report@mailinator.com
5454
# required in framework
5555
app.model.packages: edu.tamu.app.model
5656

57-
app.authority.admins: 123456789, 990000081, 523008230, 512004707, 613001223
57+
app.authority.admins: 123456789, 990000081, 523008230, 512004707, 613001223, 402001311
5858

5959
app.ui.host: http://localhost/status-system
6060

0 commit comments

Comments
 (0)