mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-12-15 04:41:58 +09:00
Implemented sorting on Action logs and standardized filtering/pagination mechanics
This commit is contained in:
@@ -2,12 +2,11 @@ package tech.ailef.dbadmin.internal.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
|
||||
import tech.ailef.dbadmin.external.dto.LogsSearchRequest;
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
|
||||
public interface CustomActionRepository {
|
||||
public List<UserAction> findActions(String table, String actionType, String itemId, PageRequest pageRequest);
|
||||
public List<UserAction> findActions(LogsSearchRequest r);
|
||||
|
||||
public long countActions(String table, String actionType, String itemId);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
import tech.ailef.dbadmin.external.dto.LogsSearchRequest;
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
|
||||
@Component
|
||||
@@ -21,7 +22,11 @@ public class CustomActionRepositoryImpl implements CustomActionRepository {
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Override
|
||||
public List<UserAction> findActions(String table, String actionType, String itemId, PageRequest page) {
|
||||
public List<UserAction> findActions(LogsSearchRequest request) {
|
||||
String table = request.getTable();
|
||||
String actionType = request.getActionType();
|
||||
String itemId = request.getItemId();
|
||||
PageRequest page = request.toPageRequest();
|
||||
|
||||
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<UserAction> query = cb.createQuery(UserAction.class);
|
||||
@@ -41,6 +46,15 @@ public class CustomActionRepositoryImpl implements CustomActionRepository {
|
||||
predicates.toArray(new Predicate[predicates.size()])));
|
||||
}
|
||||
|
||||
if (request.getSortKey() != null) {
|
||||
String key = request.getSortKey();
|
||||
if (request.getSortOrder().equalsIgnoreCase("ASC")) {
|
||||
query.orderBy(cb.asc(userAction.get(key)));
|
||||
} else {
|
||||
query.orderBy(cb.desc(userAction.get(key)));
|
||||
}
|
||||
}
|
||||
|
||||
return entityManager.createQuery(query)
|
||||
.setMaxResults(page.getPageSize())
|
||||
.setFirstResult((int)page.getOffset())
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import tech.ailef.dbadmin.external.dto.LogsSearchRequest;
|
||||
import tech.ailef.dbadmin.external.dto.PaginatedResult;
|
||||
import tech.ailef.dbadmin.external.dto.PaginationInfo;
|
||||
import tech.ailef.dbadmin.internal.model.UserAction;
|
||||
@@ -26,13 +27,18 @@ public class UserActionService {
|
||||
return repo.save(a);
|
||||
}
|
||||
|
||||
public PaginatedResult<UserAction> findActions(String table, String actionType, String userId, PageRequest page) {
|
||||
long count = customRepo.countActions(table, actionType, userId);
|
||||
List<UserAction> actions = customRepo.findActions(table, actionType, userId, page);
|
||||
public PaginatedResult<UserAction> findActions(LogsSearchRequest request) {
|
||||
String table = request.getTable();
|
||||
String actionType = request.getActionType();
|
||||
String itemId = request.getItemId();
|
||||
PageRequest page = request.toPageRequest();
|
||||
|
||||
long count = customRepo.countActions(table, actionType, itemId);
|
||||
List<UserAction> actions = customRepo.findActions(request);
|
||||
int maxPage = (int)(Math.ceil ((double)count / page.getPageSize()));
|
||||
|
||||
return new PaginatedResult<>(
|
||||
new PaginationInfo(page.getPageNumber() + 1, maxPage, page.getPageSize(), count, null, null),
|
||||
new PaginationInfo(page.getPageNumber() + 1, maxPage, page.getPageSize(), count, null, request),
|
||||
actions
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user