This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
forked from Onlineberatung/onlineBeratung-agencyService
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide hal based structure for agency search
- Loading branch information
mebo4b
committed
Nov 12, 2020
1 parent
20474d5
commit b446c7d
Showing
16 changed files
with
435 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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/de/caritas/cob/agencyservice/api/admin/hallink/HalLinkBuilder.java
This file contains 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 de.caritas.cob.agencyservice.api.admin.hallink; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; | ||
|
||
import de.caritas.cob.agencyservice.api.model.HalLink; | ||
import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum; | ||
import org.springframework.hateoas.Link; | ||
import org.springframework.http.HttpEntity; | ||
|
||
public interface HalLinkBuilder { | ||
|
||
default HalLink buildHalLink(HttpEntity<?> httpEntity, MethodEnum method) { | ||
Link link = linkTo(httpEntity).withSelfRel(); | ||
HalLink halLink = new HalLink(); | ||
halLink.setHref(link.getHref()); | ||
halLink.setMethod(method); | ||
halLink.setTemplated(link.isTemplated()); | ||
return halLink; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/de/caritas/cob/agencyservice/api/admin/hallink/RootDTOBuilder.java
This file contains 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,42 @@ | ||
package de.caritas.cob.agencyservice.api.admin.hallink; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; | ||
|
||
import de.caritas.cob.agencyservice.api.model.HalLink; | ||
import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum; | ||
import de.caritas.cob.agencyservice.api.model.RootDTO; | ||
import de.caritas.cob.agencyservice.api.model.RootLinks; | ||
import de.caritas.cob.agencyservice.generated.api.controller.AdminApi; | ||
import de.caritas.cob.agencyservice.generated.api.controller.DefaultApi; | ||
|
||
/** | ||
* Builder to create the root navigation hal DTO. | ||
*/ | ||
public class RootDTOBuilder implements HalLinkBuilder { | ||
|
||
private static final Integer DEFAULT_PAGE = 1; | ||
private static final Integer DEFAULT_PER_PAGE = 20; | ||
|
||
/** | ||
* Builds the root navigation DTO. | ||
* | ||
* @return the {@link RootDTO} containing hal links | ||
*/ | ||
public RootDTO buildRootDTO() { | ||
return new RootDTO() | ||
.links(new RootLinks() | ||
.self(buildSelfLink()) | ||
.agencies(buildSearchLink())); | ||
} | ||
|
||
private HalLink buildSelfLink() { | ||
return buildHalLink(methodOn(DefaultApi.class).getRoot(), MethodEnum.GET); | ||
} | ||
|
||
private HalLink buildSearchLink() { | ||
return buildHalLink( | ||
methodOn(AdminApi.class).searchAgencies(DEFAULT_PAGE, DEFAULT_PER_PAGE, null), | ||
MethodEnum.GET); | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/de/caritas/cob/agencyservice/api/admin/hallink/SearchResultLinkBuilder.java
This file contains 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,100 @@ | ||
package de.caritas.cob.agencyservice.api.admin.hallink; | ||
|
||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn; | ||
|
||
import de.caritas.cob.agencyservice.api.model.HalLink; | ||
import de.caritas.cob.agencyservice.api.model.HalLink.MethodEnum; | ||
import de.caritas.cob.agencyservice.api.model.SearchResultLinks; | ||
import de.caritas.cob.agencyservice.generated.api.controller.AdminApi; | ||
|
||
/** | ||
* Link builder to create hal links for search results. | ||
*/ | ||
public class SearchResultLinkBuilder implements HalLinkBuilder { | ||
|
||
private Integer page; | ||
private Integer perPage; | ||
private String keyword; | ||
|
||
private SearchResultLinkBuilder() {} | ||
|
||
/** | ||
* Creates an {@link SearchResultLinkBuilder} instance. | ||
* | ||
* @return a instance of {@link SearchResultLinkBuilder} | ||
*/ | ||
public static SearchResultLinkBuilder getInstance() { | ||
return new SearchResultLinkBuilder(); | ||
} | ||
|
||
/** | ||
* Sets the page param. | ||
* | ||
* @param page the page value for building links | ||
* @return the current {@link SearchResultLinkBuilder} | ||
*/ | ||
public SearchResultLinkBuilder withPage(Integer page) { | ||
this.page = page; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the perPage param. | ||
* | ||
* @param perPage the amount value of results per page for building links | ||
* @return the current {@link SearchResultLinkBuilder} | ||
*/ | ||
public SearchResultLinkBuilder withPerPage(Integer perPage) { | ||
this.perPage = perPage; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the keyword search param. | ||
* | ||
* @param keyword the keyword value for building links | ||
* @return the current {@link SearchResultLinkBuilder} | ||
*/ | ||
public SearchResultLinkBuilder withKeyword(String keyword) { | ||
this.keyword = keyword; | ||
return this; | ||
} | ||
|
||
/** | ||
* Creates the {@link SearchResultLinks}. | ||
* | ||
* @return the generated {@link SearchResultLinks} | ||
*/ | ||
public SearchResultLinks buildSearchResultLinks() { | ||
return new SearchResultLinks() | ||
.self(buildSelfLink()) | ||
.next(buildNextLink()) | ||
.previous(buildPreviousLink()) | ||
.search(buildSearchLink()); | ||
} | ||
|
||
private HalLink buildSelfLink() { | ||
return buildHalLinkForParams(this.page, this.perPage, this.keyword); | ||
} | ||
|
||
private HalLink buildHalLinkForParams(Integer page, Integer perPage, String keyword) { | ||
return buildHalLink(methodOn(AdminApi.class).searchAgencies(page, perPage, keyword), | ||
MethodEnum.GET); | ||
} | ||
|
||
private HalLink buildNextLink() { | ||
return buildHalLinkForParams(this.page + 1, this.perPage, this.keyword); | ||
} | ||
|
||
private HalLink buildPreviousLink() { | ||
if (this.page > 1) { | ||
return buildHalLinkForParams(this.page - 1, this.perPage, this.keyword); | ||
} | ||
return null; | ||
} | ||
|
||
private HalLink buildSearchLink() { | ||
return buildHalLinkForParams(this.page, this.perPage, null); | ||
} | ||
|
||
} |
Oops, something went wrong.