-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #178 from FEBFES/feature/46/add-sorting
46 - add sorting
- Loading branch information
Showing
9 changed files
with
128 additions
and
18 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/main/java/com/febfes/fftmback/annotation/SortParam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.febfes.fftmback.annotation; | ||
|
||
import io.swagger.v3.oas.annotations.Parameter; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.PARAMETER}) | ||
@Parameter( | ||
name = "sort", | ||
description = " Sort parameter. ASCENDING = \"+\"; DESCENDING = \"-\". For example: sort=-id&sort=+name" | ||
) | ||
public @interface SortParam { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.febfes.fftmback.util; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
@UtilityClass | ||
public class CaseUtils { | ||
|
||
public static String camelToSnake(final String camelStr) { | ||
String ret = camelStr.replaceAll("([a-z])([A-Z]+)", "$1_$2"); | ||
return ret.toLowerCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.febfes.fftmback.util; | ||
|
||
import lombok.experimental.UtilityClass; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.domain.Sort.Order; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@UtilityClass | ||
public class SortUtils { | ||
|
||
public static Order getOrderFromParam(String sortParam) { | ||
Pattern pattern = Pattern.compile("([\\-+]?)(\\w+)"); | ||
Matcher matcher = pattern.matcher(sortParam); | ||
if (!matcher.find()) { | ||
throw new IllegalArgumentException("Sort parameter doesn't match pattern"); | ||
} | ||
String direction = matcher.group(1); | ||
String property = matcher.group(2); | ||
return new Order(getDirection(direction), property); | ||
} | ||
|
||
public static List<Order> getOrderFromParams(String[] sortParams) { | ||
List<Order> sortRows = new ArrayList<>(); | ||
for (String sortParam : sortParams) { | ||
sortRows.add(getOrderFromParam(sortParam)); | ||
} | ||
return sortRows; | ||
} | ||
|
||
private static Sort.Direction getDirection(String direction) { | ||
return direction.equals("+") ? Sort.Direction.ASC : Sort.Direction.DESC; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters