Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: opening search APIs for OrgUnits and Categories #364

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
import ca.bc.gov.restapi.results.common.pagination.PaginationParameters;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchFiltersDto;
import ca.bc.gov.restapi.results.oracle.dto.OpeningSearchResponseDto;
import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity;
import ca.bc.gov.restapi.results.oracle.service.OpenCategoryCodeService;
import ca.bc.gov.restapi.results.oracle.service.OpeningService;
import ca.bc.gov.restapi.results.oracle.service.OrgUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand All @@ -14,6 +18,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -31,6 +36,10 @@ public class OpeningSearchEndpoint {

private final OpeningService openingService;

private final OpenCategoryCodeService openCategoryCodeService;

private final OrgUnitService orgUnitService;

/**
* Search for Openings with different filters.
*
Expand Down Expand Up @@ -188,4 +197,99 @@ public PaginatedResult<OpeningSearchResponseDto> openingSearch(
mainSearchTerm);
return openingService.openingSearch(filtersDto, paginationParameters);
}

/**
* Get all opening categories. Optionally you can ask for the expired ones.
*
* @param includeExpired Query param to include expired categories.
* @return List of OpenCategoryCodeEntity with found categories.
*/
@GetMapping("/categories")
@Operation(
summary = "Get all opening categories",
description = "Get all opening categories. Optionally you can ask for the expired ones.",
responses = {
@ApiResponse(
responseCode = "200",
description = "An array with found objects, or an empty array.",
content = @Content(mediaType = "application/json")),
@ApiResponse(
responseCode = "401",
description = "Access token is missing or invalid",
content = @Content(schema = @Schema(implementation = Void.class)))
})
public List<OpenCategoryCodeEntity> getOpeningCategories(
@RequestParam(value = "includeExpired", required = false)
@Parameter(
name = "includeExpired",
in = ParameterIn.QUERY,
description = "Defines if the API should include expired categories",
required = false)
Boolean includeExpired) {
boolean addExpired = Boolean.TRUE.equals(includeExpired);
return openCategoryCodeService.findAllCategories(addExpired);
}

/**
* Get all org units.
*
* @param includeExpired Query param to include expired org units.
* @return List of OrgUnitEntity with found org units.
*/
@GetMapping("/org-units")
@Operation(
summary = "Get all opening org units",
description = "Get all opening org units. Optionally you can ask for the expired ones.",
responses = {
@ApiResponse(
responseCode = "200",
description = "An array with found objects, or an empty array.",
content = @Content(mediaType = "application/json")),
@ApiResponse(
responseCode = "401",
description = "Access token is missing or invalid",
content = @Content(schema = @Schema(implementation = Void.class)))
})
public List<OrgUnitEntity> getOpeningOrgUnits(
@RequestParam(value = "includeExpired", required = false)
@Parameter(
name = "includeExpired",
in = ParameterIn.QUERY,
description = "Defines if the API should include expired org units",
required = false)
Boolean includeExpired) {
boolean addExpired = Boolean.TRUE.equals(includeExpired);
return orgUnitService.findAllOrgUnits(addExpired);
}

/**
* Get all org units by code.
*
* @param codes Org Unit codes to search for.
* @return List of OrgUnitEntity with found org units.
*/
@GetMapping("/org-units-by-code")
@Operation(
summary = "Get all opening org units by code",
description = "Get all opening org units by code.",
responses = {
@ApiResponse(
responseCode = "200",
description = "An array with found objects, or an empty array.",
content = @Content(mediaType = "application/json")),
@ApiResponse(
responseCode = "401",
description = "Access token is missing or invalid",
content = @Content(schema = @Schema(implementation = Void.class)))
})
public List<OrgUnitEntity> getOpeningOrgUnitsByCode(
@RequestParam(value = "orgUnitCodes", required = true)
@Parameter(
name = "orgUnitCodes",
in = ParameterIn.QUERY,
description = "Defines the org units that should be included in the search",
required = false)
List<String> codes) {
return orgUnitService.findAllOrgUnitsByCode(codes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ca.bc.gov.restapi.results.oracle.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.LocalDate;
import lombok.Getter;
import lombok.Setter;

/** This class represents an Opening Category in the database. */
@Getter
@Setter
@Entity
@Table(name = "OPEN_CATEGORY_CODE")
public class OpenCategoryCodeEntity {

@Id
@Column(name = "OPEN_CATEGORY_CODE")
private String code;

@Column(name = "DESCRIPTION", length = 120, nullable = false)
private String description;

@Column(name = "EFFECTIVE_DATE", nullable = false)
private LocalDate effectiveDate;

@Column(name = "EXPIRY_DATE", nullable = false)
private LocalDate expiryDate;

@Column(name = "UPDATE_TIMESTAMP", nullable = false)
private LocalDate updateTimestamp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.time.LocalDate;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -28,8 +29,39 @@ public class OrgUnitEntity {
@Column(name = "ORG_UNIT_NAME", length = 100, nullable = false)
private String orgUnitName;

// An Org Unit belongs to one Opening
// Foreign key referencing the opening table
// @OneToOne(mappedBy = "adminDistrict")
// private OpeningEntity openingEntity;
@Column(name = "LOCATION_CODE", length = 3, nullable = false)
private String locationCode;

@Column(name = "AREA_CODE", length = 3, nullable = false)
private String areaCode;

@Column(name = "TELEPHONE_NO", length = 7, nullable = false)
private String telephoneNo;

@Column(name = "ORG_LEVEL_CODE", length = 1, nullable = false)
private Character orgLevelCode;

@Column(name = "OFFICE_NAME_CODE", length = 2, nullable = false)
private String officeNameCode;

@Column(name = "ROLLUP_REGION_NO", nullable = false)
private Long rollupRegionNo;

@Column(name = "ROLLUP_REGION_CODE", length = 6, nullable = false)
private String rollupRegionCode;

@Column(name = "ROLLUP_DIST_NO", nullable = false)
private Long rollupDistNo;

@Column(name = "ROLLUP_DIST_CODE", length = 6, nullable = false)
private String rollupDistCode;

@Column(name = "EFFECTIVE_DATE", nullable = false)
private LocalDate effectiveDate;

@Column(name = "EXPIRY_DATE", nullable = false)
private LocalDate expiryDate;

@Column(name = "UPDATE_TIMESTAMP", nullable = false)
private LocalDate updateTimestamp;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ca.bc.gov.restapi.results.oracle.repository;

import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import java.time.LocalDate;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

/** This interface provides methods to get, save, and manage data in the database. */
public interface OpenCategoryCodeRepository extends JpaRepository<OpenCategoryCodeEntity, String> {

List<OpenCategoryCodeEntity> findAllByExpiryDateAfter(LocalDate now);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ca.bc.gov.restapi.results.oracle.repository;

import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity;
import java.time.LocalDate;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

/** This interface provides methods to get, save, and manage org unit data in the database. */
public interface OrgUnitRepository extends JpaRepository<OrgUnitEntity, Long> {

List<OrgUnitEntity> findAllByExpiryDateAfter(LocalDate now);

List<OrgUnitEntity> findAllByOrgUnitCodeIn(List<String> orgUnitCodes);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ca.bc.gov.restapi.results.oracle.service;

import ca.bc.gov.restapi.results.oracle.entity.OpenCategoryCodeEntity;
import ca.bc.gov.restapi.results.oracle.repository.OpenCategoryCodeRepository;
import java.time.LocalDate;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/** This class contains methods to handle Opening Categories. */
@Slf4j
@Service
@RequiredArgsConstructor
public class OpenCategoryCodeService {

private final OpenCategoryCodeRepository openCategoryCodeRepository;

/**
* Find all Opening categories. Option to include expired ones.
*
* @param includeExpired True to include expired, false otherwise.
* @return List of {@link OpenCategoryCodeEntity} with found categories.
*/
public List<OpenCategoryCodeEntity> findAllCategories(boolean includeExpired) {
log.info("Getting all open category codes. Include expired: {}", includeExpired);

if (includeExpired) {
List<OpenCategoryCodeEntity> openCategoryCodes = openCategoryCodeRepository.findAll();
log.info("Found {} open category codes (including expired)", openCategoryCodes.size());
return openCategoryCodes;
}

List<OpenCategoryCodeEntity> openCategoryCodes =
openCategoryCodeRepository.findAllByExpiryDateAfter(LocalDate.now());
log.info("Found {} open category codes (excluding expired)", openCategoryCodes.size());
return openCategoryCodes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package ca.bc.gov.restapi.results.oracle.service;

import ca.bc.gov.restapi.results.oracle.entity.OrgUnitEntity;
import ca.bc.gov.restapi.results.oracle.repository.OrgUnitRepository;
import java.time.LocalDate;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/** This class contains methods to handle Org Units. */
@Slf4j
@Service
@AllArgsConstructor
public class OrgUnitService {

private final OrgUnitRepository orgUnitRepository;

/**
* Find all Org Units. Option to include expired ones.
*
* @param includeExpired True to include expired, false otherwise.
* @return List of {@link OrgUnitEntity} with found categories.
*/
public List<OrgUnitEntity> findAllOrgUnits(boolean includeExpired) {
log.info("Getting all org units. Include expired: {}", includeExpired);

if (includeExpired) {
List<OrgUnitEntity> orgUnits = orgUnitRepository.findAll();
log.info("Found {} org units (including expired)", orgUnits.size());
return orgUnits;
}

List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByExpiryDateAfter(LocalDate.now());
log.info("Found {} org units (excluding expired)", orgUnits.size());
return orgUnits;
}

/**
* Find all Org Units by code.
*
* @param orgUnitCodes Org Unit codes to search for.
* @return List of {@link OrgUnitEntity} with found categories.
*/
public List<OrgUnitEntity> findAllOrgUnitsByCode(List<String> orgUnitCodes) {
log.info("Getting all org units by codes: {}", orgUnitCodes);

List<OrgUnitEntity> orgUnits = orgUnitRepository.findAllByOrgUnitCodeIn(orgUnitCodes);
log.info("Found {} org units by codes", orgUnits.size());
return orgUnits;
}
}
Loading