Repository navigation
Conversation
- Add SearchController with endpoints for /v1/search, /v1/search/posts, and /v1/search/users - Add SearchService and SearchServiceImpl with query validation, wildcard escaping, and @-handle handling - Add JPQL search queries to PostsRepository and UsersRepository - Add SearchResultModel DTO record and wire MapStruct mappers - Add ControllerExceptionHandler mapping for MissingServletRequestParameterException - Add unit tests for SearchService and SearchController, and security and integration tests - Quote Surefire argLine in pom.xml to prevent JVM agent path-splitting on paths with spaces Signed-off-by: Vicky Patel <vickybiz55808@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
This pull request implements the search endpoints requested in #19. It introduces dedicated REST endpoints for searching posts, searching users, and performing combined searches, backed by case-insensitive JPA queries and strict validation rules.
Closes #19
Architectural Design & Layering
The implementation follows the established
Controller -> Service -> Repositoryarchitecture:Controller Layer (
SearchController.java):GET /v1/search: Combined search returning a composite payload of users and posts.GET /v1/search/posts: Paginated search across post content (text).GET /v1/search/users: Paginated search across user attributes (username,name,bio).@Timed(value = "moo.search.timer")for latency monitoring.@Countedmetrics (moo.search.all,moo.search.posts,moo.search.users).Utility.validatePageable(...).Service Layer (
SearchService.java,SearchServiceImpl.java):@Transactional(readOnly = true).@characters for user handle queries so that searches for@aliceandaliceboth succeed.BadRequestException.LIKEwildcard characters (\,%,_) to guard against wildcard pattern injection.PostModel,UserModel,SearchResultModel) using existing MapStruct mappers (PostMapper,UserMapper).Repository Layer (
PostsRepository.java,UsersRepository.java):searchPosts(@Param("query") @NonNull String query, @NonNull Pageable pageable)with case-insensitivelower(...)comparison andESCAPE '\\'.searchUsers(@Param("query") @NonNull String query, @NonNull Pageable pageable)matching againstusername,name, andbiowith case-insensitivelower(...)comparison andESCAPE '\\'.Exception Handling (
ControllerExceptionHandler.java):MissingServletRequestParameterException.classtomalformedRequestExceptionHandlerto ensure missingqparameters return a clean HTTP 400 Bad Request response.Build Support (
pom.xml):-javaagentin the Maven Surefire configuration to prevent JVM argument splitting on directory paths containing spaces.Edge Cases and Defenses
\,%, and_characters to prevent wildcard denial-of-service and unintended full table scans.qreturns HTTP 400. Blank or whitespace-only queries return HTTP 400. Queries exceeding 100 characters return HTTP 400.password) are caught byUtility.validatePageableand return HTTP 400.lower(...)wrapping in JPQL guarantees consistent case-insensitive searches across MySQL and H2 database collations./v3/api-docsremain accessible.Verification and Test Coverage
SearchServiceImplTest: Tests blank queries, queries over 100 characters, whitespace trimming, wildcard escaping, leading@strip, and empty page responses.SearchControllerTest: Tests controller delegation, pagination parameters, and rejection of invalid sort properties.SecurityMvcTest: Verified search endpoints require authentication.TwitterBackendApplicationTests: Verified case-insensitive search queries against the database for users, user handles, and post content../mvnw spotless:check), keeping all 77 repository files compliant with Google Java Format.