Skip to content

Commit a39998b

Browse files
committed
add SortRepository
1 parent 216fc20 commit a39998b

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.codingapi.springboot.fast.jpa.repository;
2+
3+
import com.codingapi.springboot.framework.domain.ISort;
4+
import com.codingapi.springboot.framework.dto.request.SortRequest;
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.repository.NoRepositoryBean;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
@NoRepositoryBean
12+
public interface SortRepository<T extends ISort, ID> extends JpaRepository<T, ID> {
13+
14+
15+
default void reSort(SortRequest request) {
16+
if (request != null && !request.getIds().isEmpty()) {
17+
List<T> list = new ArrayList<>();
18+
int minSort = 0;
19+
for (Object objectId : request.getIds()) {
20+
ID id = (ID) objectId;
21+
T t = getReferenceById(id);
22+
if (t.getSort() != null && t.getSort() < minSort) {
23+
minSort = t.getSort();
24+
}
25+
list.add(t);
26+
}
27+
for (T t : list) {
28+
t.setSort(minSort++);
29+
}
30+
saveAll(list);
31+
}
32+
}
33+
34+
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codingapi.springboot.framework.domain;
2+
3+
public interface ISort {
4+
5+
void setSort(Integer sort);
6+
7+
Integer getSort();
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.codingapi.springboot.framework.dto.request;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
6+
import java.util.List;
7+
8+
@Setter
9+
@Getter
10+
public class SortRequest {
11+
12+
private List<Object> ids;
13+
14+
}

0 commit comments

Comments
 (0)