Skip to content

Commit

Permalink
Merge pull request #23 from Trails-Through-Shadows/implementedFeatures
Browse files Browse the repository at this point in the history
Implemented features requested by myself (:
  • Loading branch information
Firestone82 authored Jan 26, 2024
2 parents 3e36188 + 23bb670 commit 40fd38f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public ResponseEntity<RestPaginatedResult<Part>> getParts(
) {
List<Part> entries = partRepo.findAll().stream()
.filter((entry) -> Filtering.match(entry, List.of(filter.split(","))))
.sorted((a, b) -> Sorting.compareTo(a, b, List.of(sort.split(","))))
.toList();

List<Part> entriesPage = entries.stream()
.skip((long) Math.max(page, 0) * limit)
.limit(limit)
.sorted((a, b) -> Sorting.compareTo(a, b, List.of(sort.split(","))))
.toList();

Pagination pagination = new Pagination(entriesPage.size(), (entries.size() > (Math.max(page, 0) + 1) * limit), entries.size(), page, limit);
Expand Down Expand Up @@ -105,12 +105,12 @@ public ResponseEntity<RestPaginatedResult<Location>> getLocations(
) {
List<Location> entries = locationRepo.findAll().stream()
.filter((entry) -> Filtering.match(entry, List.of(filter.split(","))))
.sorted((a, b) -> Sorting.compareTo(a, b, List.of(sort.split(","))))
.toList();

List<Location> entriesPage = entries.stream()
.skip((long) Math.max(page, 0) * limit)
.limit(limit)
.sorted((a, b) -> Sorting.compareTo(a, b, List.of(sort.split(","))))
.toList();

Pagination pagination = new Pagination(entriesPage.size(), (entries.size() > (Math.max(page, 0) + 1) * limit), entries.size(), page, limit);
Expand All @@ -135,12 +135,12 @@ public ResponseEntity<?> getObstacles(
) {
List<Obstacle> entries = obstacleRepo.findAll().stream()
.filter((entry) -> Filtering.match(entry, List.of(filter.split(","))))
.sorted((a, b) -> Sorting.compareTo(a, b, List.of(sort.split(","))))
.toList();

List<Obstacle> entriesPage = entries.stream()
.skip((long) Math.max(page, 0) * limit)
.limit(limit)
.sorted((a, b) -> Sorting.compareTo(a, b, List.of(sort.split(","))))
.toList();

Pagination pagination = new Pagination(entriesPage.size(), (entries.size() > (Math.max(page, 0) + 1) * limit), entries.size(), page, limit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -33,11 +35,24 @@ public static <T> boolean match(T object, String filter) {
try {
Field field = Ref.getField(object.getClass(), filterKey);
Object fieldValue = field.get(object);
Object parsedValue = Ref.parseValue(field, filterValue);

// When filter list, filter by list size
if (fieldValue instanceof Enumeration<?> || fieldValue instanceof Collection<?>) {
fieldValue = (Integer) ((Collection<?>) fieldValue).size();
}

if (filterOperator.equalsIgnoreCase("bwn")) {
String[] filterValueSplit = filterValue.split("_");

Object filterValueMin = Integer.parseInt(filterValueSplit[0]);
Object filterValueMax = Integer.parseInt(filterValueSplit[1]);

return Ref.compare(fieldValue, filterValueMin, "gte") && Ref.compare(fieldValue, filterValueMax, "lte");
}

return switch (filterOperator) {
case "eq" -> fieldValue.equals(parsedValue);
case "gt", "gte", "lt", "lte" -> Ref.compare(fieldValue, parsedValue, filterOperator);
case "eq" -> fieldValue.equals(Ref.parseValue(field, filterValue));
case "gt", "gte", "lt", "lte" -> Ref.compare(fieldValue, Ref.parseValue(field, filterValue), filterOperator);
case "has" -> fieldValue.toString().contains(filterValue);
default -> false;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -44,6 +46,17 @@ public static <T, U extends Comparable<U>> int compareTo(T first, T second, Stri
secondField.setAccessible(true);
Object secondValue = secondField.get(second);

// Sorting types must match
if (!firstValue.getClass().equals(secondValue.getClass())) {
throw new IllegalArgumentException("Field values are not of same type");
}

// When sorting list, sort by list size
if (firstValue instanceof Enumeration<?> || firstValue instanceof Collection<?>) {
firstValue = (Integer) ((Collection<?>) firstValue).size();
secondValue = (Integer) ((Collection<?>) secondValue).size();
}

if (!(firstValue instanceof Comparable<?>) || !(secondValue instanceof Comparable<?>)) {
throw new IllegalArgumentException("Field values are not comparable");
}
Expand Down

0 comments on commit 40fd38f

Please sign in to comment.