Skip to content

Commit

Permalink
Merge pull request #107 from naitsirch/force_create
Browse files Browse the repository at this point in the history
Added a flag for forced person creation
  • Loading branch information
DumbergerL authored Sep 15, 2022
2 parents e144fbd + f861dd5 commit 736effd
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [Serialize Models to Data-Array for JSON-Export](https://github.com/5pm-HDH/churchtools-api/pull/103)
- [Retrieve Birthdays](https://github.com/5pm-HDH/churchtools-api/pull/104)
- [Retrieve Tags of Person](https://github.com/5pm-HDH/churchtools-api/pull/110)
- [Added force flag to person creation, to add persons with same name](https://github.com/5pm-HDH/churchtools-api/pull/107)
- [Absence-API](https://github.com/5pm-HDH/churchtools-api/pull/111)
- [File-API](https://github.com/5pm-HDH/churchtools-api/pull/114)

Expand Down
22 changes: 22 additions & 0 deletions docs/out/PersonAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@

```

Sometimes it will happen that you have to add a person with the same name
as an existing one. ChurchTools will respond with an error to prevent you from
adding duplicates accidently.

Therefore you can add the `force` parameter and set it to `true`.

```php
use CTApi\Models\Person;
use CTApi\Requests\PersonRequest;

$newPerson = new Person();
$newPerson->setFirstName("John")
->setLastName("Doe")
->setBirthday("1970-01-01");
//add further attributes

PersonRequest::create($newPerson, force: true);

```

This will make ChurchTools to insert the record, even if there is a second John Doe.

## Update a person's data

Use the setters of the person model to modify its data and utilize the
Expand Down
10 changes: 10 additions & 0 deletions docs/src/ressources/PersonAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

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

Sometimes it will happen that you have to add a person with the same name
as an existing one. ChurchTools will respond with an error to prevent you from
adding duplicates accidently.

Therefore you can add the `force` parameter and set it to `true`.

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

This will make ChurchTools to insert the record, even if there is a second John Doe.

## Update a person's data

Use the setters of the person model to modify its data and utilize the
Expand Down
25 changes: 21 additions & 4 deletions src/Requests/AbstractRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ public function get(): array
* Send Create-Request for given Model.
*
* @param UpdatableModel $model Model
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
*/
protected function createDataForModel(UpdatableModel $model): void
protected function createDataForModel(UpdatableModel $model, bool $force = false): void
{
$createAttributes = $model->getModifiableAttributes();

Expand All @@ -80,7 +84,7 @@ protected function createDataForModel(UpdatableModel $model): void
return !is_null($value);
});

$responseData = $this->createData($data);
$responseData = $this->createData($data, $force);

// Some data like the ID is created on Churchtools and we have to sync it
// back to the model.
Expand All @@ -100,13 +104,26 @@ protected function createDataForModel(UpdatableModel $model): void
* Sends the data to the API endpoint to create a new record at ChurchTools.
* When the action was successfull, the actual data from ChurchTools is returned
* as an array.
*
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
*/
protected function createData(array $data): array
protected function createData(array $data, bool $force = false): array
{
$url = $this->getApiEndpoint();
$query = [];

if ($force) {
$query['force'] = '1';
}

$client = CTClient::getClient();
$response = $client->post($url, ['json' => $data]);
$response = $client->post($url, [
'query' => $query,
'json' => $data,
]);

$response->getBody();
return CTResponseUtil::dataAsArray($response);
Expand Down
9 changes: 7 additions & 2 deletions src/Requests/PersonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public static function find(int $id): ?Person

/**
* Add the person to ChurchTools.
*
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
*/
public static function create(Person $person): void
public static function create(Person $person, bool $force = false): void
{
(new PersonRequestBuilder())->create($person);
(new PersonRequestBuilder())->create($person, $force);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/Requests/PersonRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,23 @@ public function get(): array
return Person::createModelsFromArray($data);
}


public function create(Person $person): void
/**
* Add the person to ChurchTools.
*
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
* @throws CTModelException If the passed person already has an ID.
*/
public function create(Person $person, bool $force = false): void
{
$id = $person->getId();
if (!is_null($id)) {
throw new CTModelException("ID of a new Person has to be null.");
}

$this->createDataForModel($person);
$this->createDataForModel($person, $force);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/Docs/PersonRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public function testCreatePerson()
PersonRequest::create($newPerson);
}

/**
* @doesNotPerformAssertions
*/
public function testCreatePersonWithEqualName()
{
$newPerson = new Person();
$newPerson->setFirstName("John")
->setLastName("Doe")
->setBirthday("1970-01-01");
//add further attributes

PersonRequest::create($newPerson, force: true);
}

/**
* @doesNotPerformAssertions
*/
Expand Down

0 comments on commit 736effd

Please sign in to comment.