Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import java.time.Instant
data class AuditLogFilterDto(
val fromDate: Instant? = null,
val toDate: Instant? = null,
val performedByUserId: String? = null,
val searchText: String? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package com.alleslocker.backend.persistence.auditlog.specification

import com.alleslocker.backend.application.auditlog.dto.filter.AuditLogFilterDto
import com.alleslocker.backend.persistence.auditlog.entity.AuditLogEntity
import com.alleslocker.backend.persistence.user.entity.UserEntity
import jakarta.persistence.criteria.JoinType
import jakarta.persistence.criteria.Predicate
import org.springframework.data.jpa.domain.Specification

object AuditLogSpecification {
fun withFilter(filter: AuditLogFilterDto): Specification<AuditLogEntity> =
Specification { root, _, cb ->
Specification { root, query, cb ->
val predicates = mutableListOf<Predicate>()

filter.fromDate?.let {
Expand All @@ -18,9 +20,29 @@ object AuditLogSpecification {
predicates.add(cb.lessThanOrEqualTo(root.get("createdAt"), it))
}

filter.performedByUserId?.let { userId ->
predicates.add(cb.equal(root.get<Any>("performedBy").get<String>("id"), userId))
}
filter.searchText
?.takeIf { it.isNotBlank() }
?.let { searchText ->
val isCountQuery =
query?.resultType == java.lang.Long::class.java ||
query?.resultType == Long::class.javaPrimitiveType

if (!isCountQuery) {
query?.distinct(true)
}

val pattern = "%${searchText.lowercase()}%"
Comment thread
wan0v marked this conversation as resolved.
val performedBy = root.join<AuditLogEntity, UserEntity>("performedBy", JoinType.LEFT)

predicates.add(
cb.or(
cb.like(cb.lower(root.get("message")), pattern),
cb.like(cb.lower(performedBy.get("username")), pattern),
cb.like(cb.lower(performedBy.get("firstname")), pattern),
cb.like(cb.lower(performedBy.get("lastname")), pattern),
),
)
}

cb.and(*predicates.toTypedArray())
}
Expand Down
Loading