Skip to content

Commit

Permalink
chore: small code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj committed Oct 2, 2024
1 parent ce75def commit 750835e
Show file tree
Hide file tree
Showing 10 changed files with 653 additions and 524 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import ca.bc.gov.app.dto.client.CodeNameDto;
import ca.bc.gov.app.dto.client.DistrictDto;
import ca.bc.gov.app.dto.client.IdentificationTypeDto;
import ca.bc.gov.app.service.client.ClientCodeService;
import ca.bc.gov.app.service.client.ClientCountryProvinceService;
import ca.bc.gov.app.service.client.ClientDistrictService;
import ca.bc.gov.app.service.client.ClientService;
import io.micrometer.observation.annotation.Observed;
import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
Expand All @@ -25,19 +29,22 @@
public class ClientCodesController {

private final ClientService clientService;
private final ClientDistrictService clientDistrictService;
private final ClientCountryProvinceService clientCountryProvinceService;
private final ClientCodeService clientCodeService;

@GetMapping("/client-types")
public Flux<CodeNameDto> findActiveClientTypeCodes() {
log.info("Requesting a list of active client type codes from the client service.");
return clientService
return clientCodeService
.findActiveClientTypeCodes(LocalDate.now());
}

@GetMapping("/client-types/{code}")
public Mono<CodeNameDto> getClientTypeByCode(
@PathVariable String code) {
log.info("Requesting a client type by code {} from the client service.", code);
return clientService.getClientTypeByCode(code);
return clientCodeService.getClientTypeByCode(code);
}

@GetMapping("/contact-types")
Expand All @@ -48,7 +55,7 @@ public Flux<CodeNameDto> listClientContactTypeCodes(
Integer size
) {
log.info("Requesting a list of active client contact type codes from the client service.");
return clientService
return clientCodeService
.listClientContactTypeCodes(LocalDate.now(), page, size);
}

Expand All @@ -59,13 +66,13 @@ public Flux<CodeNameDto> getActiveDistrictCodes(
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size) {
log.info("Requesting a list of districts from the client service.");
return clientService.getActiveDistrictCodes(page, size);
return clientDistrictService.getActiveDistrictCodes(page, size, LocalDate.now());
}

@GetMapping("/districts/{districtCode}")
public Mono<DistrictDto> getDistrictByCode(@PathVariable String districtCode) {
log.info("Requesting a district by code {} from the client service.", districtCode);
return clientService.getDistrictByCode(districtCode);
return clientDistrictService.getDistrictByCode(districtCode);
}

@GetMapping("/countries")
Expand All @@ -75,14 +82,14 @@ public Flux<CodeNameDto> countries(
@RequestParam(value = "size", required = false, defaultValue = "10")
Integer size) {
log.info("Requesting a list of countries from the client service.");
return clientService.listCountries(page, size);
return clientCountryProvinceService.listCountries(page, size, LocalDate.now());
}

@GetMapping("/countries/{countryCode}")
public Mono<CodeNameDto> getCountryByCode(
@PathVariable String countryCode) {
log.info("Requesting a country by code {} from the client service.", countryCode);
return clientService.getCountryByCode(countryCode);
return clientCountryProvinceService.getCountryByCode(countryCode);
}

@GetMapping("/countries/{countryCode}/provinces")
Expand All @@ -94,7 +101,7 @@ public Flux<CodeNameDto> listProvinces(
Integer size) {
log.info("Requesting a list of provinces by country code {} from the client service.",
countryCode);
return clientService
return clientCountryProvinceService
.listProvinces(countryCode, page, size);
}

Expand All @@ -105,20 +112,21 @@ public Mono<CodeNameDto> getProvinceByCountryAndProvinceCode(
log.info("Requesting a province by country and province code {} {} from the client service.",
countryCode,
provinceCode);
return clientService.getProvinceByCountryAndProvinceCode(countryCode, provinceCode);
return clientCountryProvinceService.getProvinceByCountryAndProvinceCode(countryCode, provinceCode);
}

@GetMapping("/identification-types")
public Flux<IdentificationTypeDto> identificationTypes() {
log.info("Requesting a list of identification type codes.");
return clientService.getAllActiveIdentificationTypes(LocalDate.now());
return clientCodeService.getAllActiveIdentificationTypes(LocalDate.now());
}

//TODO: This endpoint needs to be updated to properly reflect what code is returning
@GetMapping("{idCode}")
public Mono<CodeNameDto> getIdentificationTypeByCode(
@PathVariable String idCode) {
log.info("Requesting an identification type by code {}.", idCode);
return clientService.getIdentificationTypeByCode(idCode);
return clientCodeService.getIdentificationTypeByCode(idCode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package ca.bc.gov.app.service.client;

import static org.springframework.data.relational.core.query.Query.query;

import ca.bc.gov.app.dto.client.CodeNameDto;
import ca.bc.gov.app.dto.client.IdentificationTypeDto;
import ca.bc.gov.app.entity.client.ClientTypeCodeEntity;
import ca.bc.gov.app.predicates.QueryPredicates;
import ca.bc.gov.app.repository.client.ClientTypeCodeRepository;
import ca.bc.gov.app.repository.client.ContactTypeCodeRepository;
import ca.bc.gov.app.repository.client.IdentificationTypeCodeRepository;
import io.micrometer.observation.annotation.Observed;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
@RequiredArgsConstructor
@Slf4j
@Observed
public class ClientCodeService {

private final ClientTypeCodeRepository clientTypeCodeRepository;
private final ContactTypeCodeRepository contactTypeCodeRepository;
private final IdentificationTypeCodeRepository identificationTypeCodeRepository;
private final R2dbcEntityTemplate template;

/**
* <p><b>Find Active Client Type Codes</b></p>
* <p>List client type code based on it's effective and expiration date.
* The rule used for it is the expiration date must not be set or should be bigger than provided
* date and the effective date bust be before or equal to the provided date.</p>
* <p>The order is by description.</p>
*
* @param targetDate The date to be used as reference.
* @return A list of {@link CodeNameDto}
*/
public Flux<CodeNameDto> findActiveClientTypeCodes(LocalDate targetDate) {
log.info("Loading active client type codes for {}", targetDate);
return
clientTypeCodeRepository
.findActiveAt(targetDate)
.map(entity -> new CodeNameDto(
entity.getCode(),
entity.getDescription()
)
);
}


/**
* Retrieves a client type by its unique code. This method queries the clientTypeCodeRepository to
* find a client type entity with the specified code. If a matching entity is found, it is
* converted to a {@code CodeNameDto} object containing the code and description, and wrapped in a
* Mono. If no matching entity is found, the Mono will complete without emitting any items.
*
* @param code The unique code of the client type to retrieve.
* @return A Mono emitting a {@code CodeNameDto} if a matching client type is found, or an empty
* result if no match is found.
* @see CodeNameDto
*/
public Mono<CodeNameDto> getClientTypeByCode(String code) {
log.info("Loading client type for {}", code);
return clientTypeCodeRepository
.findByCode(code)
.map(entity -> new CodeNameDto(entity.getCode(),
entity.getDescription()));
}

/**
* Retrieves a map of client types that are currently active.
* This method queries the database for client type entities that are effective as of now and not expired.
* The results are collected into a list and then converted into a map where the keys are the client type codes
* and the values are the descriptions.
*
* @return A Mono emitting a map with client type codes as keys and their descriptions as values.
*/
public Mono<Map<String, String>> getClientTypes() {
return template
.select(
query(
QueryPredicates.isBefore(LocalDateTime.now(), "effectiveAt")
.and(
QueryPredicates.isAfter(LocalDateTime.now(), "expiredAt")
.or(QueryPredicates.isNull("expiredAt"))
)
),
ClientTypeCodeEntity.class
)
.collectList()
// Convert the list into a map using code as the key and description as value
.map(clientTypeCodeEntities ->
clientTypeCodeEntities
.stream()
.collect(Collectors.toMap(ClientTypeCodeEntity::getCode,
ClientTypeCodeEntity::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 CodeNameDto} entries.
*/
public Flux<CodeNameDto> listClientContactTypeCodes(LocalDate activeDate, int page, int size) {
log.info("Loading contact types for page {} with size {}", page, size);
return contactTypeCodeRepository
.findActiveAt(activeDate, PageRequest.of(page, size))
.map(entity -> new CodeNameDto(
entity.getContactTypeCode(),
entity.getDescription()));
}

/**
* Retrieves all active identification types as of the specified target date.
*
* @param targetDate the date to check for active identification types.
* @return a Flux stream of IdentificationTypeDto containing the code, description, and country
* code of each active identification type.
*/
public Flux<IdentificationTypeDto> getAllActiveIdentificationTypes(LocalDate targetDate) {
log.info("Loading active identification type codes by {}", targetDate);
return identificationTypeCodeRepository
.findActiveAt(targetDate)
.map(entity -> new IdentificationTypeDto(
entity.getCode(),
entity.getDescription(),
entity.getCountryCode()));
}

/**
* Retrieves an identification type by its code.
*
* @param idCode the code of the identification type to retrieve.
* @return a Mono containing a CodeNameDto with the code and description of the identification
* type, or an empty Mono if not found.
*/
public Mono<CodeNameDto> getIdentificationTypeByCode(String idCode) {
log.info("Loading identification type by {}", idCode);
return identificationTypeCodeRepository
.findByCode(idCode)
.map(entity -> new CodeNameDto(entity.getCode(),
entity.getDescription()));
}


}
Loading

0 comments on commit 750835e

Please sign in to comment.