renamed SearchResponse to ServiceItem and moved to shared package
This commit is contained in:
@@ -14,6 +14,7 @@ import com.vaessl.app.connection.ConnectionRepository;
|
|||||||
import com.vaessl.app.connection.HomeboxEntity;
|
import com.vaessl.app.connection.HomeboxEntity;
|
||||||
import com.vaessl.app.exception.ConnectionNotFoundException;
|
import com.vaessl.app.exception.ConnectionNotFoundException;
|
||||||
import com.vaessl.app.exception.RemoteApiException;
|
import com.vaessl.app.exception.RemoteApiException;
|
||||||
|
import com.vaessl.app.shared.ServiceItem;
|
||||||
import com.vaessl.app.shared.ServiceType;
|
import com.vaessl.app.shared.ServiceType;
|
||||||
|
|
||||||
import static com.vaessl.app.shared.Endpoint.*;
|
import static com.vaessl.app.shared.Endpoint.*;
|
||||||
@@ -37,7 +38,7 @@ public class HomeboxSearchProvider implements SearchProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page<SearchResponse> getSearchResults(SearchRequest request, Pageable pageable) {
|
public Page<ServiceItem> getSearchResults(SearchRequest request, Pageable pageable) {
|
||||||
|
|
||||||
ConnectionEntity entity =
|
ConnectionEntity entity =
|
||||||
cRepository.findByAppUrlAndUsername(request.appUrl(), request.username());
|
cRepository.findByAppUrlAndUsername(request.appUrl(), request.username());
|
||||||
@@ -58,12 +59,12 @@ public class HomeboxSearchProvider implements SearchProvider {
|
|||||||
throw new RemoteApiException(request.appUrl(), HOMEBOX_QUERY_ALL_ITEMS.getValue());
|
throw new RemoteApiException(request.appUrl(), HOMEBOX_QUERY_ALL_ITEMS.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
List<SearchResponse> items = hbResponse.items().stream().map(i -> {
|
List<ServiceItem> items = hbResponse.items().stream().map(i -> {
|
||||||
String id = i.id();
|
String id = i.id();
|
||||||
String title = i.name();
|
String title = i.name();
|
||||||
String description = i.description();
|
String description = i.description();
|
||||||
Map<String, Object> extraSearchResponseData = Map.of("location", i.parent());
|
Map<String, Object> extraSearchResponseData = Map.of("location", i.parent());
|
||||||
return new SearchResponse(id, title, description, extraSearchResponseData);
|
return new ServiceItem(id, title, description, extraSearchResponseData);
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
return new PageImpl<>(items, pageable, hbResponse.total());
|
return new PageImpl<>(items, pageable, hbResponse.total());
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import com.vaessl.app.shared.ServiceItem;
|
||||||
import com.vaessl.app.shared.SessionKeys;
|
import com.vaessl.app.shared.SessionKeys;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpSession;
|
import jakarta.servlet.http.HttpSession;
|
||||||
@@ -26,7 +26,7 @@ public class SearchController {
|
|||||||
* there is no active session.
|
* there is no active session.
|
||||||
*/
|
*/
|
||||||
@PostMapping("/search")
|
@PostMapping("/search")
|
||||||
public ResponseEntity<PagedSearchResponse<SearchResponse>> search(
|
public ResponseEntity<PagedSearchResponse<ServiceItem>> search(
|
||||||
@Valid @RequestBody SearchRequest request,
|
@Valid @RequestBody SearchRequest request,
|
||||||
@PageableDefault(size = 20) Pageable pageable, HttpServletRequest httpReq) {
|
@PageableDefault(size = 20) Pageable pageable, HttpServletRequest httpReq) {
|
||||||
HttpSession session = httpReq.getSession(false);
|
HttpSession session = httpReq.getSession(false);
|
||||||
@@ -35,7 +35,7 @@ public class SearchController {
|
|||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
Page<SearchResponse> result = searchService.search(request, pageable);
|
Page<ServiceItem> result = searchService.search(request, pageable);
|
||||||
|
|
||||||
return ResponseEntity.ok(PagedSearchResponse.from(result));
|
return ResponseEntity.ok(PagedSearchResponse.from(result));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package com.vaessl.app.search;
|
|||||||
|
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import com.vaessl.app.shared.ServiceItem;
|
||||||
import com.vaessl.app.shared.ServiceProvider;
|
import com.vaessl.app.shared.ServiceProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,5 +17,5 @@ public interface SearchProvider extends ServiceProvider {
|
|||||||
* @param pageable the Pageable interface
|
* @param pageable the Pageable interface
|
||||||
* @return a list of Page<SearchResponse> items matching the query
|
* @return a list of Page<SearchResponse> items matching the query
|
||||||
*/
|
*/
|
||||||
Page<SearchResponse> getSearchResults(SearchRequest request, Pageable pageable);
|
Page<ServiceItem> getSearchResults(SearchRequest request, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.Map;
|
|||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import com.vaessl.app.shared.ServiceItem;
|
||||||
import com.vaessl.app.shared.ServiceType;
|
import com.vaessl.app.shared.ServiceType;
|
||||||
import com.vaessl.app.exception.WrongServiceTypeException;
|
import com.vaessl.app.exception.WrongServiceTypeException;
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ public class SearchService {
|
|||||||
* @return results returned by the matching provider
|
* @return results returned by the matching provider
|
||||||
* @throws WrongServiceTypeException if no provider is registered for the given service type
|
* @throws WrongServiceTypeException if no provider is registered for the given service type
|
||||||
*/
|
*/
|
||||||
public Page<SearchResponse> search(SearchRequest request, Pageable pageable) {
|
public Page<ServiceItem> search(SearchRequest request, Pageable pageable) {
|
||||||
|
|
||||||
SearchProvider provider = providerRegistry.get(request.serviceType());
|
SearchProvider provider = providerRegistry.get(request.serviceType());
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -1,8 +1,9 @@
|
|||||||
package com.vaessl.app.search;
|
package com.vaessl.app.shared;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
public record SearchResponse(String id, String title, String description,
|
public record ServiceItem(String id, @NotNull String title, String description,
|
||||||
Map<String, Object> extraData) {
|
Map<String, Object> extraData) {
|
||||||
|
|
||||||
public String getExtra(String key) {
|
public String getExtra(String key) {
|
||||||
@@ -4,12 +4,13 @@ import static com.vaessl.app.Mockdata.*;
|
|||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import com.vaessl.app.shared.ServiceItem;
|
||||||
|
|
||||||
class SearchResponseTest {
|
class SearchResponseTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldReturnNullWhenExtraDataIsNull() {
|
void shouldReturnNullWhenExtraDataIsNull() {
|
||||||
SearchResponse response = new SearchResponse(MOCK_ID, MOCK_TITLE, MOCK_DESCRIPTION, null);
|
ServiceItem response = new ServiceItem(MOCK_ID, MOCK_TITLE, MOCK_DESCRIPTION, null);
|
||||||
|
|
||||||
assertThat(response.getExtra(null)).isNull();
|
assertThat(response.getExtra(null)).isNull();
|
||||||
}
|
}
|
||||||
@@ -17,7 +18,7 @@ class SearchResponseTest {
|
|||||||
@Test
|
@Test
|
||||||
void shouldReturnNullWhenExtraDataKeyIsMissing() {
|
void shouldReturnNullWhenExtraDataKeyIsMissing() {
|
||||||
|
|
||||||
SearchResponse response = new SearchResponse(MOCK_ID, MOCK_TITLE, MOCK_DESCRIPTION, Map.of("key", "value"));
|
ServiceItem response = new ServiceItem(MOCK_ID, MOCK_TITLE, MOCK_DESCRIPTION, Map.of("key", "value"));
|
||||||
|
|
||||||
assertThat(response.getExtra("missing")).isNull();
|
assertThat(response.getExtra("missing")).isNull();
|
||||||
}
|
}
|
||||||
@@ -25,7 +26,7 @@ class SearchResponseTest {
|
|||||||
@Test
|
@Test
|
||||||
void shouldReturnExtraDataValue() {
|
void shouldReturnExtraDataValue() {
|
||||||
|
|
||||||
SearchResponse response = new SearchResponse(MOCK_ID, MOCK_TITLE, MOCK_DESCRIPTION, Map.of("key", "value"));
|
ServiceItem response = new ServiceItem(MOCK_ID, MOCK_TITLE, MOCK_DESCRIPTION, Map.of("key", "value"));
|
||||||
|
|
||||||
assertThat(response.id()).isEqualTo(MOCK_ID);
|
assertThat(response.id()).isEqualTo(MOCK_ID);
|
||||||
assertThat(response.getExtra("key")).contains("value");
|
assertThat(response.getExtra("key")).contains("value");
|
||||||
|
|||||||
Reference in New Issue
Block a user