diff --git a/CHANGELOG.md b/CHANGELOG.md index 06f394ee..abaeb96f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added +- Retrieve, create and delete SongArrangement comments ([PR187](https://github.com/5pm-HDH/churchtools-api/pull/187)) ### Changed @@ -20,7 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added **New Requests:** -- Get config request `ConfigRequest:getConfig()` ([175](https://github.com/5pm-HDH/churchtools-api/pull/175)) +- Get config request `ConfigRequest:getConfig()` ([PR175](https://github.com/5pm-HDH/churchtools-api/pull/175)) - Get SongStatistic `SongStatisticRequest::all()` ([PR140](https://github.com/5pm-HDH/churchtools-api/pull/140), [PR164](https://github.com/5pm-HDH/churchtools-api/pull/164)) - Song-Tags (`$song->requestTags()`) and Group-Tags (`$group->requestTags()`) ([PR168](https://github.com/5pm-HDH/churchtools-api/pull/168)) - Get CombinedAppointment consisting of appointment, booking and event ([PR174](https://github.com/5pm-HDH/churchtools-api/pull/174)) diff --git a/docs/out/SongAPI.md b/docs/out/SongAPI.md index 266d147f..572f324d 100644 --- a/docs/out/SongAPI.md +++ b/docs/out/SongAPI.md @@ -201,6 +201,65 @@ ``` +## Retrieve, create and delete comments: + +**Retrieve comments:** + +```php + use CTApi\Models\Events\Song\SongCommentRequest; + + $comments = SongCommentRequest::getForSongArrangement(2); + $comment = $comments[0]; + + var_dump( $comment->getId()); + // Output: 2 + + var_dump( $comment->getDomainId()); + // Output: 3 + + var_dump( $comment->getDomainType()); + // Output: "arrangement" + + var_dump( $comment->getText()); + // Output: "Ich finde den Song super!" + + var_dump( $comment->getMeta()?->getModifiedDate()); + // Output: "2023-12-11 13:06:35" + + var_dump( $comment->getMeta()?->getModifiedPerson()?->getId()); + // Output: 12 + + + $person = $comment->getMeta()?->requestModifiedPerson(); + + var_dump( $person->getFirstName()); + // Output: "David" + + +``` + +**Create comment:** + +```php + use CTApi\Models\Events\Song\SongCommentRequest; + + SongCommentRequest::create(2, "Die Tonart ist super für Sopran."); + +``` + +**Delete comments:** + +```php + use CTApi\Models\Events\Song\SongCommentRequest; + + $comments = SongCommentRequest::getForSongArrangement(2); + $comment = $comments[0]; + + SongCommentRequest::delete($comment->getIdAsInteger()); + +``` + + ## Retrieve Data from CCLI **Retrieve Lyrics for CCLI-Number:** @@ -374,4 +433,4 @@ The method returns a nullable [File-Model](/../../src/Models/File.php). // The whole song-statistic will be fetched for every "find"-call. -``` +``` \ No newline at end of file diff --git a/docs/src/ressources/SongAPI.md b/docs/src/ressources/SongAPI.md index 002e2214..b1042cde 100644 --- a/docs/src/ressources/SongAPI.md +++ b/docs/src/ressources/SongAPI.md @@ -16,6 +16,21 @@ {{ \CTApi\Test\Unit\Docs\SongRequestTest.testUpdateArrangement }} +## Retrieve, create and delete comments: + +**Retrieve comments:** + +{{ \CTApi\Test\Unit\Docs\SongCommentRequestTest.testGetAllComments }} + +**Create comment:** + +{{ \CTApi\Test\Unit\Docs\SongCommentRequestTest.testCreateComments }} + +**Delete comments:** + +{{ \CTApi\Test\Unit\Docs\SongCommentRequestTest.testDeleteComments }} + + ## Retrieve Data from CCLI **Retrieve Lyrics for CCLI-Number:** @@ -48,4 +63,4 @@ The method returns a nullable [File-Model](/../../src/Models/File.php). **Lazy-Builder:** -{{ \CTApi\Test\Unit\Docs\SongStatisticRequestTest.testLazy }} +{{ \CTApi\Test\Unit\Docs\SongStatisticRequestTest.testLazy }} \ No newline at end of file diff --git a/src/Models/Common/Domain/Meta.php b/src/Models/Common/Domain/Meta.php index 87657906..5feb8622 100644 --- a/src/Models/Common/Domain/Meta.php +++ b/src/Models/Common/Domain/Meta.php @@ -80,8 +80,8 @@ public function requestCreatedPerson(): ?Person public function requestModifiedPerson(): ?Person { - if (!is_null($this->getCreatedPerson())) { - $id = $this->getModifiedPerson()?->getId(); + if (!is_null($this->getModifiedPerson())) { + $id = $this->getModifiedPerson()->getId(); return $this->requestPerson($id); } return null; diff --git a/src/Models/Events/Song/SongComment.php b/src/Models/Events/Song/SongComment.php new file mode 100644 index 00000000..b25ff15c --- /dev/null +++ b/src/Models/Events/Song/SongComment.php @@ -0,0 +1,94 @@ +domainType = $value; + break; + case "domain_id": + $this->domainId = $value; + break; + case "modified_date": + if ($this->meta === null) { + $this->meta = new Meta(); + } + $this->meta->setModifiedDate($value); + break; + case "modified_pid": + if ($this->meta === null) { + $this->meta = new Meta(); + } + $this->meta->setModifiedPerson(Person::createModelFromData(["id" => $value])); + break; + default: + $this->fillDefault($key, $value); + } + } + + public function getDomainId(): ?string + { + return $this->domainId; + } + + public function setDomainId(?string $domainId): SongComment + { + $this->domainId = $domainId; + return $this; + } + + public function getDomainType(): ?string + { + return $this->domainType; + } + + public function setDomainType(?string $domainType): SongComment + { + $this->domainType = $domainType; + return $this; + } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(?string $text): SongComment + { + $this->text = $text; + return $this; + } + + public function setId(?string $id) + { + $this->id = $id; + return $this; + } + + public function getMeta(): ?Meta + { + return $this->meta; + } + + public function setMeta(?Meta $meta): SongComment + { + $this->meta = $meta; + return $this; + } +} \ No newline at end of file diff --git a/src/Models/Events/Song/SongCommentRequest.php b/src/Models/Events/Song/SongCommentRequest.php new file mode 100644 index 00000000..3169560e --- /dev/null +++ b/src/Models/Events/Song/SongCommentRequest.php @@ -0,0 +1,26 @@ +getComments(); + } + + public static function create(int $arrangementId, string $text): void + { + $builder = new SongCommentRequestBuilder($arrangementId); + $builder->createComment($text); + } + + public static function delete(int $commentId): void + { + $builder = new SongCommentRequestBuilder(0); + $builder->deleteComment($commentId); + } + +} \ No newline at end of file diff --git a/src/Models/Events/Song/SongCommentRequestBuilder.php b/src/Models/Events/Song/SongCommentRequestBuilder.php new file mode 100644 index 00000000..0c493494 --- /dev/null +++ b/src/Models/Events/Song/SongCommentRequestBuilder.php @@ -0,0 +1,46 @@ +requestAjax("churchservice/ajax", "getComments", [ + "domain_type" => "arrangement", + "domain_id" => $this->arrangementId + ]); + + $data = CTResponseUtil::dataAsArray($response); + return SongComment::createModelsFromArray(array_values($data)); + } + + public function createComment(string $text): void + { + $this->requestAjax("churchservice/ajax", "addComment", [ + "domain_type" => "arrangement", + "domain_id" => $this->arrangementId, + "text" => $text + ]); + } + + public function deleteComment(int $commentId): void + { + $this->requestAjax("churchservice/ajax", "delComment", [ + "id" => $commentId, + ]); + } + +} \ No newline at end of file diff --git a/tests/Integration/Requests/SongCommentRequestTest.php b/tests/Integration/Requests/SongCommentRequestTest.php new file mode 100644 index 00000000..46cdcb7c --- /dev/null +++ b/tests/Integration/Requests/SongCommentRequestTest.php @@ -0,0 +1,59 @@ +arrangementId = IntegrationTestData::getFilterAsInt("get_song_comments", "song_arrangement_id"); + } + + public function testGetSongArrangement() + { + $comments = SongCommentRequest::getForSongArrangement($this->arrangementId); + + $commentId = IntegrationTestData::getResultAsInt("get_song_comments", "any_comment.id"); + $foundComment = null; + foreach ($comments as $comment) { + if ($comment->getId() == $commentId) { + $foundComment = $comment; + } + } + $this->assertNotNull($foundComment); + $this->assertEqualsTestData("get_song_comments", "any_comment.text", $foundComment->getText()); + $this->assertEqualsTestData("get_song_comments", "any_comment.modified_date", $foundComment->getMeta()?->getModifiedDate()); + $this->assertEqualsTestData("get_song_comments", "any_comment.modified_person_id", $foundComment->getMeta()?->getModifiedPerson()?->getIdAsInteger()); + } + + public function testCreateAndDeleteSongArrangement() + { + $arrangementId = IntegrationTestData::getFilterAsInt("create_and_delete_song_comments", "song_arrangement_id"); + SongCommentRequest::create($arrangementId, "Hello world!"); + + $anyComment = null; + $allComments = SongCommentRequest::getForSongArrangement($arrangementId); + foreach ($allComments as $comment) { + if ($comment->getText() === "Hello world!") { + $anyComment = $comment; + } + } + $this->assertNotNull($anyComment); + + // DELETE ALL COMMENTS + foreach ($allComments as $comment) { + SongCommentRequest::delete($comment->getIdAsInteger()); + } + $allComments = SongCommentRequest::getForSongArrangement($arrangementId); + $this->assertEquals(0, sizeof($allComments)); + } + +} \ No newline at end of file diff --git a/tests/Integration/integration-test-data.json b/tests/Integration/integration-test-data.json index d85c054b..bee1a2ed 100644 --- a/tests/Integration/integration-test-data.json +++ b/tests/Integration/integration-test-data.json @@ -257,6 +257,26 @@ } } }, + "get_song_comments": { + "description": "Request song comments", + "filter": { + "song_arrangement_id": 3 + }, + "result": { + "any_comment": { + "id": 2, + "text": "Ich finde den Song super!", + "modified_date": "2023-12-11 13:06:35", + "modified_person_id": 1 + } + } + }, + "create_and_delete_song_comments": { + "description": "Create and delete song comments", + "filter": { + "song_arrangement_id": 14 + } + }, "get_service": { "description": "Request single service. The corresponding service-group is stored in result.", "filter": { diff --git a/tests/Unit/Docs/SongCommentRequestTest.php b/tests/Unit/Docs/SongCommentRequestTest.php new file mode 100644 index 00000000..0b36a875 --- /dev/null +++ b/tests/Unit/Docs/SongCommentRequestTest.php @@ -0,0 +1,49 @@ +assertEquals(2, $comment->getId()); + $this->assertEquals(3, $comment->getDomainId()); + $this->assertEquals("arrangement", $comment->getDomainType()); + $this->assertEquals("Ich finde den Song super!", $comment->getText()); + $this->assertEquals("2023-12-11 13:06:35", $comment->getMeta()?->getModifiedDate()); + $this->assertEquals(12, $comment->getMeta()?->getModifiedPerson()?->getId()); + + $person = $comment->getMeta()?->requestModifiedPerson(); + + $this->assertEquals("David", $person->getFirstName()); + } + + /** + * @return void + * @doesNotPerformAssertions + */ + public function testCreateComments() + { + SongCommentRequest::create(2, "Die Tonart ist super für Sopran."); + } + + /** + * @return void + * @doesNotPerformAssertions + */ + public function testDeleteComments() + { + $comments = SongCommentRequest::getForSongArrangement(2); + $comment = $comments[0]; + + SongCommentRequest::delete($comment->getIdAsInteger()); + } + +} \ No newline at end of file diff --git a/tests/Unit/HttpMock/data/POST_index.php_q=churchservice_ajax_func=getcomments.json b/tests/Unit/HttpMock/data/POST_index.php_q=churchservice_ajax_func=getcomments.json new file mode 100644 index 00000000..1bb93553 --- /dev/null +++ b/tests/Unit/HttpMock/data/POST_index.php_q=churchservice_ajax_func=getcomments.json @@ -0,0 +1,14 @@ +{ + "status": "success", + "data": { + "2": { + "id": "2", + "domain_type": "arrangement", + "domain_id": "3", + "securitylevel_id": null, + "text": "Ich finde den Song super!", + "modified_date": "2023-12-11 13:06:35", + "modified_pid": "12" + } + } +} \ No newline at end of file