From 6cf0194b39942a01aa9386fd6bf26672054605f7 Mon Sep 17 00:00:00 2001 From: DumbergerL Date: Wed, 31 Jul 2024 12:31:21 +0200 Subject: [PATCH] feat(note): add note request and replace song-comment #215 --- src/Models/Common/Note/Note.php | 66 ++++++++++++++++ src/Models/Common/Note/NoteRequest.php | 62 +++++++++++++++ src/Models/Common/Note/NoteRequestBuilder.php | 75 +++++++++++++++++++ src/Models/Events/Song/SongComment.php | 17 ++++- src/Models/Events/Song/SongCommentRequest.php | 3 + .../Events/Song/SongCommentRequestBuilder.php | 30 ++++---- .../Integration/Requests/NoteRequestTest.php | 71 ++++++++++++++++++ .../Requests/SongCommentRequestTest.php | 1 + tests/Integration/integration-test-data.json | 6 +- tests/Unit/Docs/SongCommentRequestTest.php | 2 +- ...q=churchservice_ajax_func=getcomments.json | 2 +- 11 files changed, 313 insertions(+), 22 deletions(-) create mode 100644 src/Models/Common/Note/Note.php create mode 100644 src/Models/Common/Note/NoteRequest.php create mode 100644 src/Models/Common/Note/NoteRequestBuilder.php create mode 100644 tests/Integration/Requests/NoteRequestTest.php diff --git a/src/Models/Common/Note/Note.php b/src/Models/Common/Note/Note.php new file mode 100644 index 00000000..73b1e6a8 --- /dev/null +++ b/src/Models/Common/Note/Note.php @@ -0,0 +1,66 @@ +setMeta(Meta::createModelFromData($data)); + break; + } + } + + public function setId(?string $id): Note + { + $this->id = $id; + return $this; + } + + public function getDomainId(): ?string + { + return $this->domainId; + } + + public function setDomainId(?string $domainId): Note + { + $this->domainId = $domainId; + return $this; + } + + public function getDomainType(): ?string + { + return $this->domainType; + } + + public function setDomainType(?string $domainType): Note + { + $this->domainType = $domainType; + return $this; + } + + public function getText(): ?string + { + return $this->text; + } + + public function setText(?string $text): Note + { + $this->text = $text; + return $this; + } +} \ No newline at end of file diff --git a/src/Models/Common/Note/NoteRequest.php b/src/Models/Common/Note/NoteRequest.php new file mode 100644 index 00000000..0709dd95 --- /dev/null +++ b/src/Models/Common/Note/NoteRequest.php @@ -0,0 +1,62 @@ +domainType . "/" . $this->domainIdentifier; + } + + + /** + * @return Note[] + */ + public function get(): array + { + $ctClient = CTClient::getClient(); + $response = $ctClient->get($this->getApiEndpoint()); + $data = CTResponseUtil::dataAsArray($response); + if (empty($data)) { + return []; + } else { + return Note::createModelsFromArray($data); + } + } + + public function delete(int $noteId): void + { + $ctClient = CTClient::getClient(); + $ctClient->delete($this->getApiEndpoint() . "/" . $noteId); + } + + public function create(string $text, ?int $securityLevelId = null): ?Note + { + $ctClient = CTClient::getClient(); + $response = $ctClient->post($this->getApiEndpoint(), [ + "domainId" => "" . $this->domainIdentifier, + "domainType" => $this->domainType, + "securityLevelId" => $securityLevelId, + "text" => $text, + ]); + + $data = CTResponseUtil::dataAsArray($response); + if (empty($data)) { + return null; + } else { + return Note::createModelFromData($data); + } + } + + public function update(int $noteId, string $text, ?int $securityLevelId = null): ?Note + { + $ctClient = CTClient::getClient(); + $response = $ctClient->put($this->getApiEndpoint() . '/' . $noteId, [ + "text" => $text, + "securityLevelId" => $securityLevelId, + ]); + + $data = CTResponseUtil::dataAsArray($response); + if (empty($data)) { + return null; + } else { + return Note::createModelFromData($data); + } + } +} \ No newline at end of file diff --git a/src/Models/Events/Song/SongComment.php b/src/Models/Events/Song/SongComment.php index 184c44bd..63487579 100644 --- a/src/Models/Events/Song/SongComment.php +++ b/src/Models/Events/Song/SongComment.php @@ -6,15 +6,19 @@ use CTApi\Models\Common\Domain\Meta; use CTApi\Models\Groups\Person\Person; use CTApi\Traits\Model\FillWithData; +use CTApi\Traits\Model\MetaAttribute; +/** + * @deprecated Use Note instead. This class will be removed in the next major-release v3. + */ class SongComment extends AbstractModel { use FillWithData; + use MetaAttribute; private ?string $domainId = null; private ?string $domainType = null; private ?string $text = null; - private ?Meta $meta = null; protected function fillNonArrayType(string $key, $value): void { @@ -42,6 +46,17 @@ protected function fillNonArrayType(string $key, $value): void } } + protected function fillArrayType(string $key, array $data): void + { + switch ($key) { + case "meta": + $this->meta = Meta::createModelFromData($data); + break; + default: + $this->fillDefault($key, $data); + } + } + public function getDomainId(): ?string { return $this->domainId; diff --git a/src/Models/Events/Song/SongCommentRequest.php b/src/Models/Events/Song/SongCommentRequest.php index 7592f659..4faf7334 100644 --- a/src/Models/Events/Song/SongCommentRequest.php +++ b/src/Models/Events/Song/SongCommentRequest.php @@ -2,6 +2,9 @@ namespace CTApi\Models\Events\Song; +/** + * @deprecated Use NoteRequest::forSongArrangement() instead. This class will be removed in the next major-release v3. + */ class SongCommentRequest { public static function getForSongArrangement(int $arrangementId): array diff --git a/src/Models/Events/Song/SongCommentRequestBuilder.php b/src/Models/Events/Song/SongCommentRequestBuilder.php index ff4002be..c404a60a 100644 --- a/src/Models/Events/Song/SongCommentRequestBuilder.php +++ b/src/Models/Events/Song/SongCommentRequestBuilder.php @@ -2,9 +2,13 @@ namespace CTApi\Models\Events\Song; +use CTApi\CTClient; +use CTApi\Models\Common\Note\NoteRequest; use CTApi\Traits\Request\AjaxApi; use CTApi\Utils\CTResponseUtil; - +/** + * @deprecated Use NoteRequest::forSongArrangement() instead. This class will be removed in the next major-release v3. + */ class SongCommentRequestBuilder { use AjaxApi; @@ -16,29 +20,23 @@ public function __construct( public function getComments() { - $response = $this->requestAjax("churchservice/ajax", "getComments", [ - "domain_type" => "arrangement", - "domain_id" => $this->arrangementId - ]); - + $ctClient = CTClient::getClient(); + $response = $ctClient->get( "/api/notes/song_arrangement/" . $this->arrangementId ); $data = CTResponseUtil::dataAsArray($response); - return SongComment::createModelsFromArray(array_values($data)); + if (empty($data)) { + return []; + } else { + return SongComment::createModelsFromArray($data); + } } public function createComment(string $text): void { - $this->requestAjax("churchservice/ajax", "addComment", [ - "domain_type" => "arrangement", - "domain_id" => $this->arrangementId, - "text" => $text - ]); + NoteRequest::forSongArrangement($this->arrangementId)->create($text); } public function deleteComment(int $commentId): void { - $this->requestAjax("churchservice/ajax", "delComment", [ - "id" => $commentId, - ]); + NoteRequest::forSongArrangement($this->arrangementId)->delete($commentId); } - } diff --git a/tests/Integration/Requests/NoteRequestTest.php b/tests/Integration/Requests/NoteRequestTest.php new file mode 100644 index 00000000..b125dc93 --- /dev/null +++ b/tests/Integration/Requests/NoteRequestTest.php @@ -0,0 +1,71 @@ +get(); + + CTConfig::enableDebugging(); + + foreach($data as $note) + { + echo "=> " . $note->getText() . "\n"; + NoteRequest::forGroup(17)->delete($note->getIdAsInteger()); + } + } + + public function testCreateNoteOnGroup() + { + CTConfig::enableDebugging(); + + $data = NoteRequest::forGroup(17)->create("Test String", 1); + print_r($data); + + } + public function testCreateNotOnGroup_DirectWithCURL() + { + $csrfToken = CSRFTokenRequest::getOrFail(); + + $ch = curl_init(); + + $options = [ + CURLOPT_URL => CTConfig::getApiUrl() . '/api/notes/group/17', + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => [ + "domainId" => "" . 17, + "domainType" => "group", + "securityLevelId" => 1, + "text" => "AawdionAWd", + ], + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER=>array('Content-Type:application/json', + "csrf-token:" . $csrfToken + ) + ]; + + curl_setopt_array($ch, $options); + + $cookie = CTConfig::getSessionCookie(); + print_r([$cookie["Name"], $cookie["Value"]]); + if ($cookie != null) { + curl_setopt($ch, CURLOPT_COOKIE, $cookie["Name"] . '=' . $cookie["Value"]); + } + + $resp = curl_exec($ch); + + $curlInfo = curl_getinfo($ch); + var_dump($curlInfo); + + var_dump($resp); + } + +} \ No newline at end of file diff --git a/tests/Integration/Requests/SongCommentRequestTest.php b/tests/Integration/Requests/SongCommentRequestTest.php index a6dfde41..d0ab6fd2 100644 --- a/tests/Integration/Requests/SongCommentRequestTest.php +++ b/tests/Integration/Requests/SongCommentRequestTest.php @@ -2,6 +2,7 @@ namespace CTApi\Test\Integration\Requests; +use CTApi\Models\Events\Song\SongArrangementRequest; use CTApi\Models\Events\Song\SongCommentRequest; use CTApi\Test\Integration\IntegrationTestData; use CTApi\Test\Integration\TestCaseAuthenticated; diff --git a/tests/Integration/integration-test-data.json b/tests/Integration/integration-test-data.json index f1e85653..89688cea 100644 --- a/tests/Integration/integration-test-data.json +++ b/tests/Integration/integration-test-data.json @@ -307,10 +307,10 @@ }, "result": { "any_comment": { - "id": 2, + "id": 179, "text": "Ich finde den Song super!", - "modified_date": "2023-12-11 13:06:35", - "modified_person_id": 1 + "modified_date": "2024-07-31T10:09:27Z", + "modified_person_id": 12 } } }, diff --git a/tests/Unit/Docs/SongCommentRequestTest.php b/tests/Unit/Docs/SongCommentRequestTest.php index ba0f1cf4..cb3a4c78 100644 --- a/tests/Unit/Docs/SongCommentRequestTest.php +++ b/tests/Unit/Docs/SongCommentRequestTest.php @@ -14,7 +14,7 @@ public function testGetAllComments() $this->assertEquals(2, $comment->getId()); $this->assertEquals(3, $comment->getDomainId()); - $this->assertEquals("arrangement", $comment->getDomainType()); + $this->assertEquals("song_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()); 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 index 1bb93553..bc0b143d 100644 --- 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 @@ -3,7 +3,7 @@ "data": { "2": { "id": "2", - "domain_type": "arrangement", + "domain_type": "song_arrangement", "domain_id": "3", "securitylevel_id": null, "text": "Ich finde den Song super!",