Skip to content

Commit

Permalink
feat: efficient count() methods on Activity, Address, Estate, and User
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Nov 28, 2024
1 parent 9144101 commit 624ff98
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## main
- TODO: Remove old activity repository code in next major release

## v1.6.0
- feat: add efficient count() to ActivityRepository, AddressRepository, EstateRepository, and SettingsRepository::user()

## v1.5.10
- allow passing strings to relation trait so new relations do not have to be added every time they are used
- add missing relation type to trait
Expand Down Expand Up @@ -36,7 +39,7 @@
- fixes issue where requests with strings as resource types could not be processed when generating the hmac

## v1.5.0
- feat: enable withCredentials call_ on Builder so config is not needed to set runtime credentials
- feat: enable withCredentials call on Builder so config is not needed to set runtime credentials

## v1.4.4
- fix type annotation for upload function on file repository
Expand Down
4 changes: 4 additions & 0 deletions docs/repositories/activity-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ ActivityRepository::query()
->each(function (array $estates) {
// First page
});

$count = ActivityRepository::query()
->addressIds([1, 2])
->count();
```

## Create
Expand Down
7 changes: 7 additions & 0 deletions docs/repositories/estate-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ $estate = EstateRepository::query()
]);
```

### Count

```php
$count = EstateRepository::query()
->count();
```

### Search

::: tip
Expand Down
3 changes: 3 additions & 0 deletions docs/repositories/setting-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ SettingRepository::users()
->each(function (array $settings) {
// First page
});

$count = SettingRepository::users()
->count();
```

## Regions
Expand Down
24 changes: 24 additions & 0 deletions src/Query/ActivityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,30 @@ public function create(array $data): array
->json('response.results.0.data.records.0');
}

/**
* Returns the number of records that match the query. This number is from the API
* and might be lower than the actual number of records when queried with get().
*
* @throws OnOfficeException
*/
public function count(): int
{
$request = new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => [],
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::LISTLIMIT => 1,
...$this->customParameters,
]
);

return $this->requestApi($request)
->json('response.results.0.data.meta.cntabsolute', 0);
}

/**
* @deprecated Use estateId() instead
*/
Expand Down
11 changes: 5 additions & 6 deletions src/Query/AddressBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,21 @@ public function modify(int $id): bool
}

/**
* Returns the number of records that match the query. This number is from the API
* and might be lower than the actual number of records when queried with get().
*
* @throws Throwable<OnOfficeException>
*/
public function count(): int
{
$orderBy = $this->getOrderBy();

$request = new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Address,
parameters: [
OnOfficeService::RECORDIDS => $this->recordIds,
OnOfficeService::DATA => $this->columns,
OnOfficeService::DATA => [],
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::LISTLIMIT => $this->limit > 0 ? $this->limit : $this->pageSize,
OnOfficeService::SORTBY => data_get(array_keys($orderBy), 0),
OnOfficeService::SORTORDER => data_get($orderBy, 0),
OnOfficeService::LISTLIMIT => 1,
...$this->customParameters,
]
);
Expand Down
23 changes: 23 additions & 0 deletions src/Query/EstateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,27 @@ public function search(): Collection

return $this->requestAll($request);
}

/**
* Returns the number of records that match the query. This number is from the API
* and might be lower than the actual number of records when queried with get().
*
* @throws OnOfficeException
*/
public function count(): int
{
$request = new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::Estate,
parameters: [
OnOfficeService::DATA => [],
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::LISTLIMIT => 1,
...$this->customParameters,
],
);

return $this->requestApi($request)
->json('response.results.0.data.meta.cntabsolute', 0);
}
}
23 changes: 23 additions & 0 deletions src/Query/UserBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,27 @@ public function each(callable $callback): void

$this->requestAllChunked($request, $callback);
}

/**
* Returns the number of records that match the query. This number is from the API
* and might be lower than the actual number of records when queried with get().
*
* @throws OnOfficeException
*/
public function count(): int
{
$request = new OnOfficeRequest(
OnOfficeAction::Read,
OnOfficeResourceType::User,
parameters: [
OnOfficeService::DATA => [],
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::LISTLIMIT => 1,
...$this->customParameters,
],
);

return $this->requestApi($request)
->json('response.results.0.data.meta.cntabsolute', 0);
}
}
17 changes: 17 additions & 0 deletions tests/Repositories/ActivityRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@

ActivityRepository::assertSentCount(3);
});

test('count', function () {
Http::preventStrayRequests();
Http::fake([
'https://api.onoffice.de/api/stable/api.php/' => Http::sequence([
ReadActivityResponse::make(count: 1500),
]),
]);

ActivityRepository::record();

$response = ActivityRepository::query()->count();

expect($response)->toBe(1500);

ActivityRepository::assertSentCount(1);
});
});
17 changes: 17 additions & 0 deletions tests/Repositories/AddressRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,21 @@

AddressRepository::assertSentCount(3);
});

test('count', function () {
Http::preventStrayRequests();
Http::fake([
'https://api.onoffice.de/api/stable/api.php/' => Http::sequence([
ReadAddressResponse::make(count: 1500),
]),
]);

AddressRepository::record();

$response = AddressRepository::query()->count();

expect($response)->toBe(1500);

AddressRepository::assertSentCount(1);
});
});
17 changes: 17 additions & 0 deletions tests/Repositories/EstateRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@

EstateRepository::assertSentCount(3);
});

test('count', function () {
Http::preventStrayRequests();
Http::fake([
'https://api.onoffice.de/api/stable/api.php/' => Http::sequence([
ReadEstateResponse::make(count: 1500),
]),
]);

EstateRepository::record();

$response = EstateRepository::query()->count();

expect($response)->toBe(1500);

EstateRepository::assertSentCount(1);
});
});

describe('search', function () {
Expand Down
17 changes: 17 additions & 0 deletions tests/Repositories/UserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@

SettingRepository::assertSentCount(1);
});

test('count', function () {
Http::preventStrayRequests();
Http::fake([
'https://api.onoffice.de/api/stable/api.php/' => Http::sequence([
ReadUserResponse::make(count: 1500),
]),
]);

SettingRepository::record();

$response = SettingRepository::users()->count();

expect($response)->toBe(1500);

SettingRepository::assertSentCount(1);
});
});

0 comments on commit 624ff98

Please sign in to comment.