Skip to content

Commit

Permalink
feat(song-arrangement-comment): retrieve, update and delete song-arra…
Browse files Browse the repository at this point in the history
…ngement comments #182
  • Loading branch information
DumbergerL committed Dec 11, 2023
1 parent d48e02e commit 6c1ec3b
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 4 deletions.
61 changes: 60 additions & 1 deletion docs/out/SongAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down Expand Up @@ -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.

```
```
17 changes: 16 additions & 1 deletion docs/src/ressources/SongAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down Expand Up @@ -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 }}
4 changes: 2 additions & 2 deletions src/Models/Common/Domain/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
94 changes: 94 additions & 0 deletions src/Models/Events/Song/SongComment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace CTApi\Models\Events\Song;

use CTApi\Models\AbstractModel;
use CTApi\Models\Common\Domain\Meta;
use CTApi\Models\Groups\Person\Person;
use CTApi\Traits\Model\FillWithData;

class SongComment extends AbstractModel
{
use FillWithData;

private ?string $domainId = null;
private ?string $domainType = null;
private ?string $text = null;
private ?Meta $meta = null;

protected function fillNonArrayType(string $key, $value): void
{
switch ($key) {
case "domain_type":
$this->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;
}
}
26 changes: 26 additions & 0 deletions src/Models/Events/Song/SongCommentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CTApi\Models\Events\Song;

class SongCommentRequest
{

public static function getForSongArrangement(int $arrangementId): array
{
$builder = new SongCommentRequestBuilder($arrangementId);
return $builder->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);
}

}
46 changes: 46 additions & 0 deletions src/Models/Events/Song/SongCommentRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace CTApi\Models\Events\Song;

use CTApi\Traits\Request\AjaxApi;
use CTApi\Utils\CTResponseUtil;

class SongCommentRequestBuilder
{

use AjaxApi;

public function __construct(
private int $arrangementId
)
{
}

public function getComments()
{
$response = $this->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,
]);
}

}
59 changes: 59 additions & 0 deletions tests/Integration/Requests/SongCommentRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace CTApi\Test\Integration\Requests;

use CTApi\Models\Events\Song\SongCommentRequest;
use CTApi\Test\Integration\IntegrationTestData;
use CTApi\Test\Integration\TestCaseAuthenticated;

class SongCommentRequestTest extends TestCaseAuthenticated
{

private int $arrangementId;

protected function setUp(): void
{
parent::setUp();
$this->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));
}

}
20 changes: 20 additions & 0 deletions tests/Integration/integration-test-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Loading

0 comments on commit 6c1ec3b

Please sign in to comment.