Skip to content

Commit

Permalink
feat: list contact type codes
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoMarchiEncora committed Feb 13, 2023
1 parent 13c32d5 commit 4bdd766
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ca.bc.gov.app.dto.client;

import java.time.LocalDate;

public record ContactTypeCodeDto(
String code,
String description,
LocalDate effectiveDate,
LocalDate expiryDate) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ca.bc.gov.app.entity.client;

import ca.bc.gov.app.ApplicationConstant;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;

@Table(name = "contact_type_code", schema = ApplicationConstant.POSTGRES_ATTRIBUTE_SCHEMA)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@With
public class ContactTypeCodeEntity {
@Id
@Column("contact_type_code")
private String contactTypeCode;

@NotNull
@Column("description")
private String description;

@NotNull
@Column("effective_date")
private LocalDate effectiveAt;

@NotNull
@Column("expiry_date")
private LocalDate expiredAt;

@Column("create_timestamp")
private LocalDateTime createdAt;

@Column("update_timestamp")
private LocalDateTime updatedAt;

@Column("create_user")
private String createdBy;

@Column("update_user")
private String updatedBy;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder;
import static org.springdoc.core.fn.builders.exampleobject.Builder.exampleOjectBuilder;
import static org.springdoc.core.fn.builders.header.Builder.headerBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;
import static org.springdoc.core.fn.builders.schema.Builder.schemaBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package ca.bc.gov.app.handlers.client;

import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
import static org.springdoc.core.fn.builders.arrayschema.Builder.arraySchemaBuilder;
import static org.springdoc.core.fn.builders.content.Builder.contentBuilder;
import static org.springdoc.core.fn.builders.parameter.Builder.parameterBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;
import static org.springdoc.core.fn.builders.schema.Builder.schemaBuilder;

import ca.bc.gov.app.dto.client.ContactTypeCodeDto;
import ca.bc.gov.app.handlers.BaseHandler;
import ca.bc.gov.app.service.client.ClientService;
import ca.bc.gov.app.util.HandlerUtil;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.fn.builders.operation.Builder;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ResponseStatusException;
import reactor.core.publisher.Mono;

@Component
@Slf4j
@RequiredArgsConstructor
public class ContactTypeCodeHandler implements BaseHandler {

private final ClientService clientService;


@Override
public Mono<ServerResponse> handle(ServerRequest serverRequest) {
return
ServerResponse
.ok()
.body(
clientService
.listClientContactTypeCodes(
serverRequest
.queryParam("page")
.map(Integer::parseInt)
.orElse(0),
serverRequest
.queryParam("size")
.map(Integer::parseInt)
.orElse(5)
),
ContactTypeCodeHandler.class
)
.doOnError(ResponseStatusException.class, HandlerUtil.handleStatusResponse())
.doOnError(HandlerUtil.handleError());
}

@Override
public Consumer<Builder> documentation(String tag) {
return ops -> ops
.tag(tag)
.description("List contact type codes")
.beanClass(ContactTypeCodeHandler.class)
.beanMethod("handle")
.operationId("handle")
.requestBody(requestBodyBuilder())
.parameter(
parameterBuilder()
.in(ParameterIn.QUERY)
.name("page")
.description("""
0 index page number, for example:
0 for the first page, 4 for the fifth page.
Defaults to 0 (first page)""")
.allowEmptyValue(true)
.schema(schemaBuilder().implementation(String.class))
.example("4")
)
.parameter(
parameterBuilder()
.in(ParameterIn.QUERY)
.name("size")
.description("Amount of entries per page,default to 5")
.allowEmptyValue(true)
.schema(schemaBuilder().implementation(String.class))
.example("5")
)
.response(
responseBuilder()
.responseCode("200")
.description("OK - List a page of contact type codes")
.content(
contentBuilder()
.array(
arraySchemaBuilder()
.schema(
schemaBuilder()
.name("ContactTypeCode")
.implementation(ContactTypeCodeDto.class)
)
)
.mediaType(MediaType.APPLICATION_JSON_VALUE)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ca.bc.gov.app.repository.client;

import ca.bc.gov.app.entity.client.ContactTypeCodeEntity;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;

@Repository
public interface ContactTypeCodeRepository
extends ReactiveCrudRepository<ContactTypeCodeEntity, String>,
ReactiveSortingRepository<ContactTypeCodeEntity, String> {

Flux<ContactTypeCodeEntity> findBy(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import static org.springdoc.webflux.core.fn.SpringdocRouteBuilder.route;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;

import ca.bc.gov.app.handlers.client.ClientProvinceCodeHandler;
import ca.bc.gov.app.handlers.client.ClientCountryCodeHandler;
import ca.bc.gov.app.handlers.client.ClientProvinceCodeHandler;
import ca.bc.gov.app.handlers.client.ClientTypeCodeHandler;
import ca.bc.gov.app.handlers.client.ContactTypeCodeHandler;
import ca.bc.gov.app.routes.BaseRouter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
Expand All @@ -21,6 +22,8 @@ public class ClientRouter implements BaseRouter {
private final ClientTypeCodeHandler clientHandler;
private final ClientCountryCodeHandler countryCodeHandler;

private final ContactTypeCodeHandler contactTypeCodeHandler;

@Override
public String basePath() {
return "/clients";
Expand Down Expand Up @@ -57,7 +60,12 @@ public RouterFunction<ServerResponse> routerRoute() {
provinceCodeHandler::handle,
provinceCodeHandler.documentation(routeTagName())
)
.GET(
"contact-type-codes",
accept(MediaType.ALL),
contactTypeCodeHandler::handle,
clientHandler.documentation(routeTagName())
)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import ca.bc.gov.app.dto.client.ClientCodeTypeDto;
import ca.bc.gov.app.dto.client.ClientNameCodeDto;
import ca.bc.gov.app.dto.client.ContactTypeCodeDto;
import ca.bc.gov.app.repository.client.ClientTypeCodeRepository;
import ca.bc.gov.app.repository.client.ContactTypeCodeRepository;
import ca.bc.gov.app.repository.client.CountryCodeRepository;
import ca.bc.gov.app.repository.client.ProvinceCodeRepository;
import java.time.LocalDate;
Expand All @@ -18,6 +20,7 @@ public class ClientService {
private final ClientTypeCodeRepository clientTypeCodeRepository;
private final CountryCodeRepository countryCodeRepository;
private final ProvinceCodeRepository provinceCodeRepository;
private final ContactTypeCodeRepository contactTypeCodeRepository;

/**
* <p><b>Find Active Client Type Codes</b></p>
Expand All @@ -29,6 +32,8 @@ public class ClientService {
* @param targetDate The date to be used as reference.
* @return A list of {@link ClientCodeTypeDto}
*/


public Flux<ClientCodeTypeDto> findActiveClientTypeCodes(LocalDate targetDate) {

return
Expand All @@ -45,8 +50,12 @@ public Flux<ClientCodeTypeDto> findActiveClientTypeCodes(LocalDate targetDate) {

/**
* <p><b>List countries</b></p>
<<<<<<< HEAD
* <p>List countries by page with a defined size.
* The list will be sorted by order and country name.</p>
=======
* List countries by page with a defined size. The list will be sorted by order and country name.
>>>>>>> ff164a4 (feat: list contact type codes)
*
* @param page The page number, it is a 0-index base.
* @param size The amount of entries per page.
Expand Down Expand Up @@ -74,5 +83,21 @@ public Flux<ClientNameCodeDto> listProvinces(String countryCode, int page, int s
.map(entity -> new ClientNameCodeDto(entity.getProvinceCode(), entity.getDescription()));
}


/**
* <p><b>List contact types</b></p>
* List contact type codes by page with a defined size.
*
* @param page The page number, it is a 0-index base.
* @param size The amount of entries per page.
* @return A list of {@link ContactTypeCodeDto} entries.
*/
public Flux<ContactTypeCodeDto> listClientContactTypeCodes(int page, int size) {
return contactTypeCodeRepository
.findBy(PageRequest.of(page, size))
.map(entity -> new ContactTypeCodeDto(
entity.getContactTypeCode(),
entity.getDescription(),
entity.getEffectiveAt(),
entity.getExpiredAt()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void shouldListCodesAsExpected() {
@ParameterizedTest(name = "{2} - {3} is the first on page {0} with size {1}")
@MethodSource("countryCode")
@DisplayName("List countries by")
void shouldListCountryData(Integer page, Integer size,String code, String name) {
void shouldListCountryData(Integer page, Integer size, String code, String name) {

//This is to allow parameter to be ommitted during test
Function<UriBuilder, URI> uri = uriBuilder -> {
Expand Down Expand Up @@ -78,7 +78,8 @@ void shouldListCountryData(Integer page, Integer size,String code, String name)
@ParameterizedTest(name = "{3} - {4} is the first on page {1} with size {2} for country {0}")
@MethodSource("provinceCode")
@DisplayName("List provinces by")
void shouldListProvinceData(String countryCode,Integer page, Integer size,String code, String name) {
void shouldListProvinceData(String countryCode, Integer page, Integer size, String code,
String name) {

//This is to allow parameter to be ommitted during test
Function<UriBuilder, URI> uri = uriBuilder -> {
Expand All @@ -93,7 +94,7 @@ void shouldListProvinceData(String countryCode,Integer page, Integer size,String
localBuilder = localBuilder.queryParam("size", size);
}

return localBuilder.build(Map.of("countryCode",countryCode));
return localBuilder.build(Map.of("countryCode", countryCode));
};

client
Expand All @@ -111,22 +112,63 @@ void shouldListProvinceData(String countryCode,Integer page, Integer size,String
private static Stream<Arguments> countryCode() {
return
Stream.of(
Arguments.of(null,null,"CA","Canada"),
Arguments.of(0,1,"CA","Canada"),
Arguments.of(1,1,"US","United States of America"),
Arguments.of(7,null,"BN","Brunei Darussalam"),
Arguments.of(3,10,"BA","Bosnia and Herzegovina"),
Arguments.of(33,1,"BR","Brazil"),
Arguments.of(49,1,"CO","Colombia")
Arguments.of(null, null, "CA", "Canada"),
Arguments.of(0, 1, "CA", "Canada"),
Arguments.of(1, 1, "US", "United States of America"),
Arguments.of(7, null, "BN", "Brunei Darussalam"),
Arguments.of(3, 10, "BA", "Bosnia and Herzegovina"),
Arguments.of(33, 1, "BR", "Brazil"),
Arguments.of(49, 1, "CO", "Colombia")
);
}

private static Stream<Arguments> provinceCode() {
return
Stream.of(
Arguments.of("CA",null,null,"AB","Alberta"),
Arguments.of("CA",0,1,"AB","Alberta"),
Arguments.of("US",1,1,"AK","Alaska")
Arguments.of("CA", null, null, "AB", "Alberta"),
Arguments.of("CA", 0, 1, "AB", "Alberta"),
Arguments.of("US", 1, 1, "AK", "Alaska"));
}

@ParameterizedTest(name = "{2} - {3} is the first on page {0} with size {1}")
@MethodSource("contactTypeCodes")
@DisplayName("List contact type codes")
void shouldListContactTypes(Integer page, Integer size, String code, String description) {
Function<UriBuilder, URI> uri = uriBuilder -> {

UriBuilder localBuilder = uriBuilder
.path("/api/clients/contact-type-codes");

if (page != null) {
localBuilder = localBuilder.queryParam("page", page);
}
if (size != null) {
localBuilder = localBuilder.queryParam("size", size);
}

return localBuilder.build(new HashMap<>());
};

client
.get()
.uri(uri)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$[0].code").isNotEmpty()
.jsonPath("$[0].code").isEqualTo(code)
.jsonPath("$[0].code").isNotEmpty()
.jsonPath("$[0].code").isEqualTo(description);
}

private static Stream<Arguments> contactTypeCodes() {
return
Stream.of(
Arguments.of(null, null, "AP", "Accounts Payable"),
Arguments.of(0, 1, "AP", "Accounts Payable"),
Arguments.of(1, 1, "AR", "Accounts Receivable"),
Arguments.of(11, 1, "GP", "General Partner"),
Arguments.of(22, 1, "TP", "EDI Trading Partner")
);
}
}

0 comments on commit 4bdd766

Please sign in to comment.