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

DDFHER-101- Allow retrieval of opening hours for multiple branches #1659

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
8 changes: 6 additions & 2 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -709,8 +709,12 @@
},
{
"name": "nid",
"type": "integer",
"description": "The (node) id of the node (Branch) that the opening hour applies to.",
"type": "array",
"items": {
"type": "integer"
},
"collectionFormat": "csv",
"description": "The id(s) of the branch(es) for which to retrieve opening hours. Can be a single id or a comma-separated list of ids.",
"in": "query",
"required": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,25 @@ public function load(int $id): ?OpeningHoursInstance {
/**
* Load a collection of opening hours.
*
* @param int[] $branchIds
* The branch ID or an array of branch IDs.
* @param \DateTimeInterface|null $fromDate
* The start date.
* @param \DateTimeInterface|null $toDate
* The end date.
* @param int|null $repetitionId
* The repetition ID.
* @param int|null $categoryId
* The category ID.
*
* @return OpeningHoursInstance[]
* Opening hours instances which match the provided criteria.
*/
public function loadMultiple(int $branchId = NULL, \DateTimeInterface $fromDate = NULL, \DateTimeInterface $toDate = NULL, int $repetitionId = NULL, int $categoryId = NULL): array {
public function loadMultiple(array $branchIds = [], \DateTimeInterface $fromDate = NULL, \DateTimeInterface $toDate = NULL, int $repetitionId = NULL, int $categoryId = NULL): array {
$query = $this->connection->select(self::INSTANCE_TABLE, self::INSTANCE_TABLE)
->fields(self::INSTANCE_TABLE);
if ($branchId) {
$query->condition('branch_nid', $branchId);
if ($branchIds) {
$query->condition('branch_nid', $branchIds, 'IN');
}
if ($fromDate) {
$query->condition('date', $fromDate->format('Y-m-d'), '>=');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function get(Request $request): Response {

try {
$openingHoursInstances = $this->repository->loadMultiple(
$typedRequest->getInt('branch_id'),
$typedRequest->getInt('branch_id') ? [$typedRequest->getInt('branch_id')] : [],
$typedRequest->getDateTime('from_date'),
$typedRequest->getDateTime('to_date')
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,12 @@ public function getPluginDefinition(): array {
],
'nid' => [
'name' => 'nid',
'type' => 'integer',
'description' => 'The (node) id of the node (Branch) that the opening hour applies to.',
'type' => 'array',
'items' => [
'type' => 'integer',
],
'collectionFormat' => 'csv',
'description' => 'The id(s) of the branch(es) for which to retrieve opening hours. Can be a single id or a comma-separated list of ids.',
'in' => 'query',
'required' => TRUE,
],
Expand Down Expand Up @@ -118,8 +122,8 @@ public function getPluginDefinition(): array {
public function get(Request $request): Response {
$typedRequest = new RequestTyped($request);

$nid = $typedRequest->getInt('nid');
if ($nid === NULL) {
$nid = $typedRequest->getInts('nid');
if (empty($nid)) {
throw new BadRequestHttpException("The 'nid' parameter is required.");
}
$fromDate = $typedRequest->getDateTime('from_date');
Expand Down
26 changes: 26 additions & 0 deletions web/modules/custom/drupal_typed/src/RequestTyped.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ public function getDateTime(string $key, ?\DateTimeInterface $default = NULL) :
}
}

/**
* Retrieve a list of values as integers.
*
* This method retrieves a string value from the request, splits it using the
* provided separator, and returns an array of integers. If the value is null,
* the default array will be returned.
*
* @param string $key
* The key to retrieve from the request.
* @param int[] $default
* The default array of integers to return if the key does not exist.
* @param string $separator
* The separator used to split the string value. Defaults to a comma (",").
*
* @return int[]
* An array of integers.
*/
public function getInts(string $key, array $default = [], string $separator = ","): array {
$value = $this->request->get($key);
if ($value === NULL) {
return $default;
}
$strings = \Safe\preg_split("/\s*{$separator}\s*/", $value, PREG_SPLIT_NO_EMPTY);
return array_map(fn($value) => intval($value), $strings);
}

}
Loading