Skip to content

Commit a44354e

Browse files
committed
feat(user): implements search into User scope
1 parent ce05151 commit a44354e

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/main/java/com/github/renancvitor/inventory/application/user/controller/UserController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public class UserController {
3535
@GetMapping
3636
public ResponseEntity<CustomPage<UserListingData>> list(@RequestParam(required = false) Boolean active,
3737
@PageableDefault(size = 10, sort = ("person.personName")) Pageable pageable,
38+
@RequestParam(required = false) String search,
3839
@AuthenticationPrincipal User loggedInUser) {
39-
var page = userService.list(pageable, loggedInUser, active);
40+
var page = userService.list(pageable, search, loggedInUser, active);
4041

4142
return ResponseEntity.ok(PageMapper.toCustomPage(page));
4243
}

src/main/java/com/github/renancvitor/inventory/application/user/repository/UserRepository.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import java.util.Optional;
44

5-
import org.springframework.data.domain.Pageable;
65
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
77
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
9+
import org.springframework.data.repository.query.Param;
810

911
import com.github.renancvitor.inventory.domain.entity.user.User;
1012

@@ -21,4 +23,14 @@ public interface UserRepository extends JpaRepository<User, Long> {
2123
Page<User> findAllByActive(Boolean active, Pageable pageable);
2224

2325
Optional<User> findByIdAndFirstAccessTrue(Long id);
26+
27+
@Query("""
28+
SELECT u FROM User u
29+
WHERE
30+
LOWER(u.person.personName) LIKE LOWER(CONCAT('%', :search, '%'))
31+
OR LOWER(u.person.email) LIKE LOWER(CONCAT('%', :search, '%'))
32+
OR LOWER(u.userType.userTypeName) LIKE LOWER(CONCAT('%', :search, '%'))
33+
""")
34+
Page<User> search(@Param("search") String search, Pageable pageable);
35+
2436
}

src/main/java/com/github/renancvitor/inventory/application/user/service/UserService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ public class UserService {
3939
private final AuthenticationService authenticationService;
4040
private final SystemLogPublisherService logPublisherService;
4141

42-
public Page<UserListingData> list(Pageable pageable, User loggedInUser, Boolean active) {
42+
public Page<UserListingData> list(Pageable pageable, String search, User loggedInUser, Boolean active) {
4343
authenticationService.authorize(List.of(UserTypeEnum.ADMIN));
4444

45+
if (search != null && !search.isBlank()) {
46+
return userRepository.search(search, pageable).map(UserListingData::new);
47+
}
48+
4549
if (active != null) {
4650
return userRepository.findAllByActive(active, pageable).map(UserListingData::new);
4751
}

0 commit comments

Comments
 (0)