-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/endpoint usersfilter #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0a06649
feat(endpoint-userfilter): add the endpoint
romainVanhee 21a194d
feat(endpoint-userfilter): add enum for gender
romainVanhee cf5a5ba
feat(endpoint-userfilter): update readme
romainVanhee b748631
feat(endpoint-userfilter): add test for usecase and services
romainVanhee cac7c1c
feat(endpoint-userfilter): add jpa for api criteria
romainVanhee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ain/java/com/xpeho/spring_boot_java_random_user/data/sources/database/UserRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,29 @@ | ||
| package com.xpeho.spring_boot_java_random_user.data.sources.database; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.data.models.database.User; | ||
| import org.springframework.data.jdbc.repository.query.Query; | ||
| import org.springframework.data.repository.CrudRepository; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface UserRepository extends CrudRepository<User, Long> { | ||
|
|
||
| @Query("SELECT * FROM users WHERE " + | ||
| "(:gender IS NULL OR LOWER(gender) = LOWER(:gender)) AND " + | ||
| "(:firstname IS NULL OR LOWER(firstname) LIKE LOWER(CONCAT('%', :firstname, '%'))) AND " + | ||
| "(:lastname IS NULL OR LOWER(lastname) LIKE LOWER(CONCAT('%', :lastname, '%'))) AND " + | ||
| "(:civility IS NULL OR civility LIKE CONCAT('%', :civility, '%')) AND " + | ||
| "(:email IS NULL OR email LIKE CONCAT('%', :email, '%')) AND " + | ||
| "(:phone IS NULL OR phone LIKE CONCAT('%', :phone, '%')) AND " + | ||
| "(:nationality IS NULL OR LOWER(nationality) LIKE LOWER(CONCAT('%', :nationality, '%')))") | ||
| List<User> findByFilters( | ||
| @Param("gender") String gender, | ||
| @Param("firstname") String firstname, | ||
| @Param("lastname") String lastname, | ||
| @Param("civility") String civility, | ||
| @Param("email") String email, | ||
| @Param("phone") String phone, | ||
| @Param("nationality") String nationality | ||
| ); | ||
| } | ||
14 changes: 14 additions & 0 deletions
14
src/main/java/com/xpeho/spring_boot_java_random_user/domain/entities/UserFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.entities; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.domain.enums.Gender; | ||
|
|
||
| public record UserFilter( | ||
| Gender gender, | ||
| String firstname, | ||
| String lastname, | ||
| String civility, | ||
| String email, | ||
| String phone, | ||
| String nat | ||
| ) { | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/xpeho/spring_boot_java_random_user/domain/enums/Gender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.enums; | ||
|
|
||
| public enum Gender { | ||
| MALE, | ||
| FEMALE | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/xpeho/spring_boot_java_random_user/domain/usecases/FilterUsersUseCase.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.usecases; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity; | ||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserFilter; | ||
| import com.xpeho.spring_boot_java_random_user.domain.services.LocalUserService; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class FilterUsersUseCase { | ||
| private final LocalUserService userService; | ||
|
|
||
| public FilterUsersUseCase(LocalUserService userService) { | ||
| this.userService = userService; | ||
| } | ||
|
|
||
| public List<UserEntity> execute(UserFilter filter) { | ||
| return userService.filterUsers(filter); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...t/java/com/xpeho/spring_boot_java_random_user/domain/usecases/FilterUsersUseCaseTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.xpeho.spring_boot_java_random_user.domain.usecases; | ||
|
|
||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserEntity; | ||
| import com.xpeho.spring_boot_java_random_user.domain.entities.UserFilter; | ||
| import com.xpeho.spring_boot_java_random_user.domain.enums.Gender; | ||
| import com.xpeho.spring_boot_java_random_user.domain.services.LocalUserService; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| class FilterUsersUseCaseTest { | ||
| private LocalUserService userService; | ||
| private FilterUsersUseCase useCase; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| userService = mock(LocalUserService.class); | ||
| useCase = new FilterUsersUseCase(userService); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should return filtered users matching the filter") | ||
| void shouldReturnFilteredUsers() { | ||
| UserFilter filter = new UserFilter(Gender.MALE, "John", null, null, null, null, null); | ||
| List<UserEntity> expected = List.of( | ||
| new UserEntity(1L, "male", "John", "Doe", "Mr", "john@example.com", "0600000000", "http://pic.jpg", "FR") | ||
| ); | ||
| when(userService.filterUsers(filter)).thenReturn(expected); | ||
|
|
||
| List<UserEntity> result = useCase.execute(filter); | ||
|
|
||
| assertEquals(expected, result); | ||
| verify(userService).filterUsers(filter); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should return empty list when no users match the filter") | ||
| void shouldReturnEmptyListWhenNoMatch() { | ||
| UserFilter filter = new UserFilter(Gender.FEMALE, "Unknown", null, null, null, null, null); | ||
| when(userService.filterUsers(filter)).thenReturn(Collections.emptyList()); | ||
|
|
||
| List<UserEntity> result = useCase.execute(filter); | ||
|
|
||
| assertTrue(result.isEmpty()); | ||
| verify(userService).filterUsers(filter); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Should pass filter with all fields to the service") | ||
| void shouldPassFilterWithAllFields() { | ||
| UserFilter filter = new UserFilter(Gender.FEMALE, "Alice", "Smith", "Ms", "alice@example.com", "0611111111", "US"); | ||
| List<UserEntity> expected = List.of( | ||
| new UserEntity(5L, "female", "Alice", "Smith", "Ms", "alice@example.com", "0611111111", "http://pic2.jpg", "US") | ||
| ); | ||
| when(userService.filterUsers(filter)).thenReturn(expected); | ||
|
|
||
| List<UserEntity> result = useCase.execute(filter); | ||
|
|
||
| assertEquals(expected, result); | ||
| verify(userService, times(1)).filterUsers(filter); | ||
| verifyNoMoreInteractions(userService); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.