Skip to content

Commit

Permalink
fix: use metamodel for specification
Browse files Browse the repository at this point in the history
  • Loading branch information
AkagiYui committed Aug 25, 2024
1 parent 8b64443 commit 70b0425
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.akagiyui.common.ResponseEnum
import com.akagiyui.common.exception.CustomException
import com.akagiyui.common.utils.hasText
import com.akagiyui.drive.entity.Announcement
import com.akagiyui.drive.entity.Announcement_
import com.akagiyui.drive.model.AnnouncementFilter
import com.akagiyui.drive.model.request.announcement.UpdateAnnouncementRequest
import com.akagiyui.drive.repository.AnnouncementRepository
Expand Down Expand Up @@ -51,11 +52,11 @@ class AnnouncementServiceImpl(private val announcementRepository: AnnouncementRe
val pageable = PageRequest.of(index, size)

// 条件查询
val specification = Specification<Announcement> { root, _, cb ->
val specification = Specification { root, _, cb ->
filter?.expression.hasText {
val likePattern = "%$it%"
val titlePredicate = cb.like(root.get("title"), likePattern)
val contentPredicate = cb.like(root.get("content"), likePattern)
val titlePredicate = cb.like(root[Announcement_.title], likePattern)
val contentPredicate = cb.like(root[Announcement_.content], likePattern)
cb.or(titlePredicate, contentPredicate)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.akagiyui.common.delegate.LoggerDelegate
import com.akagiyui.common.exception.CustomException
import com.akagiyui.common.utils.hasText
import com.akagiyui.drive.entity.FileInfo
import com.akagiyui.drive.entity.FileInfo_
import com.akagiyui.drive.entity.User
import com.akagiyui.drive.model.CacheConstants
import com.akagiyui.drive.model.FileInfoFilter
Expand Down Expand Up @@ -123,12 +124,13 @@ class FileInfoServiceImpl(
val pageable = PageRequest.of(pageIndex, pageSize)

// 条件查询
val specification = Specification<FileInfo> { root, _, cb ->
if (filter != null && filter.expression.hasText()) {
val namePredicate = cb.like(root.get("name"), "%${filter.expression}%")
val typePredicate = cb.like(root.get("type"), "%${filter.expression}%")
val specification = Specification { root, _, cb ->
filter?.expression.hasText {
val likePattern = "%$it%"
val namePredicate = cb.like(root[FileInfo_.name], likePattern)
val typePredicate = cb.like(root[FileInfo_.type], likePattern)
cb.or(namePredicate, typePredicate)
} else null
}
}

return fileInfoRepository.findAll(specification, pageable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.akagiyui.common.delegate.LoggerDelegate
import com.akagiyui.common.exception.CustomException
import com.akagiyui.common.utils.hasText
import com.akagiyui.drive.entity.Role
import com.akagiyui.drive.entity.Role_
import com.akagiyui.drive.entity.User
import com.akagiyui.drive.model.CacheConstants
import com.akagiyui.drive.model.Permission
Expand Down Expand Up @@ -55,12 +56,13 @@ class RoleServiceImpl(private val roleRepository: RoleRepository) : RoleService
val pageable = PageRequest.of(index, size)

// 条件查询
val specification = Specification<Role> { root, _, cb ->
if (filter != null && filter.expression.hasText()) {
val namePredicate = cb.like(root.get("name"), "%${filter.expression}%")
val descriptionPredicate = cb.like(root.get("description"), "%${filter.expression}%")
val specification = Specification { root, _, cb ->
filter?.expression.hasText {
val likePattern = "%$it%"
val namePredicate = cb.like(root[Role_.name], likePattern)
val descriptionPredicate = cb.like(root[Role_.description], likePattern)
cb.or(namePredicate, descriptionPredicate)
} else null
}
}

return roleRepository.findAll(specification, pageable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.akagiyui.common.utils.hasText
import com.akagiyui.drive.component.RedisCache
import com.akagiyui.drive.entity.Role
import com.akagiyui.drive.entity.User
import com.akagiyui.drive.entity.User_
import com.akagiyui.drive.model.AddUserModel
import com.akagiyui.drive.model.CacheConstants
import com.akagiyui.drive.model.UserFilter
Expand All @@ -17,7 +18,6 @@ import com.akagiyui.drive.model.request.user.UpdateUserInfoRequest
import com.akagiyui.drive.repository.UserRepository
import com.akagiyui.drive.service.*
import jakarta.annotation.Resource
import jakarta.persistence.criteria.Predicate
import org.jetbrains.annotations.NotNull
import org.springframework.cache.annotation.CacheEvict
import org.springframework.cache.annotation.Cacheable
Expand Down Expand Up @@ -77,14 +77,14 @@ class UserServiceImpl(
override fun getUsers(index: Int, size: Int, userFilter: UserFilter?): Page<User> {
val pageable: Pageable = PageRequest.of(index, size)

val specification: Specification<User> = Specification { root, _, cb ->
if (userFilter != null && userFilter.expression.hasText()) {
val queryString = "%${userFilter.expression}%"
val usernamePredicate: Predicate = cb.like(root["username"], queryString)
val nicknamePredicate: Predicate = cb.like(root["nickname"], queryString)
val emailPredicate: Predicate = cb.like(root["email"], queryString)
val specification = Specification { root, _, cb ->
userFilter?.expression.hasText {
val queryString = "%$it%"
val usernamePredicate = cb.like(root[User_.username], queryString)
val nicknamePredicate = cb.like(root[User_.nickname], queryString)
val emailPredicate = cb.like(root[User_.email], queryString)
cb.or(usernamePredicate, nicknamePredicate, emailPredicate)
} else null
}
}

return repository.findAll(specification, pageable)
Expand Down

0 comments on commit 70b0425

Please sign in to comment.