Fixed Log filters and fixed chat attachment download
This commit is contained in:
@@ -23,8 +23,11 @@ public class ActivityLogController {
|
||||
|
||||
@GetMapping
|
||||
public ResponseEntity<List<ActivityLogResponse>> getActivityLogs(
|
||||
@RequestParam(defaultValue = "2000") int limit) {
|
||||
@RequestParam(defaultValue = "2000") int limit,
|
||||
@RequestParam(required = false) Long storeId,
|
||||
@RequestParam(required = false) String role,
|
||||
@RequestParam(required = false) String search) {
|
||||
int safeLimit = Math.min(Math.max(1, limit), 10000);
|
||||
return ResponseEntity.ok(activityLogService.getLogs(safeLimit));
|
||||
return ResponseEntity.ok(activityLogService.getLogs(safeLimit, storeId, role, search));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,15 @@ package com.petshop.backend.repository;
|
||||
|
||||
import com.petshop.backend.entity.ActivityLog;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface ActivityLogRepository extends JpaRepository<ActivityLog, Long> {
|
||||
public interface ActivityLogRepository extends JpaRepository<ActivityLog, Long>, JpaSpecificationExecutor<ActivityLog> {
|
||||
boolean existsByUser_Id(Long userId);
|
||||
|
||||
@Query("select a from ActivityLog a order by a.logTimestamp desc, a.logId desc")
|
||||
|
||||
@@ -6,12 +6,16 @@ import com.petshop.backend.entity.StoreLocation;
|
||||
import com.petshop.backend.entity.User;
|
||||
import com.petshop.backend.repository.ActivityLogRepository;
|
||||
import com.petshop.backend.repository.UserRepository;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -60,9 +64,41 @@ public class ActivityLogService {
|
||||
record(user.getId(), activity);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ActivityLogResponse> getLogs(int limit, Long storeId, String role, String search) {
|
||||
Specification<ActivityLog> spec = (root, query, cb) -> {
|
||||
List<Predicate> predicates = new ArrayList<>();
|
||||
|
||||
if (storeId != null) {
|
||||
predicates.add(cb.equal(root.get("store").get("storeId"), storeId));
|
||||
}
|
||||
|
||||
if (role != null && !role.isBlank()) {
|
||||
predicates.add(cb.equal(root.get("roleSnapshot"), role));
|
||||
}
|
||||
|
||||
if (search != null && !search.isBlank()) {
|
||||
String pattern = "%" + search.toLowerCase() + "%";
|
||||
Predicate searchPredicate = cb.or(
|
||||
cb.like(cb.lower(root.get("activity")), pattern),
|
||||
cb.like(cb.lower(root.get("fullNameSnapshot")), pattern),
|
||||
cb.like(cb.lower(root.get("usernameSnapshot")), pattern)
|
||||
);
|
||||
predicates.add(searchPredicate);
|
||||
}
|
||||
|
||||
return cb.and(predicates.toArray(new Predicate[0]));
|
||||
};
|
||||
|
||||
PageRequest pageRequest = PageRequest.of(0, limit, Sort.by(Sort.Direction.DESC, "logTimestamp", "logId"));
|
||||
return activityLogRepository.findAll(spec, pageRequest).stream()
|
||||
.map(this::toResponse)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ActivityLogResponse> getLogs(int limit) {
|
||||
return activityLogRepository.findRecent(PageRequest.of(0, limit)).stream().map(this::toResponse).toList();
|
||||
return getLogs(limit, null, null, null);
|
||||
}
|
||||
|
||||
private ActivityLogResponse toResponse(ActivityLog entry) {
|
||||
|
||||
Reference in New Issue
Block a user