Skip to content

Commit ad0b5aa

Browse files
committed
Item B-03526: As a Manager, I wold like to sort services in the service management table
The ServiceSpecification is implemented to provide sorting and pagination. The AbstracSpecification used a default 'lastModified' order by, which does not exist in the ServiceRepo. To prevent breakage, the the order by assignment was move into its own function, toPredicateDefaultQueryOrderBy(). This allows for the ServiceSpecification, as an exception, to not attempt to order by "lastModified" by overriding that new method.
1 parent 199531e commit ad0b5aa

5 files changed

Lines changed: 46 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import edu.tamu.app.model.Service;
1818
import edu.tamu.app.model.repo.IdeaRepo;
1919
import edu.tamu.app.model.repo.ServiceRepo;
20+
import edu.tamu.app.model.request.FilteredPageRequest;
2021
import edu.tamu.app.model.request.IssueRequest;
2122
import edu.tamu.app.model.request.ServiceRequest;
2223
import edu.tamu.app.service.ProjectService;
@@ -52,6 +53,12 @@ public ApiResponse getPublicServices() {
5253
return new ApiResponse(SUCCESS, serviceRepo.findByIsPublicOrderByStatusDescNameAsc(true));
5354
}
5455

56+
@RequestMapping("/page")
57+
@PreAuthorize("hasRole('ANONYMOUS')")
58+
public ApiResponse page(@RequestBody FilteredPageRequest filteredPageRequest) {
59+
return new ApiResponse(SUCCESS, serviceRepo.findAll(filteredPageRequest.getServiceSpecification(), filteredPageRequest.getPageRequest()));
60+
}
61+
5562
@RequestMapping("/{id}")
5663
@PreAuthorize("hasRole('ANONYMOUS')")
5764
public ApiResponse getService(@PathVariable Long id) {

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

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

33
import java.util.List;
44

5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.jpa.domain.Specification;
58
import org.springframework.data.jpa.repository.JpaRepository;
69

710
import edu.tamu.app.enums.Status;
@@ -10,6 +13,8 @@
1013

1114
public interface ServiceRepo extends JpaRepository<Service, Long>, ServiceRepoCustom {
1215

16+
public Page<Service> findAll(Specification<Service> specification, Pageable pageable);
17+
1318
public List<Service> findByIsPublicOrderByStatusDescNameAsc(Boolean isPublic);
1419

1520
public List<Service> findByIsAuto(Boolean isAuto);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,18 @@ public Predicate toPredicate(Root<E> root, CriteriaQuery<?> query, CriteriaBuild
4949
}
5050
}
5151

52-
query.orderBy(cb.desc(root.get("lastModified")));
52+
toPredicateDefaultQueryOrderBy(root, query, cb);
5353

5454
return builder.build(cb);
5555
}
5656

57+
/**
58+
* Allow implementing classes to control order by in case lastModified is non-existent.
59+
*/
60+
protected void toPredicateDefaultQueryOrderBy(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
61+
query.orderBy(cb.desc(root.get("lastModified")));
62+
}
63+
5764
private class PredicateBuilder {
5865

5966
private final Map<String, List<Predicate>> predicates;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package edu.tamu.app.model.repo.specification;
2+
3+
import java.util.Map;
4+
5+
import javax.persistence.criteria.CriteriaBuilder;
6+
import javax.persistence.criteria.CriteriaQuery;
7+
import javax.persistence.criteria.Root;
8+
9+
public class ServiceSpecification<E> extends AbstractSpecification<E> {
10+
11+
public ServiceSpecification(Map<String, String[]> filters) {
12+
super(filters);
13+
}
14+
15+
@Override
16+
protected void toPredicateDefaultQueryOrderBy(Root<E> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
17+
query.orderBy(cb.desc(root.get("name")));
18+
}
19+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import edu.tamu.app.model.FeatureProposal;
1414
import edu.tamu.app.model.Idea;
1515
import edu.tamu.app.model.Note;
16+
import edu.tamu.app.model.Service;
1617
import edu.tamu.app.model.repo.specification.FeatureProposalSpecification;
1718
import edu.tamu.app.model.repo.specification.IdeaSpecification;
1819
import edu.tamu.app.model.repo.specification.NoteSpecification;
20+
import edu.tamu.app.model.repo.specification.ServiceSpecification;
1921

2022
public class FilteredPageRequest {
2123

@@ -42,6 +44,11 @@ public IdeaSpecification<Idea> getIdeaSpecification() {
4244
return new IdeaSpecification<Idea>(filters);
4345
}
4446

47+
@JsonIgnore
48+
public ServiceSpecification<Service> getServiceSpecification() {
49+
return new ServiceSpecification<Service>(filters);
50+
}
51+
4552
@JsonIgnore
4653
public FeatureProposalSpecification<FeatureProposal> getFeatureProposalSpecification() {
4754
return new FeatureProposalSpecification<FeatureProposal>(filters);

0 commit comments

Comments
 (0)