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

Added method to delete a person on churchtools #91

Merged
merged 6 commits into from
Aug 23, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added
- [Added method to update person data](https://github.com/5pm-HDH/churchtools-api/pull/84)
- [Added modifiable-attributes to person-api docs](https://github.com/5pm-HDH/churchtools-api/pull/88)
- [Added method to delete person records](https://github.com/5pm-HDH/churchtools-api/pull/91)

### Changed
- [Refactor CTClient:](https://github.com/5pm-HDH/churchtools-api/pull/83) transform inheritance from GuzzleClient to composition-relation
Expand Down
17 changes: 17 additions & 0 deletions docs/out/PersonAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Follow this example:
```php
use CTApi\Requests\PersonRequest;

$person = PersonRequest::findOrFail(21);
$person->setEmail('new-mail@example.com');

PersonRequest::update($person);
Expand All @@ -60,6 +61,7 @@ data sent to the API, by adding a whitelist of attributes.
```php
use CTApi\Requests\PersonRequest;

$person = PersonRequest::findOrFail(21);
$person->setEmail('new-mail@example.com');
$person->setJob('This should not be persisted!');

Expand All @@ -75,11 +77,26 @@ The following attributes can be updated:
```php
use CTApi\Requests\PersonRequest;

$person = PersonRequest::findOrFail(21);

// Attributes that can be updated in ChurchTools-API
$listOfModifiableAttributes = implode("; ", $person->getModifiableAttributes());
var_dump( $listOfModifiableAttributes);
// Output: "addressAddition; birthday; birthName; birthplace; city; country; email; fax; firstName; job; lastName; mobile; nickname; phonePrivate; phoneWork; sexId; street; zip"


```

## Delete person

Delete person via PersonRequest:

```php
use CTApi\Requests\PersonRequest;

$person = PersonRequest::findOrFail(21);

// delete person on churchtools
PersonRequest::delete($person);

```
8 changes: 7 additions & 1 deletion docs/src/ressources/PersonAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ unnecessary traffic if you are going to do some bulk updates.

The following attributes can be updated:

{{ \Tests\Unit\Docs\PersonRequestTest.testUpdatePersonModifiableAttributes }}
{{ \Tests\Unit\Docs\PersonRequestTest.testUpdatePersonModifiableAttributes }}

## Delete person

Delete person via PersonRequest:

{{ \Tests\Unit\Docs\PersonRequestTest.testDeletePerson }}
10 changes: 10 additions & 0 deletions src/CTClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ public function patch($uri, array $options = []): ResponseInterface
}
}

public function delete($uri, array $options = []): ResponseInterface
{
try {
CTLog::getLog()->debug('CTClient: DELETE-Request URI:' . $uri, ["options" => $options, "mergedOptions" => self::mergeOptions($options)]);
return $this->handleResponse($this->guzzleClient->delete($uri, self::mergeOptions($options)));
} catch (Exception $exception) {
return $this->handleException($exception);
}
}

private function handleResponse(ResponseInterface $response): ResponseInterface
{
switch ($response->getStatusCode()) {
Expand Down
15 changes: 15 additions & 0 deletions src/Requests/AbstractRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ protected function updateDataForModel($model, string $modelId, array $updateAttr
$this->updateData($modelId, $updateAttributes);
}

/**
* Delete the object in ChurchTools by the given ID.
*/
public function delete(string $modelId): void
{
$url = $this->getApiEndpoint() . '/' . $modelId;

$client = CTClient::getClient();
$response = $client->delete($url);

if (!in_array($response->getStatusCode(), [200, 204])) {
throw CTRequestException::ofErrorResponse($response);
}
}

abstract protected function getApiEndpoint(): string;

abstract protected function getModelClass(): string;
Expand Down
15 changes: 15 additions & 0 deletions src/Requests/PersonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace CTApi\Requests;

use CTApi\Exceptions\CTModelException;
use CTApi\Models\Person;

class PersonRequest
Expand Down Expand Up @@ -51,4 +52,18 @@ public static function update(Person $person, array $attributesToUpdate = []): v
{
(new PersonRequestBuilder())->update($person, $attributesToUpdate);
}

/**
* Delete the person on churchtools.
*/
public static function delete(Person $person): void
{
$id = $person->getId();

if (is_null($id)) {
throw new CTModelException("ID of Person cannot be null.");
}

(new PersonRequestBuilder())->delete($id);
}
}
20 changes: 17 additions & 3 deletions tests/unit/Docs/PersonRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ public function testExampleCode()
/**
* @doesNotPerformAssertions
*/
public function testUpdatePerson(){
public function testUpdatePerson()
{
$person = PersonRequest::findOrFail(21);
$person->setEmail('new-mail@example.com');

Expand All @@ -53,19 +54,32 @@ public function testUpdatePerson(){
/**
* @doesNotPerformAssertions
*/
public function testUpdatePersonSingleAttrbute(){
public function testUpdatePersonSingleAttrbute()
{
$person = PersonRequest::findOrFail(21);
$person->setEmail('new-mail@example.com');
$person->setJob('This should not be persisted!');

PersonRequest::update($person, ['email']);
}

public function testUpdatePersonModifiableAttributes(){
public function testUpdatePersonModifiableAttributes()
{
$person = PersonRequest::findOrFail(21);

// Attributes that can be updated in ChurchTools-API
$listOfModifiableAttributes = implode("; ", $person->getModifiableAttributes());
$this->assertEquals("addressAddition; birthday; birthName; birthplace; city; country; email; fax; firstName; job; lastName; mobile; nickname; phonePrivate; phoneWork; sexId; street; zip", $listOfModifiableAttributes);
}

/**
* @doesNotPerformAssertions
*/
public function testDeletePerson()
{
$person = PersonRequest::findOrFail(21);

// delete person on churchtools
PersonRequest::delete($person);
}
}