Skip to content

Commit

Permalink
[FSADT1-748] Create general search API (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoMarchiEncora authored May 4, 2023
1 parent 49c68d9 commit c40a97d
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ca.bc.gov.api.oracle.legacy.dto.ClientLocationDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientPublicViewDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientViewDto;
import ca.bc.gov.api.oracle.legacy.service.ClientLocationService;
import ca.bc.gov.api.oracle.legacy.service.ClientService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -83,6 +84,51 @@ public Mono<ClientPublicViewDto> findByClientNumber(
return clientService.findByClientNumber(clientNumber);
}

@GetMapping("/findByClientNumberOrName/{clientNumberOrName}")
@Operation(
summary = "Search clients by client number or client name. It will return active and inactive",
responses = {
@ApiResponse(
responseCode = "200",
description = "Returns a client based on it's number or name",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(
name = "ClientView",
implementation = ClientPublicViewDto.class
)
)
),
@ApiResponse(
responseCode = "404",
description = "Unable find a client based on the provided parameter",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = String.class),
examples = {@ExampleObject(value = "Client not found")}
)
)
}
)
public Flux<ClientViewDto> findByClientNumberOrName(
@Parameter(description = "The one index page number, defaults to 0", example = "0")
@RequestParam(value = "page", required = false, defaultValue = "0")
Integer page,

@Parameter(description = "The amount of data to be returned per page, defaults to 10",
example = "10")
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size,

@Parameter(
description = "The client number to look for",
example = "00000002"
)
@PathVariable String clientNumberOrName
) {
return clientService.findByClientNumberOrName(page, size, clientNumberOrName);
}

@GetMapping("/findAllNonIndividuals")
@Operation(
summary = "Search all non-individual client. It will return active and inactive",
Expand Down Expand Up @@ -123,8 +169,8 @@ public Flux<ClientPublicViewDto> findAllNonIndividuals(
@GetMapping("/findByNames")
@Operation(
summary = """
Search a client by it's name (including first, middle and last) and client type.
It will return active and inactive""",
Search a client by it's name (including first, middle and last) and client type.
It will return active and inactive""",
responses = {
@ApiResponse(
responseCode = "200",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;
import lombok.experimental.SuperBuilder;

@Builder
@Data
@SuperBuilder
@With
@NoArgsConstructor
@AllArgsConstructor
@Schema(
description = "A snapshot of the client information",
title = "ClientDetails",
Expand All @@ -24,28 +30,28 @@
)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ClientPublicViewDto(
public class ClientPublicViewDto {
@Schema(description = "The client number", example = "00000002")
String clientNumber,
protected String clientNumber;

@Schema(description = """
The client last name if it's an individual or
the company name if it's a company""",
example = "BAXTER"
)
String clientName,
protected String clientName;

@Schema(description = """
The first name of the individual,
or null if it's a company""",
example = "JAMES")
String legalFirstName,
protected String legalFirstName;

@Schema(description = """
The middle name of the individual,
or null if it's a company""",
example = "Canter")
String legalMiddleName,
protected String legalMiddleName;

@Schema(description = """
The status of the client, can be any of the following:<br>
Expand All @@ -56,7 +62,7 @@ public record ClientPublicViewDto(
REC (Receivership)<br>
SPN (Suspended)""",
example = "ACT")
String clientStatusCode,
protected String clientStatusCode;

@Schema(description = """
The type of client, can be any of the following:<br>
Expand All @@ -74,9 +80,9 @@ public record ClientPublicViewDto(
T (First Nation Tribal Council)<br>
U (Unregistered Company)""",
example = "I")
String clientTypeCode,
protected String clientTypeCode;

@Schema(description = "An acronyms for this client",
example = "JAMES BAXTER")
String acronym
) {
protected String acronym;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ca.bc.gov.api.oracle.legacy.dto;

import lombok.Getter;

@Getter
public enum ClientStatusCodeEnum {
ACT("Active"),
DAC("Deactivated"),
DEC("Deceased"),
REC("Receivership"),
SPN("Suspended");

private final String description;

ClientStatusCodeEnum(String description) {
this.description = description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ca.bc.gov.api.oracle.legacy.dto;

import lombok.Getter;

@Getter
public enum ClientTypeCodeEnum {
A("Association"),
B("First Nation Band"),
C("Corporation"),
F("Ministry of Forests and Range"),
G("Government"),
I("Individual"),
L("Limited Partnership"),
P("General Partnership"),
R("First Nation Group"),
S("Society"),
T("First Nation Tribal Council"),
U("Unregistered Company");

private final String description;

ClientTypeCodeEnum(String description) {
this.description = description;
}
}
24 changes: 24 additions & 0 deletions src/main/java/ca/bc/gov/api/oracle/legacy/dto/ClientViewDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ca.bc.gov.api.oracle.legacy.dto;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.With;
import lombok.experimental.SuperBuilder;

@Data
@Getter(AccessLevel.PROTECTED)
@With
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ClientViewDto extends ClientPublicViewDto {
private String clientStatusCodeDescription;
private String clientTypeCodeDescription;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ca.bc.gov.api.oracle.legacy.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.With;
import lombok.experimental.SuperBuilder;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package ca.bc.gov.api.oracle.legacy.entity;


import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
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;

Expand All @@ -24,7 +20,6 @@
@Table(name = "FOREST_CLIENT", schema = "THE")
public class ForestClientEntity extends ForestClientBaseEntity {


@Column("CLIENT_ID_TYPE_CODE")
private String clientIdTypeCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface ForestClientRepository extends ReactiveCrudRepository<ForestCli

Flux<ForestClientEntity> findByClientAcronym(String acronym);

Flux<ForestClientEntity> findByClientNumberContainingOrClientNameContaining(
String clientNumber, String clientName, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import ca.bc.gov.api.oracle.legacy.ApplicationConstants;
import ca.bc.gov.api.oracle.legacy.dto.ClientPublicViewDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientStatusCodeEnum;
import ca.bc.gov.api.oracle.legacy.dto.ClientTypeCodeEnum;
import ca.bc.gov.api.oracle.legacy.dto.ClientViewDto;
import ca.bc.gov.api.oracle.legacy.entity.ForestClientEntity;
import ca.bc.gov.api.oracle.legacy.exception.ClientNotFoundException;
import ca.bc.gov.api.oracle.legacy.exception.InvalidClientNumberException;
Expand Down Expand Up @@ -47,6 +50,27 @@ public Mono<ClientPublicViewDto> findByClientNumber(String clientNumber) {
.map(ClientMapper::mapEntityToDto);
}

public Flux<ClientViewDto> findByClientNumberOrName(
int page, int size, String clientNumberOrName) {

log.info("Searching for client with number or name {}", clientNumberOrName);

return forestClientRepository
.findByClientNumberContainingOrClientNameContaining(
clientNumberOrName,
clientNumberOrName,
PageRequest.of(page, size))
.switchIfEmpty(Mono.error(new ClientNotFoundException()))
.map(ClientMapper::mapEntityToClientViewDto)
.map(clientViewDto -> {
clientViewDto.setClientTypeCodeDescription(
ClientTypeCodeEnum.valueOf(clientViewDto.getClientTypeCode()).getDescription());

return clientViewDto.withClientStatusCodeDescription(
ClientStatusCodeEnum.valueOf(clientViewDto.getClientStatusCode()).getDescription());
});
}

public Flux<ClientPublicViewDto> findAllNonIndividualClients(
int page, int size, String sortBy) {

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ca/bc/gov/api/oracle/legacy/util/ClientMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.bc.gov.api.oracle.legacy.util;

import ca.bc.gov.api.oracle.legacy.dto.ClientPublicViewDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientViewDto;
import ca.bc.gov.api.oracle.legacy.entity.ForestClientEntity;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand All @@ -19,4 +20,17 @@ public static ClientPublicViewDto mapEntityToDto(ForestClientEntity clientEntity
.acronym(clientEntity.getClientAcronym())
.build();
}

public static ClientViewDto mapEntityToClientViewDto(ForestClientEntity clientEntity) {
return ClientViewDto
.builder()
.clientNumber(clientEntity.getClientNumber())
.clientName(clientEntity.getClientName())
.legalFirstName(clientEntity.getLegalFirstName())
.legalMiddleName(clientEntity.getLegalMiddleName())
.clientStatusCode(clientEntity.getClientStatusCode())
.clientTypeCode(clientEntity.getClientTypeCode())
.acronym(clientEntity.getClientAcronym())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ca.bc.gov.api.oracle.legacy.AbstractTestContainerIntegrationTest;
import ca.bc.gov.api.oracle.legacy.dto.ClientPublicViewDto;
import ca.bc.gov.api.oracle.legacy.dto.ClientViewDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -62,6 +63,19 @@ void shouldFailFindByClientNumberNonNumericClientNumberTest() {
.expectStatus().isBadRequest();
}

@Test
@DisplayName("Search client by client number or name")
void shouldfindByClientNumberOrName() {
webTestClient
.get()
.uri("/api/clients/findByClientNumberOrName/{clientNumberOrName}", "01")
.exchange()
.expectStatus()
.isOk()
.expectBodyList(ClientViewDto.class)
.hasSize(5);
}

@Test
@DisplayName("Non Individual clients")
void shouldFindAllNonIndividualsTest() {
Expand Down

0 comments on commit c40a97d

Please sign in to comment.